diff --git a/doc/HACKING b/doc/HACKING index f768976..5ea7994 100644 --- a/doc/HACKING +++ b/doc/HACKING @@ -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. + diff --git a/proto.h b/proto.h index 1120f22..0ac935e 100644 --- a/proto.h +++ b/proto.h @@ -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); diff --git a/rdesktop.c b/rdesktop.c index 8446188..43a8514 100644 --- a/rdesktop.c +++ b/rdesktop.c @@ -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) diff --git a/xwin.c b/xwin.c index 08178f1..44aa2d3 100644 --- a/xwin.c +++ b/xwin.c @@ -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); }