Skip to contentSkip to conversation

linux-user: Protect against allocation failure in load_symbols.

PATCH Jay Foad 2 participants 2 messages Jul 30, 2010

lore
Share & tools
lore

Jay Foad

author
#0 lore raw

Review

Quoted 10 lines
(quoted diff) +9 -0
+    /* Attempt to free the storage associated with the local symbols
+       that we threw away.  Whether or not this has any effect on the
+       memory allocation depends on the malloc implementation and how
+       many symbols we managed to discard.  */
     syms = realloc(syms, nsyms * sizeof(*syms));
+    if (syms == NULL) {
+        free(s);
+        free(strings);
+        return;
+    }

If realloc() fails it leaves the original object unchanged, so can't you just write:

    t = realloc(syms, nsyms * sizeof(*syms));
    if (t != NULL) {
        syms = t;
    }

?

Jay.

#1 lore raw
Quoted 11 lines · Jay Foad

Review

Quoted 10 lines
(quoted diff) +9 -0
+    /* Attempt to free the storage associated with the local symbols
+       that we threw away.  Whether or not this has any effect on the
+       memory allocation depends on the malloc implementation and how
+       many symbols we managed to discard.  */
     syms = realloc(syms, nsyms * sizeof(*syms));
+    if (syms == NULL) {
+        free(s);
+        free(strings);
+        return;
+    }
Quoted 2 lines

If realloc() fails it leaves the original object unchanged, so can't you just write:

Life is not as simple as one would like it to be.
http://www.mail-archive.com/[email protected]/msg22137.html

Quoted 10 lines
    t = realloc(syms, nsyms * sizeof(*syms));
    if (t != NULL) {
        syms = t;
    }

?

Jay.

Signature · 1 line
mailto:[email protected]