sscanf("-1", "%d", &i) fails, returns 0
Quoted 2 lines
Quoted 1 line
What should it do?
I would model this after user space. (Which does strange things:
<snip>
It only sounds strange at first. It actually means that scanf is consistent with C's rules of assignment between mixed types. For example:
ray:~$ cat signs.c
#include <stdio.h>
main() {
char scan[]="-100";
unsigned int u;
int i;
sscanf(scan, "%ud", &u);
sscanf(scan, "%d", &i);
printf("%s scanned to signed %d and unsigned %u\n", scan, i, u);
i=-100;
u=i;
printf("%d assigned to unsigned int gives %u\n", i, u);
}ray:~$ ./signs -100 scanned to signed -100 and unsigned 4294967196 -100 assigned to unsigned int gives 4294967196
So, one should think of scanf as having correct knowledge of the types it's scanning, and then shoe-horning the result into whatever you asked for. Just like C itself.
Ray
On 11 Nov 2002, Ray Lee wrote:
Randy wrote:
| > > What should it do?
Henning wrote:
| > I would model this after user space. (Which does strange things: | | <snip> | | It only sounds strange at first. It actually means that scanf is | consistent with C's rules of assignment between mixed types. For | example: | | ray:~$ cat signs.c | [snippage] | | ray:~$ ./signs | -100 scanned to signed -100 and unsigned 4294967196 | -100 assigned to unsigned int gives 4294967196
These values (-100 and 4294967196) are the same bits, just observed/used in a different manner.
| So, one should think of scanf as having correct knowledge of the types | it's scanning, and then shoe-horning the result into whatever you asked | for. Just like C itself.
Why would scanf() have to know whether the destination is signed or unsigned? It needs to know type (=> size) of course.
Seems to me that "signed-ness" is in the eyes of the user of the value. I.e., scanf() can scan "-100" to an <int value> of
-100 = (unsigned) 4294967196 and shoe-horn that value into an int-sized shoe and the caller can use it as signed or unsigned.
Andreas Schwab wrote:
|> IOW, asking for an unsigned number (in the format string) |> and getting "-123" does return 0.
| Not in C. According to the standard scanf is supposed to convert the | value to unsinged and return that.
OK, thanks, I found that in the C spec.
Now what does it mean to "convert the value to an unsigned and return that." This is the same as above, isn't it?
I.e., on the scanf() side, there is no conversion needed; just store the value.
So... somebody tell me if the following patch makes the kernel's vsscanf() function act like C's sscanf() function, or why it doesn't, or what I'm overlooking. :)
And then we'll see if the change is worthwhile... or for which kernel version.
Signature · 77 lines
~Randy
--- ./lib/vsprintf.c%scan Sun Nov 10 19:28:30 2002
+++ ./lib/vsprintf.c Mon Nov 11 09:56:51 2002
@@ -640,7 +640,7 @@
str++;
digit = *str;
- if (is_sign && digit == '-')
+ if (digit == '-')
digit = *(str + 1);
if (!digit
@@ -652,45 +652,33 @@
switch(qualifier) {
case 'h':
- if (is_sign) {
- short *s = (short *) va_arg(args,short *);
- *s = (short) simple_strtol(str,&next,base);
- } else {
- unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
- *s = (unsigned short) simple_strtoul(str, &next, base);
+ {
+ short *s = (short *) va_arg(args,short *);
+ *s = (short) simple_strtol(str,&next,base);
}
break;
case 'l':
- if (is_sign) {
- long *l = (long *) va_arg(args,long *);
- *l = simple_strtol(str,&next,base);
- } else {
- unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
- *l = simple_strtoul(str,&next,base);
+ {
+ long *l = (long *) va_arg(args,long *);
+ *l = simple_strtol(str,&next,base);
}
break;
case 'L':
- if (is_sign) {
- long long *l = (long long*) va_arg(args,long long *);
- *l = simple_strtoll(str,&next,base);
- } else {
- unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
- *l = simple_strtoull(str,&next,base);
+ {
+ long long *l = (long long*) va_arg(args,long long *);
+ *l = simple_strtoll(str,&next,base);
}
break;
case 'Z':
- {
+ {
size_t *s = (size_t*) va_arg(args,size_t*);
*s = (size_t) simple_strtoul(str,&next,base);
- }
- break;
+ }
+ break;
default:
- if (is_sign) {
- int *i = (int *) va_arg(args, int*);
- *i = (int) simple_strtol(str,&next,base);
- } else {
- unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
- *i = (unsigned int) simple_strtoul(str,&next,base);
+ {
+ int *i = (int *) va_arg(args, int*);
+ *i = (int) simple_strtol(str,&next,base);
}
break;
}"Randy.Dunlap" <[email protected]> writes:
|> Andreas Schwab wrote: |> |> IOW, asking for an unsigned number (in the format string) |> |> and getting "-123" does return 0. |> |> | Not in C. According to the standard scanf is supposed to convert the |> | value to unsinged and return that. |> OK, thanks, I found that in the C spec. |> |> Now what does it mean to "convert the value to an unsigned and return |> that." This is the same as above, isn't it? |> I.e., on the scanf() side, there is no conversion needed; just store the |> value.
The C standard also supports ones-complement and sign-magnitude representation of signed integers where signed<->unsigned conversion is a non-trivial operation in the sense that the bit representation does change. And scanf knows the signedness of the destination due to the format spec.
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Quoted 3 lines · Randy.Dunlap
| It only sounds strange at first. It actually means that scanf is | consistent with C's rules of assignment between mixed types. For | example:
<snip>
Quoted 2 lines
These values (-100 and 4294967196) are the same bits, just observed/used in a different manner.
I probably didn't state it very clearly, but that was what I was getting at. It's the same value after a cast. As Andreas points out though, it may not be bit-for-bit identical on machines that don't use two's complement. That's why the conversion code cares about the format specifiers, it seems.
Quoted 2 lines
Now what does it mean to "convert the value to an unsigned and return that." This is the same as above, isn't it?
Explicitly, in the scan conversion you'd do a:
unsigned int *u = (unsigned int *) va_arg(args,long long *); *u = (unsigned int) converted_value;
...from wherever you get converted_value from (simple_strtoul? I haven't looked at that routine). (The cast here is implied if left off, I'm just being explicit.)
Quoted 2 lines
I.e., on the
scanf()side, there is no conversion needed; just store the value.
Almost true, as Andreas pointed out. You have to be careful that the target you're storing to has the correctly declared type inside the conversion routine.
Ray
On 11 Nov 2002, Ray Lee wrote:
| I probably didn't state it very clearly, but that was what I was getting | at. It's the same value after a cast. As Andreas points out though, it | may not be bit-for-bit identical on machines that don't use two's | complement. That's why the conversion code cares about the format | specifiers, it seems. | | > Now what does it mean to "convert the value to an unsigned and return | > that." This is the same as above, isn't it? | | Explicitly, in the scan conversion you'd do a: | | unsigned int *u = (unsigned int *) va_arg(args,long long *); | *u = (unsigned int) converted_value; | | ...from wherever you get converted_value from (simple_strtoul? I haven't | looked at that routine). (The cast here is implied if left off, I'm just | being explicit.) | | > I.e., on the scanf() side, there is no conversion needed; just store the | > value. | | Almost true, as Andreas pointed out. You have to be careful that the | target you're storing to has the correctly declared type inside the | conversion routine.
See if this is close...
For 'h' (short), 'l' (long), 'L' (long long), and default ('d' and 'i'), signed input is allowed, even for octal or hex (as well as decimal), so scan/convert signed values. Then if the destination type is not signed (!is_sign), store the value in an unsigned <length> var so that conversion can be done as needed.
Here are a few sample conversions:
scanning input string:{-42}:
%o level = 037777777736 = 0xffffffde
%x level = -66 = 0xffffffbe
%i level = -42 = 0xffffffd6
scanning input string:{-100}:
%o level = 037777777700 = 0xffffffc0
%x level = -256 = 0xffffff00
%i level = -100 = 0xffffff9c
scanning input string:{-10}:
%o level = 037777777770 = 0xfffffff8
%x level = -16 = 0xfffffff0
%i level = -10 = 0xfffffff6
I think that this patch (to 2.5.47) gets the kernel close to the same semantics as C's sscanf() function, which is usually a good thing. What say you?
Signature · 95 lines
~Randy
"I read part of it all the way through." -- Samuel Goldwyn
--- ./lib/vsprintf.c%scan Sun Nov 10 19:28:30 2002
+++ ./lib/vsprintf.c Tue Nov 12 20:51:23 2002
@@ -640,7 +640,7 @@
str++;
digit = *str;
- if (is_sign && digit == '-')
+ if (digit == '-')
digit = *(str + 1);
if (!digit
@@ -652,46 +652,50 @@
switch(qualifier) {
case 'h':
- if (is_sign) {
- short *s = (short *) va_arg(args,short *);
- *s = (short) simple_strtol(str,&next,base);
- } else {
- unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
- *s = (unsigned short) simple_strtoul(str, &next, base);
+ {
+ short *s = (short *) va_arg(args, short *);
+ *s = (short) simple_strtol(str, &next, base);
+ if (!is_sign) {
+ unsigned short *us = s;
+ *us = (unsigned short) *s;
}
+ }
break;
case 'l':
- if (is_sign) {
- long *l = (long *) va_arg(args,long *);
- *l = simple_strtol(str,&next,base);
- } else {
- unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
- *l = simple_strtoul(str,&next,base);
+ {
+ long *l = (long *) va_arg(args, long *);
+ *l = simple_strtol(str, &next, base);
+ if (!is_sign) {
+ unsigned long *ul = l;
+ *ul = (unsigned long) *l;
}
+ }
break;
case 'L':
- if (is_sign) {
- long long *l = (long long*) va_arg(args,long long *);
- *l = simple_strtoll(str,&next,base);
- } else {
- unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
- *l = simple_strtoull(str,&next,base);
+ {
+ long long *l = (long long *) va_arg(args, long long *);
+ *l = simple_strtoll(str, &next, base);
+ if (!is_sign) {
+ unsigned long long *ul = l;
+ *ul = (unsigned long long) *l;
}
+ }
break;
case 'Z':
{
- size_t *s = (size_t*) va_arg(args,size_t*);
- *s = (size_t) simple_strtoul(str,&next,base);
+ size_t *s = (size_t *) va_arg(args, size_t *);
+ *s = (size_t) simple_strtoul(str, &next, base);
}
- break;
+ break;
default:
- if (is_sign) {
- int *i = (int *) va_arg(args, int*);
- *i = (int) simple_strtol(str,&next,base);
- } else {
- unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
- *i = (unsigned int) simple_strtoul(str,&next,base);
+ {
+ int *i = (int *) va_arg(args, int *);
+ *i = (int) simple_strtol(str, &next, base);
+ if (!is_sign) {
+ unsigned int *ui = i;
+ *ui = (unsigned int) *i;
}
+ }
break;
}
num++;(Sorry for taking so long to review something so short, btw.)
Quoted 4 lines · Randy.Dunlap
On 11 Nov 2002, Ray Lee wrote:
| Explicitly, in the scan conversion you'd do a: | unsigned int *u = (unsigned int *) va_arg(args,long long *); | *u = (unsigned int) converted_value;
(Luckily you didn't follow my code snippet too closely. Oops.)
Quoted 1 line
See if this is close...
<snip>
Quoted 3 lines
I think that this patch (to 2.5.47) gets the kernel close to the same semantics as C's
sscanf()function, which is usually a good thing. What say you?
The sample conversions and patch look correct. Time to forward it onward, me thinks.
Ray