On Sat, Nov 28, 2009 at 1:29 AM, Rob Landley <r...@landley.net> wrote: > On Saturday 28 November 2009 01:06:30 Kevin Day wrote: >> On Fri, Nov 27, 2009 at 2:45 PM, Rob Landley <r...@landley.net> wrote: >> > On Friday 27 November 2009 11:45:50 Kevin Day wrote: >> >> I was looking at uClibc 0.9.28.3 to try and apply this patch and >> >> noticed: >> >> >> >> At the very end of your patch, the untouched code only has >> >> free(packet); while 0.9.28.3 has: >> >> if (packet) >> >> free(packet); >> >> >> >> Does anybody know why was this safety check removed? >> > >> > From the Single Unix Specification version 4 (I.E. SUSv4, I.E. POSIX >> > 2008): >> > >> > http://www.opengroup.org/onlinepubs/9699919799/functions/free.html >> > >> > > If ptr is a null pointer, no action shall occur. >> > >> > I.E. free() has a null check built-in, as required by POSIX. >> > >> > Rob >> > -- >> > Latency is more important than throughput. It's that simple. - Linus >> > Torvalds >> >> So you are saying a double-free cannot happen here? > > I'm saying that freeing a NULL pointer is a NOP, so testing for NULL before > calling free() is a waste of bytes. > > What does a double-free have to do with this? If the pointer is NULL, then > there's nothing to free. If the pointer isn't NULL, the test isn't relevant. > > Legal (and pointless) code: > > void does_nothing(void) > { > x=NULL; > > free(x); > free(x); > free(x); > } > > Rob > -- > Latency is more important than throughput. It's that simple. - Linus Torvalds >
Don't worry, that part is not being misunderstood. What I was saying is that: x = malloc(sizeof(int)); free(x); free(x); <-- bad the fact that somebody was doing a test to see if the pointer was NULL before freeing it suggested to me that somebody with the older uClibc version believed a double free would be possible at this point in the code. Seeing the if(x)'s removed triggered my habits and I forgot (because I drilled the some habits into myself) that your free's do not set x to NULL after freeing it. Your previous post already defined that the if (pointer) was being removed to save space as it is safe to do so. Once I realized that I had my habits being applied to my thought process, I realized that my statement was not relevant. Thus my immediate second post stating at the start "Never mind.." And for the record, I precede my free's with if (x) to avoid extra function calls as I suspect doing an if () test is resourcefully cheaper than doing a function call. Though I never bothered to try and prove it.. However, this is uClibc and one of your goals is to save binary space. This is why I didn't bother continuing the discussion on the if () part. Again, I forgot this because I had drilled these habits into myself. It was simply me forgetting why I did something and reacting instinctively. And so I try to conclude again with, never mind. -- Kevin Day _______________________________________________ uClibc mailing list uClibc@uclibc.org http://lists.busybox.net/mailman/listinfo/uclibc