Skip to contentSkip to conversation

kobject sample code

DISCUSSION John de la Garza 3 participants 5 messages Dec 9, 2014 – Dec 17, 2014

lore
Share & tools
lore
#0 lore raw

I'm looking at samples/kobject/kobject-example.c

at line 39 foo is read:

        sscanf(buf, "%du", &foo);

foo is an int so why is it read using %du not %d?

#1 lore raw
Quoted 7 lines · John de la Garza

I'm looking at samples/kobject/kobject-example.c

at line 39 foo is read:

        sscanf(buf, "%du", &foo);

foo is an int so why is it read using %du not %d?

My reading of lib/vsprintf.c:vsscanf() is that a "%du" format expects "buf" to contain an integer followed by a literal 'u' char. Is that your reading too?

Paul Bolle

#2 lore raw
Quoted 5 lines · Paul Bolle
Quoted 4 lines · John de la Garza

I'm looking at samples/kobject/kobject-example.c

at line 39 foo is read:

Quoted 8 lines
Quoted 3 lines
        sscanf(buf, "%du", &foo);

foo is an int so why is it read using %du not %d?

My reading of lib/vsprintf.c:vsscanf() is that a "%du" format expects "buf" to contain an integer followed by a literal 'u' char. Is that your reading too?

After reading the code and the man page for vsscanf, it seems like it is not expecting the 'u', but rather ignores it.

vsscanf is a bit complicated for me, so I may be missing something.

#3 lore raw
Quoted 8 lines · John de la Garza
Quoted 7 lines · Paul Bolle
Quoted 6 lines · John de la Garza

I'm looking at samples/kobject/kobject-example.c

at line 39 foo is read:

        sscanf(buf, "%du", &foo);
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

foo is an int so why is it read using %du not %d?

My reading of lib/vsprintf.c:vsscanf() is that a "%du" format expects "buf" to contain an integer followed by a literal 'u' char. Is that your reading too?

After reading the code and the man page for vsscanf, it seems like it is not expecting the 'u', but rather ignores it.

The manpage _probably_ describes intended behavior. But I'm not sure whether the kernel's vsscanf() tries to emulate it all that faithfully. (And, besides, that manpage would gain quite a bit with a few examples.) Anyhow, the net effect of "%du" appears to be to ignore any non digit trailing input. So I think you're reading the code correctly.

Quoted 1 line

vsscanf is a bit complicated for me, so I may be missing something.

I wouldn't have bothered replying if I had not noticed
https://lkml.org/lkml/2014/12/17/386 . You might like to have a look at that patch. My primary observation is that its commit explanation is not very helpful. (That's why I CC-ed Ratislav.)

Paul Bolle

#4 lore raw
Quoted 25 lines · On Wed, 17 Dec 2014 22:31:21 +0100 Paul Bolle <[email protected]> wrote:
Quoted 8 lines · John de la Garza
Quoted 7 lines · Paul Bolle
Quoted 6 lines · John de la Garza

I'm looking at samples/kobject/kobject-example.c

at line 39 foo is read:

        sscanf(buf, "%du", &foo);
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

foo is an int so why is it read using %du not %d?

My reading of lib/vsprintf.c:vsscanf() is that a "%du" format expects "buf" to contain an integer followed by a literal 'u' char. Is that your reading too?

After reading the code and the man page for vsscanf, it seems like it is not expecting the 'u', but rather ignores it.

The manpage _probably_ describes intended behavior. But I'm not sure whether the kernel's vsscanf() tries to emulate it all that faithfully. (And, besides, that manpage would gain quite a bit with a few examples.) Anyhow, the net effect of "%du" appears to be to ignore any non digit trailing input. So I think you're reading the code correctly.

Quoted 8 lines
Quoted 1 line

vsscanf is a bit complicated for me, so I may be missing something.

I wouldn't have bothered replying if I had not noticed
https://lkml.org/lkml/2014/12/17/386 . You might like to have a look at that patch. My primary observation is that its commit explanation is not very helpful. (That's why I CC-ed Ratislav.)

Paul Bolle

My understanding is that sscanf() would ignore any trailing input anyway. So I think that "%du" is just a typo and should have been "%d" instead. There should also be check for return value from sscanf(). I send a patch which uses kstrtoint() as it is preferred to single variable sscanf().

Signature · 2 lines
Kind regards
Rastislav Barlik