[PATCH 3/3] RRModeCreate: plug memory leak of newModes if AddResource fails

Alan Coopersmith alan.coopersmith at oracle.com
Sat Jul 14 11:34:10 PDT 2012


Reported by parfait 1.0:

Error: Memory leak (CWE 401)
   Memory leak of pointer 'newModes' allocated with realloc(((char*)modes), ((num_modes + 1) * 8))
        at line 93 of randr/rrmode.c in function 'RRModeCreate'.
          pointer allocated at line 82 with realloc(((char*)modes), ((num_modes + 1) * 8)).
Error: Memory leak (CWE 401)
   Memory leak of pointer 'newModes' allocated with malloc(8)
        at line 93 of randr/rrmode.c in function 'RRModeCreate'.
          pointer allocated at line 84 with malloc(8).

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 randr/rrmode.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/randr/rrmode.c b/randr/rrmode.c
index 56e5977..f5d3f9e 100644
--- a/randr/rrmode.c
+++ b/randr/rrmode.c
@@ -89,8 +89,10 @@ RRModeCreate(xRRModeInfo * modeInfo, const char *name, ScreenPtr userScreen)
     }
 
     mode->mode.id = FakeClientID(0);
-    if (!AddResource(mode->mode.id, RRModeType, (pointer) mode))
+    if (!AddResource(mode->mode.id, RRModeType, (pointer) mode)) {
+        free(newModes);
         return NULL;
+    }
     modes = newModes;
     modes[num_modes++] = mode;
 
-- 
1.7.9.2



More information about the xorg-devel mailing list