All of lore.kernel.org
 help / color / mirror / Atom feed
* memparse(), simple_strtoul() prototypes...
@ 2007-02-18 16:04 Francis Moreau
  2007-02-19  0:30 ` H. Peter Anvin
  0 siblings, 1 reply; 5+ messages in thread
From: Francis Moreau @ 2007-02-18 16:04 UTC (permalink / raw)
  To: linux-kernel

Hi,

I must miss something...

Looking at these prototypes

unsigned long simple_strtoul(const char *cp, char **endp,unsigned int base)
unsigned long long memparse (char *ptr, char **retptr)

I'm really wondering why not all parameters are not all 'const'. None
of these functions modify any pointer containts. And simple_strtoul()
ends up doing sometghing like:

if (endp)
		*endp = (char *)cp;

Could anyone shed some light ?
-- 
Francis

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: memparse(), simple_strtoul() prototypes...
  2007-02-18 16:04 memparse(), simple_strtoul() prototypes Francis Moreau
@ 2007-02-19  0:30 ` H. Peter Anvin
  2007-02-19 13:03   ` Francis Moreau
  0 siblings, 1 reply; 5+ messages in thread
From: H. Peter Anvin @ 2007-02-19  0:30 UTC (permalink / raw)
  To: Francis Moreau; +Cc: linux-kernel

Francis Moreau wrote:
> Hi,
> 
> I must miss something...
> 
> Looking at these prototypes
> 
> unsigned long simple_strtoul(const char *cp, char **endp,unsigned int base)
> unsigned long long memparse (char *ptr, char **retptr)
> 
> I'm really wondering why not all parameters are not all 'const'. None
> of these functions modify any pointer containts. And simple_strtoul()
> ends up doing sometghing like:
> 
> if (endp)
>         *endp = (char *)cp;
> 
> Could anyone shed some light ?

The C standard behaves like that, too, mostly because C doesn't have a 
way to say "X is const iff Y is const" (unlike C++, btw.)

	-hpa

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: memparse(), simple_strtoul() prototypes...
  2007-02-19  0:30 ` H. Peter Anvin
@ 2007-02-19 13:03   ` Francis Moreau
  2007-02-19 14:05     ` Avi Kivity
  0 siblings, 1 reply; 5+ messages in thread
From: Francis Moreau @ 2007-02-19 13:03 UTC (permalink / raw)
  To: H. Peter Anvin; +Cc: linux-kernel

Hi,

On 2/19/07, H. Peter Anvin <hpa@zytor.com> wrote:
> Francis Moreau wrote:
> > Hi,
> >
> > I must miss something...
> >
> > Looking at these prototypes
> >
> > unsigned long simple_strtoul(const char *cp, char **endp,unsigned int base)
> > unsigned long long memparse (char *ptr, char **retptr)
> >
> > I'm really wondering why not all parameters are not all 'const'. None
> > of these functions modify any pointer containts. And simple_strtoul()
> > ends up doing sometghing like:
> >
> > if (endp)
> >         *endp = (char *)cp;
> >
> > Could anyone shed some light ?
>
> The C standard behaves like that, too, mostly because C doesn't have a
> way to say "X is const iff Y is const" (unlike C++, btw.)
>

hm, I don't get your point. I understand why we cast 'cp' into a (char
*) but that's not my point. My point is why aren't all function
parameters are not const ?

-- 
Francis

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: memparse(), simple_strtoul() prototypes...
  2007-02-19 13:03   ` Francis Moreau
@ 2007-02-19 14:05     ` Avi Kivity
  2007-02-20  8:19       ` Francis Moreau
  0 siblings, 1 reply; 5+ messages in thread
From: Avi Kivity @ 2007-02-19 14:05 UTC (permalink / raw)
  To: Francis Moreau; +Cc: H. Peter Anvin, linux-kernel

Francis Moreau wrote:
>> > unsigned long simple_strtoul(const char *cp, char **endp,unsigned 
>> int base)
>
> hm, I don't get your point. I understand why we cast 'cp' into a (char
> *) but that's not my point. My point is why aren't all function
> parameters are not const ?
>

'cp' can be passed as const, because simple_strtoul() does not modify 
it. 'endp' cannot be passed as const, because simple_strtoul() cannot 
know whether the caller would want to modify the string or not.

Whichever way it is written, it is broken.  If changed to 'const', it 
would preclude the caller from modifying the string if one has a 
non-const string.  As written, it can silently convert a const string to 
a non-const string.  However, as written it is (a) standard conforming, 
and (b) more useful.

-- 
error compiling committee.c: too many arguments to function


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: memparse(), simple_strtoul() prototypes...
  2007-02-19 14:05     ` Avi Kivity
@ 2007-02-20  8:19       ` Francis Moreau
  0 siblings, 0 replies; 5+ messages in thread
From: Francis Moreau @ 2007-02-20  8:19 UTC (permalink / raw)
  To: Avi Kivity; +Cc: H. Peter Anvin, linux-kernel

On 2/19/07, Avi Kivity <avi@argo.co.il> wrote:
> Francis Moreau wrote:
> >> > unsigned long simple_strtoul(const char *cp, char **endp,unsigned
> >> int base)
> >
> > hm, I don't get your point. I understand why we cast 'cp' into a (char
> > *) but that's not my point. My point is why aren't all function
> > parameters are not const ?
> >
>
> 'cp' can be passed as const, because simple_strtoul() does not modify
> it. 'endp' cannot be passed as const, because simple_strtoul() cannot
> know whether the caller would want to modify the string or not.
>
> Whichever way it is written, it is broken.  If changed to 'const', it
> would preclude the caller from modifying the string if one has a
> non-const string.  As written, it can silently convert a const string to
> a non-const string.  However, as written it is (a) standard conforming,
> and (b) more useful.
>

ok I think I finally got it and I agree that both ways are broken.

Maybe changing simple_strtoul() prototype as follow would be better ?

int simple_strtoul(const char *cp,  unsigned long *value, unsigned base)

the function would return the number of parsed char, and 'value' would
be the result.
-- 
Francis

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-02-20  8:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-18 16:04 memparse(), simple_strtoul() prototypes Francis Moreau
2007-02-19  0:30 ` H. Peter Anvin
2007-02-19 13:03   ` Francis Moreau
2007-02-19 14:05     ` Avi Kivity
2007-02-20  8:19       ` Francis Moreau

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.