Prevent segfaults in out of memory conditions by checking the pointer returned from XGetImage.

git-svn-id: svn://svn.code.sf.net/p/rdesktop/code/trunk/rdesktop@1305 423420c4-83ab-492f-b58f-81f9feb106b5
This commit is contained in:
Peter Åstrand 2006-10-27 12:59:38 +00:00
parent bcec43cb63
commit 6ac7e0a1be
4 changed files with 23 additions and 0 deletions

View File

@ -26,3 +26,12 @@ rdesktop.c
RDP resources
-------------
http://dev.remotenetworktechnology.com/refdata.htm
Checking for out of memory conditions
-------------------------------------
Try to handle out of memory conditions gracefully. Use the xmalloc
routines from rdesktop.c, instead of calling malloc manually. Also,
remember that several Xlib functions can return NULL. This includes
XGetImage. Use exit_if_null to verify returned pointers.

View File

@ -109,6 +109,7 @@ BOOL pstcache_init(uint8 cache_id);
int main(int argc, char *argv[]);
void generate_random(uint8 * random);
void *xmalloc(int size);
void exit_if_null(void *ptr);
char *xstrdup(const char *s);
void *xrealloc(void *oldmem, int size);
void xfree(void *mem);

View File

@ -1079,6 +1079,17 @@ xmalloc(int size)
return mem;
}
/* Exit on NULL pointer. Use to verify result from XGetImage etc */
void
exit_if_null(void *ptr)
{
if (ptr == NULL)
{
error("unexpected null pointer. Out of memory?\n");
exit(1);
}
}
/* strdup */
char *
xstrdup(const char *s)

2
xwin.c
View File

@ -3192,12 +3192,14 @@ ui_desktop_save(uint32 offset, int x, int y, int cx, int cy)
if (g_ownbackstore)
{
image = XGetImage(g_display, g_backstore, x, y, cx, cy, AllPlanes, ZPixmap);
exit_if_null(image);
}
else
{
pix = XCreatePixmap(g_display, g_wnd, cx, cy, g_depth);
XCopyArea(g_display, g_wnd, pix, g_gc, x, y, cx, cy, 0, 0);
image = XGetImage(g_display, pix, 0, 0, cx, cy, AllPlanes, ZPixmap);
exit_if_null(image);
XFreePixmap(g_display, pix);
}