This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[patch] Cleanup: don't call free(NULL) unnecessarily
- From: Paul Pluzhnikov <ppluzhnikov at google dot com>
- To: libc-alpha at sourceware dot org
- Cc: ppluzhnikov at google dot com, ahh at google dot com
- Date: Tue, 05 Mar 2013 11:18:01 -0800
- Subject: [patch] Cleanup: don't call free(NULL) unnecessarily
Greetings,
This patch eliminates calls to free(NULL) from within vfprintf.
This is desirable because e.g. fprintf(stderr, ...) is often called from
signal handlers, and while technically illegal, this tends to work.
Glibc free(NULL) also works in signal context, but end user could supply
his own malloc/free, which may not.
The same check was already done on e.g. line 1648.
I also added missing check for malloc failure.
Tested on Linux/x86_64 -- no new failures.
--
Paul Pluzhnikov
2013-03-05 Paul Pluzhnikov <ppluzhnikov@google.com>
* stdio-common/vfprintf.c (vfprintf): Check malloc return; don't
call free(NULL).
diff --git a/stdio-common/vfprintf.c b/stdio-common/vfprintf.c
index 89126d2..7042090 100644
--- a/stdio-common/vfprintf.c
+++ b/stdio-common/vfprintf.c
@@ -1691,7 +1691,8 @@ do_positional:
/* Just a counter. */
size_t cnt;
- free (workstart);
+ if (__builtin_expect (workstart != NULL, 0))
+ free (workstart);
workstart = NULL;
if (grouping == (const char *) -1)
@@ -1944,6 +1945,11 @@ do_positional:
{
workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
* sizeof (CHAR_T));
+ if (workstart == NULL)
+ {
+ done = -1;
+ goto all_done;
+ }
workend = workstart + (MAX (prec, width) + 32);
}
}
@@ -2021,7 +2027,8 @@ do_positional:
break;
}
- free (workstart);
+ if (__builtin_expect (workstart != NULL, 0))
+ free (workstart);
workstart = NULL;
/* Write the following constant string. */
@@ -2032,8 +2039,10 @@ do_positional:
}
all_done:
- free (args_malloced);
- free (workstart);
+ if (__builtin_expect (args_malloced != NULL, 0))
+ free (args_malloced);
+ if (__builtin_expect (workstart != NULL, 0))
+ free (workstart);
/* Unlock the stream. */
_IO_funlockfile (s);
_IO_cleanup_region_end (0);