[libgfortran,patch] Zero-length strings messed up in library (PR33079)

FX Coudert fxcoudert@gmail.com
Thu Aug 16 14:01:00 GMT 2007


>> -  if (*len > 0)
>> +  if (*len == 0)
>> +    {
>> +      /* A zero-length Fortran string is "".  */
>> +      char * tmp = internal_malloc_size (1);
>> +      tmp[0] = '\0';
>> +      *dest = tmp;
>> +    }
>> +  else
>
> Do you zero-terminate the string because you don't want to allocate  
> zero memory?  I don't know if allocating a pointer to zero memory  
> would work, though.

I don't think you can portably call malloc(0) and expect a non-NULL  
pointer out of it. From http://www.opengroup.org/onlinepubs/009695399/ 
functions/malloc.html :
> If the size of the space requested is 0, the behavior is  
> implementation-defined: the value returned shall be either a null  
> pointer or a unique pointer.
Also, internal_malloc_size catches this case and returns NULL, so I  
think it's more reasonable to special-case zero right there. (And I  
do set the allocated memory to zero just to make sure we don't end up  
with unassigned memory.)

FX



More information about the Gcc-patches mailing list