* Re: [PATCH v5 10/12] string: factorize skip_spaces and export it to be generally available
[not found] <20091115200215.GA25351@machine>
@ 2009-11-15 21:51 ` Anders Larsen
2009-11-16 12:21 ` André Goddard Rosa
0 siblings, 1 reply; 4+ messages in thread
From: Anders Larsen @ 2009-11-15 21:51 UTC (permalink / raw)
To: Anonymous; +Cc: André Goddard Rosa, Andrew Morton, linux-kernel
On 2009-11-15 21:02:15, Anonymous wrote:
> On Sun, Nov 15, 2009 at 08:33:29PM +0100, Anders Larsen wrote:
> > On 2009-11-15 08:15:03, André Goddard Rosa wrote:
> > >+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
> 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
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v5 10/12] string: factorize skip_spaces and export it to be generally available
2009-11-15 21:51 ` [PATCH v5 10/12] string: factorize skip_spaces and export it to be generally available Anders Larsen
@ 2009-11-16 12:21 ` André Goddard Rosa
0 siblings, 0 replies; 4+ messages in thread
From: André Goddard Rosa @ 2009-11-16 12:21 UTC (permalink / raw)
To: Anders Larsen; +Cc: Anonymous, Andrew Morton, linux-kernel
Hi, Anders!
On Sun, Nov 15, 2009 at 7:51 PM, Anders Larsen <al@alarsen.net> wrote:
> On 2009-11-15 21:02:15, Anonymous wrote:
>>
>> On Sun, Nov 15, 2009 at 08:33:29PM +0100, Anders Larsen wrote:
>> > On 2009-11-15 08:15:03, André Goddard Rosa wrote:
>> > >+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
>> 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.
>
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é
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v5 00/12] introduce skip_spaces(), reducing code size plus some clean-ups
@ 2009-11-14 7:11 André Goddard Rosa
2009-11-14 7:11 ` [PATCH v5 10/12] string: factorize skip_spaces and export it to be generally available André Goddard Rosa
0 siblings, 1 reply; 4+ messages in thread
From: André Goddard Rosa @ 2009-11-14 7:11 UTC (permalink / raw)
To: linux list, Andrew Morton; +Cc: André Goddard Rosa
This patch reduces lib/lib.a code size by 482 bytes on my Core 2 with gcc 4.4.1
even considering that it exports a newly defined function skip_spaces()
to drivers:
text data bss dec hex filename
64867 840 592 66299 102fb (TOTALS-lib.a-BEFORE)
64641 584 592 65817 10119 (TOTALS-lib.a-AFTER)
and implements some code tidy up.
Besides reducing lib.a size, it converts many in-tree drivers to use the
newly defined function, which makes another small reduction on kernel size
overall when those drivers are used.
Changelog:
v5: addressed comments and feedback from many people: Jan Engelhardt,
James Bottomley, Rusty Russell, Alan Cox, Henrique de Moraes Holschuh,
Theodore Tso and Julia Lawall (thanks!)
v4: converted drivers over newly defined skip_spaces() function
v3: improved comments on patch 5/7 and factorize a switch statement on 6/7
as per suggestions from Ingo and Frederic (thanks!)
v2: addressed feedback from Frederic Weisbecker review (thanks!)
and split into separate patches
v1: original submission
André Goddard Rosa (12):
vsprintf: factorize "(null)" string
vsprintf: pre-calculate final string length for later use
vsprintf: give it some care to please checkpatch.pl
vsprintf: use TOLOWER whenever possible
vsprintf: reduce code size by avoiding extra check
vsprintf: move local vars to block local vars and remove unneeded
ones
vsprintf: factor out skip_space code in a separate function
vsprintf: reuse almost identical simple_strtoulX() functions
ctype: constify read-only _ctype string
string: factorize skip_spaces and export it to be generally available
string: on strstrip(), first remove leading spaces before running
over str
tree-wide: convert open calls to remove spaces to skip_spaces() lib
function
arch/s390/kernel/debug.c | 3 +-
arch/um/drivers/mconsole_kern.c | 16 +-
arch/x86/kernel/cpu/mtrr/if.c | 11 +-
drivers/leds/led-class.c | 2 +-
drivers/leds/ledtrig-timer.c | 4 +-
drivers/md/dm-table.c | 6 +-
drivers/md/md.c | 4 +-
drivers/parisc/pdc_stable.c | 9 +-
drivers/platform/x86/thinkpad_acpi.c | 7 +-
drivers/pnp/interface.c | 36 +---
drivers/s390/block/dasd_proc.c | 5 +-
drivers/video/backlight/lcd.c | 4 +-
drivers/video/display/display-sysfs.c | 2 +-
drivers/video/output.c | 2 +-
fs/cachefiles/daemon.c | 4 +-
fs/ext4/super.c | 7 +-
include/linux/ctype.h | 3 +-
include/linux/string.h | 1 +
kernel/params.c | 8 +-
lib/argv_split.c | 13 +-
lib/ctype.c | 50 +++---
lib/dynamic_debug.c | 4 +-
lib/string.c | 19 ++-
lib/vsprintf.c | 337 ++++++++++++++++-----------------
net/irda/irnet/irnet.h | 1 +
net/irda/irnet/irnet_ppp.c | 8 +-
net/netfilter/xt_recent.c | 3 +-
sound/pci/hda/hda_hwdep.c | 7 +-
28 files changed, 264 insertions(+), 312 deletions(-)
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v5 10/12] string: factorize skip_spaces and export it to be generally available
2009-11-14 7:11 [PATCH v5 00/12] introduce skip_spaces(), reducing code size plus some clean-ups André Goddard Rosa
@ 2009-11-14 7:11 ` André Goddard Rosa
2009-11-15 19:33 ` Anders Larsen
0 siblings, 1 reply; 4+ messages in thread
From: André Goddard Rosa @ 2009-11-14 7:11 UTC (permalink / raw)
To: linux list, Andrew Morton; +Cc: André Goddard Rosa
On the following sentence:
while (*s && isspace(*s))
s++;
If *s == 0, isspace() evaluates to ((_ctype[*s] & 0x20) != 0), which
evaluates to ((0x08 & 0x20) != 0) which equals to 0 as well.
If *s == 1, we depend on isspace() result anyway. In other words,
"a char equals zero is never a space", so remove this check.
Also, *s != 0 is most common case (non-null string).
Fixed const return as noticed by Jan Engelhardt and James Bottomley.
Fixed unnecessary extra cast on strstrip() as noticed by Jan Engelhardt.
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
---
include/linux/ctype.h | 1 +
include/linux/string.h | 1 +
lib/string.c | 19 +++++++++++++++----
3 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/include/linux/ctype.h b/include/linux/ctype.h
index 6dec944..a3d6ee0 100644
--- a/include/linux/ctype.h
+++ b/include/linux/ctype.h
@@ -27,6 +27,7 @@ extern const unsigned char _ctype[];
#define islower(c) ((__ismask(c)&(_L)) != 0)
#define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
#define ispunct(c) ((__ismask(c)&(_P)) != 0)
+/* Note: isspace() must return false for %NUL-terminator */
#define isspace(c) ((__ismask(c)&(_S)) != 0)
#define isupper(c) ((__ismask(c)&(_U)) != 0)
#define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
diff --git a/include/linux/string.h b/include/linux/string.h
index b850886..168dad1 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -62,6 +62,7 @@ extern char * strnchr(const char *, size_t, int);
#ifndef __HAVE_ARCH_STRRCHR
extern char * strrchr(const char *,int);
#endif
+extern char * __must_check skip_spaces(const char *);
extern char * __must_check strstrip(char *);
#ifndef __HAVE_ARCH_STRSTR
extern char * strstr(const char *,const char *);
diff --git a/lib/string.c b/lib/string.c
index b19b87a..087d33b 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -330,6 +330,20 @@ EXPORT_SYMBOL(strnchr);
#endif
/**
+ * skip_spaces - Removes leading whitespace from @s.
+ * @s: The string to be stripped.
+ *
+ * Returns a pointer to the first non-whitespace character in @s.
+ */
+char *skip_spaces(const char *str)
+{
+ while (isspace(*str))
+ ++str;
+ return (char *)str;
+}
+EXPORT_SYMBOL(skip_spaces);
+
+/**
* strstrip - Removes leading and trailing whitespace from @s.
* @s: The string to be stripped.
*
@@ -352,10 +366,7 @@ char *strstrip(char *s)
end--;
*(end + 1) = '\0';
- while (*s && isspace(*s))
- s++;
-
- return s;
+ return skip_spaces(s);
}
EXPORT_SYMBOL(strstrip);
--
1.6.5.2.180.gc5b3e.dirty
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2009-11-16 12:22 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20091115200215.GA25351@machine>
2009-11-15 21:51 ` [PATCH v5 10/12] string: factorize skip_spaces and export it to be generally available Anders Larsen
2009-11-16 12:21 ` André Goddard Rosa
2009-11-14 7:11 [PATCH v5 00/12] introduce skip_spaces(), reducing code size plus some clean-ups André Goddard Rosa
2009-11-14 7:11 ` [PATCH v5 10/12] string: factorize skip_spaces and export it to be generally available André Goddard Rosa
2009-11-15 19:33 ` Anders Larsen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®