[Mesa-dev] [PATCH] main: replace str*dup() by str*cpy() to be compliant with C89, C99.
Samuel Iglesias Gonsálvez
siglesias at igalia.com
Mon Sep 28 04:48:56 PDT 2015
On 28/09/15 13:34, Jose Fonseca wrote:
> On 28/09/15 09:09, Samuel Iglesias Gonsalvez wrote:
>> Fix SCons MinGW build error:
>>
>> Compiling src/mesa/main/shader_query.cpp ...
>> src/mesa/main/shader_query.cpp: In function ‘char*
>> get_top_level_name(const char*)’:
>> src/mesa/main/shader_query.cpp:841:34: error: ‘strndup’ was not
>> declared in this scope
>> return strndup(name, name_size);
>> ^
>> Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=92124
>> Signed-off-by: Samuel Iglesias Gonsalvez <siglesias at igalia.com>
>
> Thanks for looking into this.
>
>> ---
>> src/mesa/main/shader_query.cpp | 19 ++++++++++++++-----
>> 1 file changed, 14 insertions(+), 5 deletions(-)
>>
>> diff --git a/src/mesa/main/shader_query.cpp
>> b/src/mesa/main/shader_query.cpp
>> index e020dce..7eecb39 100644
>> --- a/src/mesa/main/shader_query.cpp
>> +++ b/src/mesa/main/shader_query.cpp
>> @@ -838,18 +838,27 @@ get_top_level_name(const char *name)
>> else
>> name_size = first_square_bracket - name;
>>
>> - return strndup(name, name_size);
>> + char *result = (char *) malloc((name_size + 1) * sizeof(char));
>> + strncpy(result, name, name_size);
>> + result[name_size] = '\0';
>> + return result;
>
> I think we should add a strndup inline implementation in a shared header
> somewhere. We should avoid having the strndup reimplemented repeatedly,
> as that is error prone.
>
> And that implementation should malloc the length of the resulting string
> plus one, not the max_size + 1 to avoid wasting memory if later strndup
> is invoked with a max_size much larger than the incoming string.
>
Yeah, good idea. I will look into it.
Please ignore this patch.
>> }
>>
>> static char*
>> get_var_name(const char *name)
>> {
>> const char *first_dot = strchr(name, '.');
>> + char *result;
>>
>> - if (!first_dot)
>> - return strdup(name);
>> -
>
> Both MinGW and MSVC have strdup. They just lack strndup.
>
OK, I didn't know.
>> - return strndup(first_dot+1, strlen(first_dot) - 1);
>
> I think we could just return `strdup(first_dot)`.
>
Actually, strdup(first_dot+1) to get rid of the starting '.' char.
Thanks,
Sam
>
>> + if (!first_dot) {
>> + result = (char *) malloc(strlen(name) * sizeof(char));
>> + return strcpy(result, name);
>> + }
>> + int var_name_size = strlen(first_dot) - 1;
>> + result = (char *) malloc((var_name_size + 1) * sizeof(char));
>> + strncpy(result, first_dot+1, var_name_size);
>> + result[var_name_size] = '\0';
>> + return result;
>> }
>>
>> static GLint
>>
>
>
> Jose
>
More information about the mesa-dev
mailing list