M
myx
Hi. I've noticed interesting thing in scanf(). When overflow happens,
it behaves differently depending on what type specifier do you use.
Here is an example:
-------
long a;
scanf("%lx", &a);
printf("%lx", a);
-------
input:
ffffffff0 /* overflow */
output:
ffffffff /* result contain ULONG_MAX */
In second case, I'm trying to overflow short int:
-------
short a;
scanf("%hx", &a);
printf("%hx", a);
-------
input:
ffff0 /* overflow */
output:
fff0 /* result contain ffff0 % (USHRT_MAX+1) */
I think I know why it behaves so. But I think that it is not very good
behaviour, because scanf must return or ULONG_MAX, or num % (ULONG_MAX
+1) (in this particular case).
Am I wrong?
it behaves differently depending on what type specifier do you use.
Here is an example:
-------
long a;
scanf("%lx", &a);
printf("%lx", a);
-------
input:
ffffffff0 /* overflow */
output:
ffffffff /* result contain ULONG_MAX */
In second case, I'm trying to overflow short int:
-------
short a;
scanf("%hx", &a);
printf("%hx", a);
-------
input:
ffff0 /* overflow */
output:
fff0 /* result contain ffff0 % (USHRT_MAX+1) */
I think I know why it behaves so. But I think that it is not very good
behaviour, because scanf must return or ULONG_MAX, or num % (ULONG_MAX
+1) (in this particular case).
Am I wrong?