printf() leak?

Eitan Adler lists at eitanadler.com
Tue Mar 29 08:00:09 UTC 2011


Hi David,

> It seems printf() always alloc something and does not free it:

What compiler and what optimizations? Most compilers will optimize a
printf without any special formatting into a puts call instead of a
printf call.
For example clang -O3 -fomit-frame-pointer (which I use for clarity
here) outputs this code:

        .file   "leak.c"
...
main:                                   # @main
# BB#0:                                 # %entry
        subl    $12, %esp
        movl    $str, (%esp)
        calll   puts
        xorl    %eax, %eax
        addl    $12, %esp
        ret
.Ltmp0:
...
str:
        .asciz   "Hi"
        .size   str, 3
...

 [snip]
> ==67840==         suppressed: 4,096 bytes in 1 blocks

Lets take a look at what valgrind says immediately after this:
==14481== For counts of detected and suppressed errors, rerun with: -v

One of the lines we get is
--14508-- used_suppression:      1 libc puts leak

Which means it is a known issue and has been specially marked as to
avoid being reported by valgrind.

Lets take a look to see where this suppression happens: in
/usr/local/lib/valgrind/default.supp we find
{
   libc puts leak
   Memcheck:Leak
   fun:malloc
   obj:/lib/libc.so.7
   obj:/lib/libc.so.7
   obj:/lib/libc.so.7
   fun:puts
   fun:main
}

After some investigation I was able to find the following commit:
http://p4db.freebsd.org/chv.cgi?CH=168767 which shows when this
suppression was added and by whom.

I trust that if you are interested in the details of why this leak is
detected you have the skills to follow up on this by yourself :-)

Thank you for trying to make FreeBSD better!


-- 
Eitan Adler


More information about the freebsd-questions mailing list