linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* why does sscanf() does not interpret number length attributes?
@ 2003-07-02 20:34 Kay Sievers
  2003-07-02 20:55 ` Richard B. Johnson
  2003-07-02 23:00 ` Stephen Wille Padnos
  0 siblings, 2 replies; 4+ messages in thread
From: Kay Sievers @ 2003-07-02 20:34 UTC (permalink / raw)
  To: linux-kernel

I needed a conversion from hex-string to integer and found
this mail from Linus suggesting sscanf:

http://marc.theaimsgroup.com/?l=linux-kernel&m=101414195507893&w=2

but sscanf in linux-2.5/lib/vsprintf.c interpretes length attributes
only when the type is a string. It uses simple_strtoul() and it will
read the buffer until it finds a non-(hex)digit.

int i;
char str[] ="34AFFE45XYZ";
sscanf(str, "%1x", &i);

i will be '0x34AFFE45' instead of the expected '3'.

Is this behaviour intended or is just nobody caring about?


thanks
Kay


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

* Re: why does sscanf() does not interpret number length attributes?
  2003-07-02 20:34 why does sscanf() does not interpret number length attributes? Kay Sievers
@ 2003-07-02 20:55 ` Richard B. Johnson
  2003-07-02 22:26   ` Kay Sievers
  2003-07-02 23:00 ` Stephen Wille Padnos
  1 sibling, 1 reply; 4+ messages in thread
From: Richard B. Johnson @ 2003-07-02 20:55 UTC (permalink / raw)
  To: Kay Sievers; +Cc: linux-kernel

On Wed, 2 Jul 2003, Kay Sievers wrote:

> I needed a conversion from hex-string to integer and found
> this mail from Linus suggesting sscanf:
>
> http://marc.theaimsgroup.com/?l=linux-kernel&m=101414195507893&w=2
>
> but sscanf in linux-2.5/lib/vsprintf.c interpretes length attributes
> only when the type is a string. It uses simple_strtoul() and it will
> read the buffer until it finds a non-(hex)digit.
>
> int i;
> char str[] ="34AFFE45XYZ";
> sscanf(str, "%1x", &i);
>
> i will be '0x34AFFE45' instead of the expected '3'.
>
> Is this behaviour intended or is just nobody caring about?
>

The in-kernel vsprintf() is very primative, used for very simple
things. Note that it doesn't even have "%f". You should just
do something like:

	i = (int) *str + '0';

      ... if you need to read part of a number.

You don't really wany to increase the size of the permanent
in-kernel stuff if you can help. You add any increased functionality
to your modules so it is used only when needed.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.20 on an i686 machine (797.90 BogoMips).
Why is the government concerned about the lunatic fringe? Think about it.


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

* Re: why does sscanf() does not interpret number length attributes?
  2003-07-02 20:55 ` Richard B. Johnson
@ 2003-07-02 22:26   ` Kay Sievers
  0 siblings, 0 replies; 4+ messages in thread
From: Kay Sievers @ 2003-07-02 22:26 UTC (permalink / raw)
  To: Richard B. Johnson; +Cc: linux-kernel

On Wed, Jul 02, 2003 at 04:55:44PM -0400, Richard B. Johnson wrote:
> On Wed, 2 Jul 2003, Kay Sievers wrote:
> 
> > I needed a conversion from hex-string to integer and found
> > this mail from Linus suggesting sscanf:
> >
> > http://marc.theaimsgroup.com/?l=linux-kernel&m=101414195507893&w=2
> >
> > but sscanf in linux-2.5/lib/vsprintf.c interpretes length attributes
> > only when the type is a string. It uses simple_strtoul() and it will
> > read the buffer until it finds a non-(hex)digit.
> >
> > int i;
> > char str[] ="34AFFE45XYZ";
> > sscanf(str, "%1x", &i);
> >
> > i will be '0x34AFFE45' instead of the expected '3'.
> >
> > Is this behaviour intended or is just nobody caring about?
> >
> 
> The in-kernel vsprintf() is very primative, used for very simple
> things. Note that it doesn't even have "%f". You should just
> do something like:
> 
> 	i = (int) *str + '0';

And what about hex lowercases ?
Do you meant  "- '0'" :-)


> 
>       ... if you need to read part of a number.
> 
> You don't really wany to increase the size of the permanent
> in-kernel stuff if you can help. You add any increased functionality
> to your modules so it is used only when needed.

Sure, but the length attribute is already available in a var at processing
and all what is needed is one extra parameter for escaping from the char read loop.

i wouldn't call this "increase of permanent stuff".

thanks
Kay

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

* Re: why does sscanf() does not interpret number length attributes?
  2003-07-02 20:34 why does sscanf() does not interpret number length attributes? Kay Sievers
  2003-07-02 20:55 ` Richard B. Johnson
@ 2003-07-02 23:00 ` Stephen Wille Padnos
  1 sibling, 0 replies; 4+ messages in thread
From: Stephen Wille Padnos @ 2003-07-02 23:00 UTC (permalink / raw)
  To: Kay Sievers; +Cc: linux-kernel

Kay Sievers wrote:

[snip]

>but sscanf in linux-2.5/lib/vsprintf.c interpretes length attributes
>only when the type is a string. It uses simple_strtoul() and it will
>read the buffer until it finds a non-(hex)digit.
>  
>
[snip]
You could always try truncating the string, then using strtoul().  Save 
the character you replace if you want to restore the string to its 
former glory :)

eg.:
...
savechar = str[3];
str[3]=0;
i = simple_strtoul(str);
str[3]=savechar;
...
- Steve



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

end of thread, other threads:[~2003-07-02 22:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-02 20:34 why does sscanf() does not interpret number length attributes? Kay Sievers
2003-07-02 20:55 ` Richard B. Johnson
2003-07-02 22:26   ` Kay Sievers
2003-07-02 23:00 ` Stephen Wille Padnos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).