Hi Joel, > +2009-12-15 Joel E. Denny <jde...@clemson.edu> > + > + fprintf-posix: fix memory leak > + * lib/fprintf.c (fprintf): Free memory allocated by vasnprintf.
The fix is obviously correct. But it's good that you ask, because the bug occurs also in 3 other places. Below is what I'm committing. > I haven't yet explored how valgrind tests are > supposed to be added to gnulib. Currently we have no special tests for valgrind. Some of us occasionally run the entire testsuite with valgrind enabled, through a definition like this in Makefile.am: CHECKER = #CHECKER = valgrind --tool=memcheck --suppressions=$(srcdir)/../gnulib-lib/malloca.valgrind --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes But for a unit test that is supposed to detect a memory leak, you don't need valgrind. You can make it portable to any platform: - Limit the allowed total memory through a setrlimit call (cf. tests/test-printf-posix2.c). - Run the test in a loop. Determine the iteration count so that when the bug is present, the memory would exceed the limit. Of course, also consider the running time of the test when the bug is fixed. Bruno 2009-12-15 Joel E. Denny <jde...@clemson.edu> Bruno Haible <br...@clisp.org> *printf: Fix memory leak. * lib/fprintf.c (fprintf): Free memory allocated by vasnprintf. * lib/vfprintf.c (vfprintf): Likewise. * lib/dprintf.c (dprintf): Likewise. * lib/vdprintf.c (vdprintf): Likewise. --- lib/dprintf.c.orig 2009-12-15 10:04:24.000000000 +0100 +++ lib/dprintf.c 2009-12-15 10:03:24.000000000 +0100 @@ -57,6 +57,9 @@ return -1; } + if (output != buf) + free (output); + if (len > INT_MAX) { errno = EOVERFLOW; --- lib/fprintf.c.orig 2009-12-15 10:04:24.000000000 +0100 +++ lib/fprintf.c 2009-12-15 10:03:24.000000000 +0100 @@ -1,5 +1,5 @@ /* Formatted output to a stream. - Copyright (C) 2004, 2006-2008 Free Software Foundation, Inc. + Copyright (C) 2004, 2006-2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -63,6 +63,9 @@ return -1; } + if (output != buf) + free (output); + if (len > INT_MAX) { errno = EOVERFLOW; --- lib/vdprintf.c.orig 2009-12-15 10:04:24.000000000 +0100 +++ lib/vdprintf.c 2009-12-15 10:03:24.000000000 +0100 @@ -54,6 +54,9 @@ return -1; } + if (output != buf) + free (output); + if (len > INT_MAX) { errno = EOVERFLOW; --- lib/vfprintf.c.orig 2009-12-15 10:04:24.000000000 +0100 +++ lib/vfprintf.c 2009-12-15 10:03:24.000000000 +0100 @@ -1,5 +1,5 @@ /* Formatted output to a stream. - Copyright (C) 2004, 2006-2008 Free Software Foundation, Inc. + Copyright (C) 2004, 2006-2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -60,6 +60,9 @@ return -1; } + if (output != buf) + free (output); + if (len > INT_MAX) { errno = EOVERFLOW;