From gkeramidas@bytemobile.com  Mon Oct 25 12:04:48 2004
Return-Path: <gkeramidas@bytemobile.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id A574A16A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 25 Oct 2004 12:04:48 +0000 (GMT)
Received: from kane.otenet.gr (kane.otenet.gr [195.170.0.27])
	by mx1.FreeBSD.org (Postfix) with ESMTP id E821243D69
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 25 Oct 2004 12:04:47 +0000 (GMT)
	(envelope-from keramida@bytemobile.com)
Received: from orion.daedalusnetworks.priv (aris.bedc.ondsl.gr [62.103.39.226])
	by kane.otenet.gr (8.13.1/8.13.1/Debian-15.OTEnet.1) with SMTP id i9PC4iUP015459
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 25 Oct 2004 15:04:45 +0300
Received: from orion.daedalusnetworks.priv (orion [127.0.0.1])
	by orion.daedalusnetworks.priv (8.13.1/8.13.1) with ESMTP id i9PC4iGG003337
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 25 Oct 2004 15:04:44 +0300 (EEST)
	(envelope-from keramida@orion.daedalusnetworks.priv)
Received: (from keramida@localhost)
	by orion.daedalusnetworks.priv (8.13.1/8.13.1/Submit) id i9PC4iKl003336;
	Mon, 25 Oct 2004 15:04:44 +0300 (EEST)
	(envelope-from keramida)
Message-Id: <200410251204.i9PC4iKl003336@orion.daedalusnetworks.priv>
Date: Mon, 25 Oct 2004 15:04:44 +0300 (EEST)
From: Giorgos Keramidas <keramida@freebsd.org>
Reply-To: Giorgos Keramidas <gkeramidas@bytemobile.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: change atol() to strtol() in badsect
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         73112
>Category:       bin
>Synopsis:       [patch] change atol() to strtol() in badsect
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    rwatson
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Oct 25 12:10:21 GMT 2004
>Closed-Date:    Sun Jan 13 16:14:36 UTC 2008
>Last-Modified:  Sun Jan 13 16:14:36 UTC 2008
>Originator:     Giorgos Keramidas
>Release:        FreeBSD 6.0-CURRENT i386
>Organization:
>Environment:

System: FreeBSD orion.daedalusnetworks.priv 6.0-CURRENT FreeBSD 6.0-CURRENT #4: \
Fri Oct 22 17:12:12 EEST 2004 root@orion.daedalusnetworks.priv:/usr/obj/usr/src/sys/ORION i386

>Description:

The badsect(8) utility uses atol(), which doesn't allow very good error
checking and only recognizes numbers in base 10.  The attached patch
checks errno after strtol() and uses a base of 0 to allow octal, or hex
sector numbers too.

>How-To-Repeat:
>Fix:

--- badsect.patch begins here ---
Index: badsect.c
===================================================================
RCS file: /home/ncvs/src/sbin/badsect/badsect.c,v
retrieving revision 1.20
diff -u -u -r1.20 badsect.c
--- badsect.c	9 Apr 2004 19:58:25 -0000	1.20
+++ badsect.c	25 Oct 2004 12:00:37 -0000
@@ -59,6 +59,7 @@
 #include <ufs/ffs/fs.h>
 
 #include <err.h>
+#include <errno.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <libufs.h>
@@ -123,7 +124,9 @@
 			err(7, "%s", name);
 	}
 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
-		number = atol(*argv);
+		number = strtol(*argv, NULL, 0);
+		if (errno == EINVAL || errno == ERANGE)
+			err(8, "%s", *argv);
 		if (chkuse(number, 1))
 			continue;
 		/*
--- badsect.patch ends here ---
>Release-Note:
>Audit-Trail:

From: Dima Dorfman <dd@freebsd.org>
To: Giorgos Keramidas <gkeramidas@bytemobile.com>
Cc: FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/73112: change atol() to strtol() in badsect
Date: Fri, 29 Oct 2004 21:20:52 +0000

 Giorgos Keramidas <keramida@FreeBSD.org> wrote:
 > 
 > >Number:         73112
 > >Synopsis:       change atol() to strtol() in badsect
 >
 > Index: badsect.c
 > ===================================================================
 > RCS file: /home/ncvs/src/sbin/badsect/badsect.c,v
 > retrieving revision 1.20
 > diff -u -u -r1.20 badsect.c
 > --- badsect.c	9 Apr 2004 19:58:25 -0000	1.20
 > +++ badsect.c	25 Oct 2004 12:00:37 -0000
 > @@ -123,7 +124,9 @@
 >  			err(7, "%s", name);
 >  	}
 >  	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
 > -		number = atol(*argv);
 > +		number = strtol(*argv, NULL, 0);
 > +		if (errno == EINVAL || errno == ERANGE)
 > +			err(8, "%s", *argv);
 
 The error handling here is incorrect. strtol never clears errno, and
 it doesn't consider it an error if part of the string wasn't
 converted. Something like this is necessary:
 
   char *ep;
   ...
   errno = 0;
   number = strtol(*argv, &ep, 0);
   if (*ep != '\0' || errno != 0)
     ...
State-Changed-From-To: open->patched 
State-Changed-By: rwatson 
State-Changed-When: Mon Jan 3 19:03:45 GMT 2005 
State-Changed-Why:  
Committed as badsect.c:1.21.  Will MFC after two weeks. 
Thanks! 



Responsible-Changed-From-To: freebsd-bugs->rwatson 
Responsible-Changed-By: rwatson 
Responsible-Changed-When: Mon Jan 3 19:03:45 GMT 2005 
Responsible-Changed-Why:  
Grab this PR. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=73112 
State-Changed-From-To: patched->closed 
State-Changed-By: rwatson 
State-Changed-When: Sun Jan 13 16:14:24 UTC 2008 
State-Changed-Why:  
Patch has been applied (long since). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=73112 
>Unformatted:
