Bug 58249

Summary: sscanf() does not convert correctly.
Product: [Retired] Red Hat Linux Reporter: Jules Colding <jules>
Component: glibcAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: Brian Brock <bbrock>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.1CC: fweimer
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2002-01-11 22:51:24 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

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).