From nobody  Wed Jan 15 07:38:35 1997
Received: (from nobody@localhost)
          by freefall.freebsd.org (8.8.4/8.8.4) id HAA07065;
          Wed, 15 Jan 1997 07:38:35 -0800 (PST)
Message-Id: <199701151538.HAA07065@freefall.freebsd.org>
Date: Wed, 15 Jan 1997 07:38:35 -0800 (PST)
From: scrutchfield@ifusion.com
To: freebsd-gnats-submit@freebsd.org
Subject: Unable to sscanf first integer value.
X-Send-Pr-Version: www-1.0

>Number:         2502
>Category:       bin
>Synopsis:       Unable to sscanf first integer value.
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jan 15 07:40:01 PST 1997
>Closed-Date:    Fri Jan 17 23:49:06 MET 1997
>Last-Modified:  Fri Jan 17 23:49:31 MET 1997
>Originator:     Steven M. Crutchfield
>Release:        2.2-BETA
>Organization:
IFusionCom
>Environment:
FreeBSD no_dobbs.ifusion.com 2.2-BETA_A FreeBSD 2.2-BETA_A #0: Tue Jan  7 16:05:22 EST 1997     root@no_dobbs.ifusion.com:/usr/src/sys/compile/PS2MOUSE  i386


>Description:
I am unable to sscanf correctly 2 integers from a string.  A Sample
program that recreates the problem is shown below.  This is a problem
in both libc and libc_r.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

main()
{
	char *tmp = "999 12346";
	char *ptr;
	unsigned short x;
	unsigned short y;
	unsigned short z;
	unsigned int a;
	int result;

	result = sscanf ( tmp, "%d %d", &x, &y );
	z = strtol ( tmp, &ptr, 0 );
	a = atoi ( tmp );
	(void)fprintf ( stderr, "x(%d)y(%d)z(%d)a(%d)\n", x, y, z, a );
	exit ( 0 );
}

>How-To-Repeat:
Run the above program.
>Fix:

>Release-Note:
>Audit-Trail:

From: "John S. Dyson" <toor@dyson.iquest.net>
To: scrutchfield@ifusion.com
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: bin/2502: Unable to sscanf first integer value.
Date: Wed, 15 Jan 1997 11:21:26 -0500 (EST)

 > I am unable to sscanf correctly 2 integers from a string.  A Sample
 > program that recreates the problem is shown below.  This is a problem
 > in both libc and libc_r.
 > 
 > #include <stdio.h>
 > #include <string.h>
 > #include <stdlib.h>
 > 
 > main()
 > {
 > 	char *tmp = "999 12346";
 > 	char *ptr;
 > 	unsigned short x;
 > 	unsigned short y;
 > 	unsigned short z;
 > 	unsigned int a;
 > 	int result;
 > 
 > 	result = sscanf ( tmp, "%d %d", &x, &y );
 > 	z = strtol ( tmp, &ptr, 0 );
 > 	a = atoi ( tmp );
 > 	(void)fprintf ( stderr, "x(%d)y(%d)z(%d)a(%d)\n", x, y, z, a );
 > 	exit ( 0 );
 > }
 > 
 > >How-To-Repeat:
 > Run the above program.
 > >Fix:
 > 
 The problem that you are seeing is due to passing a pointer to
 a "short" instead of a pointer to an "int."  If you change the
 declarations of (x,y) to be "unsigned int", then things will work
 well.
 
 John

From: davidn@unique.usn.blaze.net.au (David Nugent)
To: scrutchfield@ifusion.com
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/2502: Unable to sscanf first integer value.
Date: Thu, 16 Jan 1997 03:23:01 +1100

 scrutchfield@ifusion.com writes:
 > I am unable to sscanf correctly 2 integers from a string.  A Sample
 > program that recreates the problem is shown below.  This is a problem
 > in both libc and libc_r.
 
 No, it is a problem with your code, not libc.
 
 > #include <stdio.h>
 > #include <string.h>
 > #include <stdlib.h>
 > 
 > main()
 > {
 > 	char *tmp = "999 12346";
 > 	char *ptr;
 > 	unsigned short x;
 > 	unsigned short y;
 > 	unsigned short z;
 > 	unsigned int a;
 > 	int result;
 > 
 > 	result = sscanf ( tmp, "%d %d", &x, &y );
 
 You're scanning integers, and this is what sscanf expects.
 You're passing it pointers to 16-bit, not 32-bit, quantities.
 'x' is "zeroed" when the value 12346 (32-bit) is written to y,
 as the following fprintf() shows.
 
 > 	z = strtol ( tmp, &ptr, 0 );
 > 	a = atoi ( tmp );
 
 And I assume this is supposed to be 'ptr', not 'tmp'.
 
 > 	(void)fprintf ( stderr, "x(%d)y(%d)z(%d)a(%d)\n", x, y, z, a );
 > 	exit ( 0 );
 > }
 
 The following is a "fixed" version of your code which simply
 changes the integral variables to the 32-bit quantities that
 sscanf expects, which works fine.
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 
 main()
 {
 	char *tmp = "999 12346";
 	char *ptr;
 	int x;
 	int y;
 	int z;
 	int a;
 	int result;
 
 	result = sscanf ( tmp, "%d %d", &x, &y );
 	z = strtol ( tmp, &ptr, 0 );
 	a = atoi ( ptr );
 	(void)fprintf ( stderr, "x(%d)y(%d)z(%d)a(%d)\n", x, y, z, a );
 	exit ( 0 );
 }
 
 
 
 Regards,
 
 David Nugent - Unique Computing Pty Ltd - Melbourne, Australia
 Voice +61-3-9791-9547  Data/BBS +61-3-9792-3507  3:632/348@fidonet
 davidn@freebsd.org davidn@blaze.net.au http://www.blaze.net.au/~davidn/

From: John-Mark Gurney <jmg@nike.efn.org>
To: scrutchfield@ifusion.com
Cc: freebsd-gnats-submit@freebsd.org,
        GNATS Management <gnats@freefall.freebsd.org>,
        freebsd-bugs@freefall.freebsd.org
Subject: Re: bin/2502: Unable to sscanf first integer value.
Date: Wed, 15 Jan 1997 12:10:45 -0800 (PST)

 On Wed, 15 Jan 1997 scrutchfield@ifusion.com wrote:
 
 > >Synopsis:       Unable to sscanf first integer value.
 
 [...]
 
 > >Description:
 > I am unable to sscanf correctly 2 integers from a string.  A Sample
 > program that recreates the problem is shown below.  This is a problem
 > in both libc and libc_r.
 > 
 > #include <stdio.h>
 > #include <string.h>
 > #include <stdlib.h>
 > 
 > main()
 > {
 > 	char *tmp = "999 12346";
 > 	char *ptr;
 > 	unsigned short x;
 > 	unsigned short y;
 > 	unsigned short z;
 
 you need these to be int's, not short... if you want a short use %hd
 instead..
 
 > 	unsigned int a;
 > 	int result;
 > 
 > 	result = sscanf ( tmp, "%d %d", &x, &y );
 > 	z = strtol ( tmp, &ptr, 0 );
 > 	a = atoi ( tmp );
 > 	(void)fprintf ( stderr, "x(%d)y(%d)z(%d)a(%d)\n", x, y, z, a );
 > 	exit ( 0 );
 > }
 > 
 > >How-To-Repeat:
 > Run the above program.
 > >Fix:
 
 use scanf like it was designed to...  if you need help man 3 scanf should
 contain the missing info...  hope this helps... ttyl..
 
 John-Mark
 
 gurney_j@efn.org
 http://resnet.uoregon.edu/~gurney_j/
 Modem/FAX: (541) 683-6954   (FreeBSD Box)
 
 Live in Peace, destroy Micro$oft, support free software, run FreeBSD (unix)
 
State-Changed-From-To: open->closed 
State-Changed-By: joerg 
State-Changed-When: Fri Jan 17 23:49:06 MET 1997 
State-Changed-Why:  
Usage error. 

>Unformatted:
