-
Notifications
You must be signed in to change notification settings - Fork 55
scrotGrabMousePointer: error out on failure #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -448,33 +448,24 @@ Window scrotGetWindow(Display *display, Window window, int x, int y) | |
void scrotGrabMousePointer(const Imlib_Image image, const int xOffset, | ||
const int yOffset) | ||
{ | ||
XFixesCursorImage *xcim = NULL; | ||
uint32_t *pixels = NULL; | ||
/* Get the X cursor and the information we want. */ | ||
if ((xcim = XFixesGetCursorImage(disp)) == NULL) { | ||
warnx("Can't get the cursor from X"); | ||
goto end; | ||
} | ||
XFixesCursorImage *xcim = XFixesGetCursorImage(disp); | ||
if (!xcim) | ||
errx(EXIT_FAILURE, "Can't get the cursor from X"); | ||
const unsigned short width = xcim->width; | ||
const unsigned short height = xcim->height; | ||
const size_t pixcnt = (size_t)width*height; | ||
|
||
/* xcim->pixels wouldn't be valid if sizeof(*pixels)*pixcnt wrapped around. | ||
*/ | ||
if ((pixels = malloc(sizeof(*pixels)*pixcnt)) == NULL) { | ||
warn("malloc()"); | ||
goto end; | ||
} | ||
uint32_t *pixels = (uint32_t *)xcim->pixels; | ||
/* XFixesCursorImage returns pixels as `unsigned long`, which is typically | ||
* 64bits, but imlib2 expects 32bit packed integers. */ | ||
for (size_t i = 0; i < pixcnt; i++) | ||
pixels[i] = xcim->pixels[i]; | ||
if (sizeof *xcim->pixels > sizeof *pixels) { | ||
for (size_t i = 0; i < pixcnt; ++i) | ||
pixels[i] = xcim->pixels[i]; | ||
} | ||
Comment on lines
+458
to
+464
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does violate C's aliasing rules: https://www.iso-9899.info/n1570.html#6.5p7 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I've read that and explained in the commit why there's no violation. If you think otherwise then feel free to explain in more detail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To elaborate, each element of an array is an object by itself (C99 § 3.14). The write into
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's perfectly valid as far as C goes that Perhaps we can let it slip, Eric S. Raymond's struct packing guide says:
i.e. in all modern machines There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, no unnecessary casts as in line 458 please. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The cast wasn't there on my first iteration, but it caused a compiler warning. So I added it.
Okay, that's fair. I was only thinking of aliasing and didn't consider alignment. But if such a machine exists, I kinda doubt Imlib2 will work there, because IIRC I saw some of the Imlib2 loaders treat raw memory as struct as well - which would most likely be broken under such machines. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ugh, compilers forcing us to write C++-like C. Edit: actually, I guess it's not the compiler's fault if we write known UB and it warns, the unnecessary cast is the "I know what I'm doing" idiom of C. |
||
|
||
Imlib_Image imcursor = imlib_create_image_using_data(width, height, pixels); | ||
if (!imcursor) { | ||
warnx("Can't create cursor image"); | ||
goto end; | ||
} | ||
if (!imcursor) | ||
errx(EXIT_FAILURE, "Can't create cursor image"); | ||
|
||
/* Overlay the cursor into `image`. */ | ||
const int x = (xcim->x - xcim->xhot) - xOffset; | ||
|
@@ -486,9 +477,6 @@ void scrotGrabMousePointer(const Imlib_Image image, const int xOffset, | |
height); | ||
imlib_context_set_image(imcursor); | ||
imlib_free_image(); | ||
|
||
end: | ||
free(pixels); | ||
XFree(xcim); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.