[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <1258321890l.9645l.1l@oscar.alarsen.net>
Date: Sun, 15 Nov 2009 22:51:30 +0100
From: Anders Larsen <al@...rsen.net>
To: Anonymous <aim0shei@...abit.com>
Cc: André Goddard Rosa <andre.goddard@...il.com>,
Andrew Morton <akpm@...ux-foundation.org>,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v5 10/12] string: factorize skip_spaces and export it to
be generally available
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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists