Bug 58249 - sscanf() does not convert correctly.
Summary: sscanf() does not convert correctly.
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: glibc
Version: 7.1
Hardware: i686
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Brian Brock
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2002-01-11 22:51 UTC by Jules Colding
Modified: 2016-11-24 15:00 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2002-01-11 22:51:24 UTC


Attachments (Terms of Use)

Description Jules Colding 2002-01-11 22:51:19 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.5) Gecko/20011013

Description of problem:
The following program does not work:

#include <stdio.h>

main ()
{
	long s;
	unsigned long us;

	sscanf("0xF0F0F0F0", "%lu", &us);
	if (us != 0xf0f0f0f0)
		printf("fail unsigned: expect 0xf0f0f0f0 got 0x%lx\n", us);

	sscanf("0xF0F0F0F0", "%li", &s);
	if (s != 0xf0f0f0f0)
		printf("fail signed: expect 0xf0f0f0f0 got 0x%lx\n", s);
}



Version-Release number of selected component (if applicable):


How reproducible:
Always

Steps to Reproduce:
1. Compile the sample code and run it.
	

Actual Results:  The output from the sample code is:

fail unsigned: expect 0xf0f0f0f0 got 0x0
fail signed: expect 0xf0f0f0f0 got 0x7fffffff

Expected Results:  The conversion should have worked.

Additional info:

I am using glibc-2.2.4-19.3

Comment 1 Jakub Jelinek 2002-01-14 11:48:32 UTC
glibc is right, please reread info libc on formated input.
%u conversion matches unsigned integer in DECIMAL radix (so it matches 0 but
not the rest) - this is strtoul (, , 10) does.
%i conversion matches optionally signed integer in decimal/octal/hex radix,
is what strtol (, , 0) does. But in this second case the problem is that you're
trying to put an value which doesn't fit into 32-bit signed integer
(errno is set to ERANGE after second sscanf).


Note You need to log in before you can comment on or make changes to this bug.