diff options
| author | Greg Ercolano <erco@seriss.com> | 2013-09-20 03:36:02 +0000 |
|---|---|---|
| committer | Greg Ercolano <erco@seriss.com> | 2013-09-20 03:36:02 +0000 |
| commit | 2fc6c3a39b6be6f8a6b9a9dd1d030b46d7ea71bf (patch) | |
| tree | 0798c0c98a7be4b9cc0f7d2c18786f928e1c4af3 /src | |
| parent | 67ed1a8da999af762085a516833ba99523ccaf48 (diff) | |
Fixes STR #2985: prevent memory leaks with XGetWindowProperty()
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@9979 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
| -rw-r--r-- | src/Fl_x.cxx | 32 | ||||
| -rw-r--r-- | src/fl_dnd_x.cxx | 6 |
2 files changed, 27 insertions, 11 deletions
diff --git a/src/Fl_x.cxx b/src/Fl_x.cxx index 6b894b93a..8bcd16538 100644 --- a/src/Fl_x.cxx +++ b/src/Fl_x.cxx @@ -709,7 +709,7 @@ static void fl_init_workarea() { Atom actual; unsigned long count, remaining; int format; - unsigned *xywh; + unsigned *xywh = 0; /* If there are several screens, the _NET_WORKAREA property does not give the work area of the main screen, but that of all screens together. @@ -733,8 +733,8 @@ static void fl_init_workarea() { fl_workarea_xywh[1] = (int)xywh[1]; fl_workarea_xywh[2] = (int)xywh[2]; fl_workarea_xywh[3] = (int)xywh[3]; - XFree(xywh); } + if ( xywh ) { XFree(xywh); xywh = 0; } } int Fl::x() { @@ -824,6 +824,13 @@ void fl_sendClientMessage(Window window, Atom message, /* Get window property value (32 bit format) Returns zero on success, -1 on error + + 'data' should be freed with XFree() using this pattern: + + unsigned long *data = 0; + if (0 == get_xwinprop(....., &nitems, &data) ) { ..success.. } + else { ..fail.. } + if ( data ) { XFree(data); data=0; } */ static int get_xwinprop(Window wnd, Atom prop, long max_length, unsigned long *nitems, unsigned long **data) { @@ -1152,6 +1159,7 @@ int fl_handle(const XEvent& thisevent) else handle_clipboard_timestamp(0, t); } + XFree(portion); portion = 0; return true; } @@ -1171,7 +1179,7 @@ int fl_handle(const XEvent& thisevent) t == fl_XaTextUriList || t == fl_XaCompoundText) type = t; } - XFree(portion); + XFree(portion); portion = 0; Atom property = xevent.xselection.property; XConvertSelection(fl_display, property, type, property, fl_xid(Fl::first_window()), @@ -1180,12 +1188,12 @@ int fl_handle(const XEvent& thisevent) } // Make sure we got something sane... if ((portion == NULL) || (format != 8) || (count == 0)) { - if (portion) XFree(portion); + if (portion) { XFree(portion); portion = 0; } return true; - } + } buffer = (unsigned char*)realloc(buffer, bytesread+count+remaining+1); memcpy(buffer+bytesread, portion, count); - XFree(portion); + if (portion) { XFree(portion); portion = 0; } bytesread += count; // Cannot trust data to be null terminated buffer[bytesread] = '\0'; @@ -1312,14 +1320,17 @@ int fl_handle(const XEvent& thisevent) XGetWindowProperty(fl_display, fl_dnd_source_window, fl_XdndTypeList, 0, 0x8000000L, False, XA_ATOM, &actual, &format, &count, &remaining, &buffer); - if (actual != XA_ATOM || format != 32 || count<4 || !buffer) + if (actual != XA_ATOM || format != 32 || count<4 || !buffer) { + if ( buffer ) { XFree(buffer); buffer = 0; } goto FAILED; + } delete [] fl_dnd_source_types; fl_dnd_source_types = new Atom[count+1]; for (unsigned i = 0; i < count; i++) { fl_dnd_source_types[i] = ((Atom*)buffer)[i]; } fl_dnd_source_types[count] = 0; + XFree(buffer); buffer = 0; } else { FAILED: // less than four data types, or if the above messes up: @@ -1692,6 +1703,7 @@ int fl_handle(const XEvent& thisevent) } } } + if ( words ) { XFree(words); words = 0; } } if (window->fullscreen_active() && !fullscreen_state) { window->_clear_fullscreen(); @@ -1891,9 +1903,11 @@ int Fl_X::ewmh_supported() { if (0 == get_xwinprop(XRootWindow(fl_display, fl_screen), fl_NET_SUPPORTING_WM_CHECK, 64, &nitems, &words) && nitems == 1) { Window child = words[0]; + if ( words ) { XFree(words); words = 0; } if (0 == get_xwinprop(child, fl_NET_SUPPORTING_WM_CHECK, 64, - &nitems, &words) && nitems == 1) { - result = (child == words[0]); + &nitems, &words) ) { + if ( nitems == 1) result = (child == words[0]); + if ( words ) { XFree(words); words = 0; } } } } diff --git a/src/fl_dnd_x.cxx b/src/fl_dnd_x.cxx index ac635e520..ca449cfa5 100644 --- a/src/fl_dnd_x.cxx +++ b/src/fl_dnd_x.cxx @@ -55,9 +55,11 @@ static int dnd_aware(Window& window) { 0, 4, False, XA_ATOM, &actual, &format, &count, &remaining, &data); + int ret = 0; if (actual == XA_ATOM && format==32 && count && data) - return int(*(Atom*)data); - return 0; + ret = int(*(Atom*)data); + if (data) { XFree(data); data = 0; } + return ret; } static int grabfunc(int event) { |
