RPM Community Forums

Mailing List Message of <popt-devel>

Re: Question about the xmalloc() et al functions

From: Jeff Johnson <n3npq@mac.com>
Date: Mon 10 Mar 2008 - 09:22:28 CET
Message-Id: <D9485866-1BED-4066-ABC8-C6CB9A537CF5@mac.com>

On Mar 9, 2008, at 10:00 PM, Wayne Davison wrote:

> On Sun, Mar 09, 2008 at 08:27:25PM -0400, Jeff Johnson wrote:
>> Using xmalloc just opens up a can-of-worms while lusers fuss about
>> non-gcc compiler extension portability.
> Aha, I had failed to notice that the "? :" bit was a gcc extension.
> What I don't like is that normal users get some memory functions that
> are fatal if they use gcc, while all others get non-fatal functions.
> Perhaps the default should be for all normal compiles to get the non-
> fatal functions, and then anyone that wanted to use the current gcc
> defines could set something extra (such as -DFATAL_MEM).

Very good point. OTOH, I don't understand why some lusers are permitted
vasprintf and strlcpy, while others have to hobble along with  

> One other reason I care about this is that I think the various bits of
> code that are doing a strdup()-like action should be using a strdup()-
> like call, not each one having their own set of strlen/malloc/strcpy
> calls.  It's clearer and a bit safer.

Yup. Way too much C developer time is spent fussing with malloc'ing  

> I'm attaching a patch that adds the extra FATAL_MEM requirement, and
> then uses xstrdup() in a few places (which would mean that strdup() is
> being used for normal popt users).

I'm still considering switching to xstrdup, likely used  
inconsistently atm.

> I was also working on changing some code to use your newly-added
> stpcpy() function, so I included that work too.  Some of the changes
> are optimizations to avoid a strlen() call (when we can easily compute
> the length via subtraction) and a fix for a potential memory leak if
> realloc() returns NULL.

Nice. I optimized away a few variables, and rewrote findProgramPath,  
adding your patch.

See what you think. I sometimes regret later what seems clever atm.

Off to waste some more development time malloc'ing strings ;-)

73 de Jeff
Received on Mon Mar 10 09:22:40 2008
Driven by Jeff Johnson and the RPM project team.
Hosted by OpenPKG and Ralf S. Engelschall.
Powered by FreeBSD and OpenPKG.