[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Minios-devel] [UNIKRAFT PATCH v2 02/10] lib/nolibc: Add strdup function



Hey,

that would be incorrect behavior of strdup. There is no mention about
any length limit in the "man strdup". I checked the musl's
implementation - it does not have any limitation either.

So in case of improbable event that the source string is longer then
SIZE_MAX, the duplicate will be a truncated copy. Happy debugging :)

- Yuri

Simon Kuenzer <simon.kuenzer@xxxxxxxxx> writes:

> Hey,
>
> On 23.08.2018 12:59, Costin Lupu wrote:
>> Shamelessly taken from Mini-OS.
>> 
>> Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
>> ---
>>   lib/nolibc/include/string.h |  1 +
>>   lib/nolibc/string.c         | 17 +++++++++++++++++
>>   2 files changed, 18 insertions(+)
>> 
>> diff --git a/lib/nolibc/include/string.h b/lib/nolibc/include/string.h
>> index 677f528..8674c77 100644
>> --- a/lib/nolibc/include/string.h
>> +++ b/lib/nolibc/include/string.h
>> @@ -57,6 +57,7 @@ size_t strlen(const char *str);
>>   const char *strchr(const char *str, int c);
>>   int strncmp(const char *str1, const char *str2, size_t len);
>>   int strcmp(const char *str1, const char *str2);
>> +char *strdup(const char *str);
>>   
>>   #ifdef __cplusplus
>>   }
>> diff --git a/lib/nolibc/string.c b/lib/nolibc/string.c
>> index bf89106..bf4ab50 100644
>> --- a/lib/nolibc/string.c
>> +++ b/lib/nolibc/string.c
>> @@ -33,6 +33,7 @@
>>    * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
>>    */
>>   
>> +#include <stdlib.h>
>>   #include <stdint.h>
>>   #include <string.h>
>>   #include <limits.h>
>> @@ -166,3 +167,19 @@ int strcmp(const char *str1, const char *str2)
>>   
>>      return __res;
>>   }
>> +
>> +char *strdup(const char *str)
>> +{
>> +    char *__res;
>> +    int __len;
>> +
>> +    __len = strlen(str);
>> +
>> +    __res = malloc(__len + 1);
>> +    if (!__res)
>> +            return NULL;
>> +
>> +    memcpy(__res, str, __len + 1);
>> +
>> +    return __res;
>> +}
>> 
>
> Could you provide an strndup() instead and make strdup() use it? This 
> way we would cover both libc variants with one patch.
>
> I mean, you would do something like:
>
> char *strdup(const char *str)
> {
>       return strndup(str, SIZE_MAX);
> }
>
> Cheers,
>
> Simon

-- 
Yuri Volchkov
Software Specialist

NEC Europe Ltd
Kurfürsten-Anlage 36
D-69115 Heidelberg

_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.