Skip to contentSkip to conversation

string: factorize skip_spaces and export it to be generally available

PATCH Anders Larsen 2 participants 2 messages Nov 15, 2009 – Nov 16, 2009 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20091115200215.GA25351@machine

#1 lore raw
Quoted 11 lines · Anonymous
Quoted 10 lines · Anders Larsen
Quoted 7 lines · André Goddard Rosa
(quoted diff) +6 -0
+char *skip_spaces(const char *str)
+{
+	while (isspace(*str))
+		++str;
+	return (char *)str;
+}

Is there a good reason why the parameter 'str' is declared 'const'

when skip_spaces() returns a non-const pointer into str ? Declaring return value as const won't let us modificate string after

Quoted 2 lines

skipping spaces. Declaring parameter as non-const won't let us giving (const char *) to this function. So i think it is ok.

skip_spaces() _implicitly_ casts away the 'const' of the parameter, which may come as a (nasty) surprise to users of the function.

Consider this (contrieved and buggy) example:

	const char* my_string = " do not modify me! ";
	char* result = strcat(skip_spaces(my_string, "boom!"));

The proposed implementation of skip_spaces() effectively prevents the compiler from catching this obvious bug.

Cheers
Anders

#2 lore raw

Hi, Anders!

Quoted 19 lines · Anders Larsen
Quoted 12 lines · Anonymous
Quoted 10 lines · Anders Larsen
Quoted 7 lines · André Goddard Rosa
(quoted diff) +6 -0
+char *skip_spaces(const char *str)
+{
+    while (isspace(*str))
+            ++str;
+    return (char *)str;
+}

Is there a good reason why the parameter 'str' is declared 'const'

when skip_spaces() returns a non-const pointer into str ? Declaring return value as const won't let us modificate string after

Quoted 2 lines

skipping spaces. Declaring parameter as non-const won't let us giving (const char *) to this function. So i think it is ok.

skip_spaces() _implicitly_ casts away the 'const' of the parameter, which may come as a (nasty) surprise to users of the function.

Quoted 8 lines

Consider this (contrieved and buggy) example:

       const char* my_string = " do not modify me! "; char* result = strcat(skip_spaces(my_string, "boom!"));

The proposed implementation of skip_spaces() effectively prevents the compiler from catching this obvious bug.

I agree with the above comment. It's possible to make it differently char* skip_spaces(char *),
but it's necessary to change many of its callers at the same time:

CC lib/argv_split.o
lib/argv_split.c: In function ‘count_argc’:
lib/argv_split.c:24: warning: passing argument 1 of ‘skip_spaces’ discards qualifiers from pointer target type include/linux/string.h:65: note: expected ‘char *’ but argument is of type ‘const char *’
lib/argv_split.c: In function ‘argv_split’:
lib/argv_split.c:78: warning: passing argument 1 of ‘skip_spaces’ discards qualifiers from pointer target type include/linux/string.h:65: note: expected ‘char *’ but argument is of type ‘const char *’
...
CC lib/vsprintf.o
lib/vsprintf.c: In function ‘vsscanf’:
lib/vsprintf.c:1737: warning: passing argument 1 of ‘skip_spaces’ discards qualifiers from pointer target type include/linux/string.h:65: note: expected ‘char *’ but argument is of type ‘const char *’
lib/vsprintf.c:1738: warning: passing argument 1 of ‘skip_spaces’ discards qualifiers from pointer target type include/linux/string.h:65: note: expected ‘char *’ but argument is of type ‘const char *’
lib/vsprintf.c:1808: warning: passing argument 1 of ‘skip_spaces’ discards qualifiers from pointer target type include/linux/string.h:65: note: expected ‘char *’ but argument is of type ‘const char *’
lib/vsprintf.c:1850: warning: passing argument 1 of ‘skip_spaces’ discards qualifiers from pointer target type include/linux/string.h:65: note: expected ‘char *’ but argument is of type ‘const char *’

    There are other functions following the same pattern presently:

        char *strstr(const char *haystack, const char *needle);
        char *strchr(const char *s, int c);
        char *strrchr(const char *s, int c);

    on both glibc and the kernel.

Thanks,
André