On 03/22/2017 10:42 AM, Henning Schild wrote:
> Am Thu, 16 Mar 2017 23:17:58 +0100
> schrieb Ralf Ramsauer <[email protected]>:
> 
>> Though very unlikely that realloc will fail here, its return value
>> should be checked.
>>
>> Fixes: 41646a9a40a7 ("tools: jailhouse: reimplement
>> jailhouse-cell-list in C") Signed-off-by: Ralf Ramsauer
>> <[email protected]> ---
>>  tools/jailhouse.c | 7 ++++++-
>>  1 file changed, 6 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/jailhouse.c b/tools/jailhouse.c
>> index 82f2e1ac41..c23bb3bda7 100644
>> --- a/tools/jailhouse.c
>> +++ b/tools/jailhouse.c
>> @@ -209,8 +209,13 @@ static char *read_sysfs_cell_string(const
>> unsigned int id, const char *entry) 
>>      /* chop trailing linefeeds and enforce the string to be
>>       * null-terminated */
>> -    if (ret[size-1] != '\n')
>> +    if (ret[size-1] != '\n') {
>>              ret = realloc(ret, ++size);
>> +            if (ret == NULL) {
>> +                    fprintf(stderr, "insufficient memory\n");
>> +                    exit(1);
>> +            }
>> +    }
> 
> Looking at that i wondered why not use strerror(errno). But looking at
> the code i saw that you just copied a pattern where strerror is used
> everywhere but for malloc.
Exactly, I just copied the existing malloc() error handling pattern to
produce equivalent error messages for the same error cases.
> 
> Is there a reason to treat malloc different? The man-page says
No, I just aligned to existing code. :-)
> something about private implementations that may fail to set errno. But
> that should not be the case and we could still use strerror(ENOMEM).
Ack, relying on errno could be dangerous, but strerror(ENOMEM) would
work, but should then be consistently changed for the whole file.

Would produce more standardised error message...

  Ralf
> 
> Henning
> 
>>      ret[size-1] = 0;
>>  
>>      return ret;
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to