aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2004-03-06 22:11:45 +0000
committerEric Andersen <andersen@codepoet.org>2004-03-06 22:11:45 +0000
commit2479445562a9b5a9f226d0b00c41dbd533e63213 (patch)
treee4891420283c085d688683a41cc217dc896917b8
parentc4db0833a6c91dd3714bec1db076a80910af6e30 (diff)
downloadbusybox-w32-2479445562a9b5a9f226d0b00c41dbd533e63213.tar.gz
busybox-w32-2479445562a9b5a9f226d0b00c41dbd533e63213.tar.bz2
busybox-w32-2479445562a9b5a9f226d0b00c41dbd533e63213.zip
Fix/eliminate use of atol
-rw-r--r--coreutils/printf.c30
-rw-r--r--libbb/Makefile.in13
-rw-r--r--libbb/safe_strtol.c92
-rw-r--r--loginutils/getty.c17
-rw-r--r--networking/ftpgetput.c5
-rw-r--r--networking/ifconfig.c7
-rw-r--r--networking/ipcalc.c3
-rw-r--r--networking/route.c5
-rw-r--r--networking/wget.c14
9 files changed, 132 insertions, 54 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c
index 181c70bfb..76f59686b 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -405,48 +405,24 @@ print_direc(char *start, size_t length, int field_width, int precision,
405static unsigned long xstrtoul(char *arg) 405static unsigned long xstrtoul(char *arg)
406{ 406{
407 unsigned long result; 407 unsigned long result;
408 char *endptr; 408 if (safe_strtoul(arg, &result))
409 //int errno_save = errno;
410
411 assert(arg!=NULL);
412
413 errno = 0;
414 result = strtoul(arg, &endptr, 0);
415 if (errno != 0 || *endptr!='\0' || endptr==arg)
416 fprintf(stderr, "%s", arg); 409 fprintf(stderr, "%s", arg);
417 //errno = errno_save;
418 return result; 410 return result;
419} 411}
420 412
421static long xstrtol(char *arg) 413static long xstrtol(char *arg)
422{ 414{
423 long result; 415 long result;
424 char *endptr; 416 if (safe_strtol(arg, &result))
425 //int errno_save = errno;
426
427 assert(arg!=NULL);
428
429 errno = 0;
430 result = strtoul(arg, &endptr, 0);
431 if (errno != 0 || *endptr!='\0' || endptr==arg)
432 fprintf(stderr, "%s", arg); 417 fprintf(stderr, "%s", arg);
433 //errno = errno_save;
434 return result; 418 return result;
435} 419}
436 420
437static double xstrtod(char *arg) 421static double xstrtod(char *arg)
438{ 422{
439 double result; 423 double result;
440 char *endptr; 424 if (safe_strtod(arg, &result))
441 //int errno_save = errno;
442
443 assert(arg!=NULL);
444
445 errno = 0;
446 result = strtod(arg, &endptr);
447 if (errno != 0 || *endptr!='\0' || endptr==arg)
448 fprintf(stderr, "%s", arg); 425 fprintf(stderr, "%s", arg);
449 //errno = errno_save;
450 return result; 426 return result;
451} 427}
452 428
diff --git a/libbb/Makefile.in b/libbb/Makefile.in
index a656a5a53..632208184 100644
--- a/libbb/Makefile.in
+++ b/libbb/Makefile.in
@@ -1,6 +1,6 @@
1# Makefile for busybox 1# Makefile for busybox
2# 2#
3# Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org> 3# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
4# 4#
5# This program is free software; you can redistribute it and/or modify 5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by 6# it under the terms of the GNU General Public License as published by
@@ -72,17 +72,21 @@ LIBBB_MSRC3:=$(LIBBB_DIR)xgetularg.c
72LIBBB_MOBJ3:=xgetularg_bnd_sfx.o xgetlarg_bnd_sfx.o getlarg10_sfx.o \ 72LIBBB_MOBJ3:=xgetularg_bnd_sfx.o xgetlarg_bnd_sfx.o getlarg10_sfx.o \
73 xgetularg_bnd.o xgetularg10_bnd.o xgetularg10.o 73 xgetularg_bnd.o xgetularg10_bnd.o xgetularg10.o
74 74
75LIBBB_MSRC4:=$(LIBBB_DIR)/safe_strtol.c
76LIBBB_MOBJ4:=safe_strtoi.o safe_strtod.o safe_strtol.o safe_strtoul.o
77
75LIBBB_MOBJS0=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ0)) 78LIBBB_MOBJS0=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ0))
76LIBBB_MOBJS1=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ1)) 79LIBBB_MOBJS1=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ1))
77LIBBB_MOBJS2=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ2)) 80LIBBB_MOBJS2=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ2))
78LIBBB_MOBJS3=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ3)) 81LIBBB_MOBJS3=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ3))
82LIBBB_MOBJS4=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ4))
79 83
80libraries-y+=$(LIBBB_DIR)$(LIBBB_AR) 84libraries-y+=$(LIBBB_DIR)$(LIBBB_AR)
81 85
82$(LIBBB_DIR)$(LIBBB_AR): $(LIBBB_OBJS) $(LIBBB_MOBJS0) $(LIBBB_MOBJS1) \ 86$(LIBBB_DIR)$(LIBBB_AR): $(LIBBB_OBJS) $(LIBBB_MOBJS0) $(LIBBB_MOBJS1) \
83 $(LIBBB_MOBJS2) $(LIBBB_MOBJS3) 87 $(LIBBB_MOBJS2) $(LIBBB_MOBJS3) $(LIBBB_MOBJS4)
84 $(AR) -ro $@ $(LIBBB_OBJS) $(LIBBB_MOBJS0) $(LIBBB_MOBJS1) \ 88 $(AR) -ro $@ $(LIBBB_OBJS) $(LIBBB_MOBJS0) $(LIBBB_MOBJS1) \
85 $(LIBBB_MOBJS2) $(LIBBB_MOBJS3) 89 $(LIBBB_MOBJS2) $(LIBBB_MOBJS3) $(LIBBB_MOBJS4)
86 90
87$(LIBBB_MOBJS0): $(LIBBB_MSRC0) 91$(LIBBB_MOBJS0): $(LIBBB_MSRC0)
88 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@ 92 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
@@ -96,3 +100,6 @@ $(LIBBB_MOBJS2): $(LIBBB_MSRC2)
96$(LIBBB_MOBJS3): $(LIBBB_MSRC3) 100$(LIBBB_MOBJS3): $(LIBBB_MSRC3)
97 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@ 101 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
98 102
103$(LIBBB_MOBJS4): $(LIBBB_MSRC4)
104 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
105
diff --git a/libbb/safe_strtol.c b/libbb/safe_strtol.c
new file mode 100644
index 000000000..fcbdba878
--- /dev/null
+++ b/libbb/safe_strtol.c
@@ -0,0 +1,92 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <stdlib.h>
23#include <errno.h>
24#include <assert.h>
25#include "libbb.h"
26
27#ifdef L_safe_strtoi
28extern
29int safe_strtoi(char *arg, int* value)
30{
31 int error;
32 long lvalue = *value;
33 error = safe_strtol(arg, &lvalue);
34 *value = (int) lvalue;
35 return error;
36}
37#endif
38
39#ifdef L_safe_strtod
40extern
41int safe_strtod(char *arg, double* value)
42{
43 char *endptr;
44 int errno_save = errno;
45
46 assert(arg!=NULL);
47 errno = 0;
48 *value = strtod(arg, &endptr);
49 if (errno != 0 || *endptr!='\0' || endptr==arg) {
50 return 1;
51 }
52 errno = errno_save;
53 return 0;
54}
55#endif
56
57#ifdef L_safe_strtol
58extern
59int safe_strtol(char *arg, long* value)
60{
61 char *endptr;
62 int errno_save = errno;
63
64 assert(arg!=NULL);
65 errno = 0;
66 *value = strtol(arg, &endptr, 0);
67 if (errno != 0 || *endptr!='\0' || endptr==arg) {
68 return 1;
69 }
70 errno = errno_save;
71 return 0;
72}
73#endif
74
75#ifdef L_safe_strtoul
76extern
77int safe_strtoul(char *arg, unsigned long* value)
78{
79 char *endptr;
80 int errno_save = errno;
81
82 assert(arg!=NULL);
83 errno = 0;
84 *value = strtoul(arg, &endptr, 0);
85 if (errno != 0 || *endptr!='\0' || endptr==arg) {
86 return 1;
87 }
88 errno = errno_save;
89 return 0;
90}
91#endif
92
diff --git a/loginutils/getty.c b/loginutils/getty.c
index 4219ff821..b12b88fb1 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -955,22 +955,15 @@ static int caps_lock(const char *s)
955/* bcode - convert speed string to speed code; return 0 on failure */ 955/* bcode - convert speed string to speed code; return 0 on failure */
956static int bcode(const char *s) 956static int bcode(const char *s)
957{ 957{
958#if 0
959 struct Speedtab *sp;
960 long speed = atol(s);
961
962 for (sp = speedtab; sp->speed; sp++)
963 if (sp->speed == speed)
964 return (sp->code);
965 return (0);
966#else
967 int r; 958 int r;
968 959 unsigned long value;
969 if ((r = bb_value_to_baud(atol(s))) > 0) { 960 if (safe_strtoul(s, &value)) {
961 return -1;
962 }
963 if ((r = bb_value_to_baud(value)) > 0) {
970 return r; 964 return r;
971 } 965 }
972 return 0; 966 return 0;
973#endif
974} 967}
975 968
976/* error - report errors to console or syslog; only understands %s and %m */ 969/* error - report errors to console or syslog; only understands %s and %m */
diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c
index 4f6be1196..17ee8a536 100644
--- a/networking/ftpgetput.c
+++ b/networking/ftpgetput.c
@@ -147,7 +147,10 @@ static int ftp_recieve(ftp_host_info_t *server, FILE *control_stream,
147 fd_data = xconnect_ftpdata(server, buf); 147 fd_data = xconnect_ftpdata(server, buf);
148 148
149 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) { 149 if (ftpcmd("SIZE ", server_path, control_stream, buf) == 213) {
150 filesize = atol(buf + 4); 150 unsigned long value=filesize;
151 if (safe_strtoul(buf + 4, &filesize))
152 bb_error_msg_and_die("SIZE error: %s", buf + 4);
153 filesize = value;
151 } 154 }
152 155
153 if ((local_path[0] == '-') && (local_path[1] == '\0')) { 156 if ((local_path[0] == '-') && (local_path[1] == '\0')) {
diff --git a/networking/ifconfig.c b/networking/ifconfig.c
index 9fdab3c3f..341998d8d 100644
--- a/networking/ifconfig.c
+++ b/networking/ifconfig.c
@@ -15,7 +15,7 @@
15 * Foundation; either version 2 of the License, or (at 15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version. 16 * your option) any later version.
17 * 17 *
18 * $Id: ifconfig.c,v 1.27 2003/11/14 03:04:08 andersen Exp $ 18 * $Id: ifconfig.c,v 1.28 2004/03/06 22:11:44 andersen Exp $
19 * 19 *
20 */ 20 */
21 21
@@ -394,8 +394,9 @@ int ifconfig_main(int argc, char **argv)
394 safe_strncpy(host, *argv, (sizeof host)); 394 safe_strncpy(host, *argv, (sizeof host));
395#ifdef CONFIG_FEATURE_IPV6 395#ifdef CONFIG_FEATURE_IPV6
396 if ((prefix = strchr(host, '/'))) { 396 if ((prefix = strchr(host, '/'))) {
397 prefix_len = atol(prefix + 1); 397 if (safe_strtoi(prefix + 1, &prefix_len) ||
398 if ((prefix_len < 0) || (prefix_len > 128)) { 398 (prefix_len < 0) || (prefix_len > 128))
399 {
399 ++goterr; 400 ++goterr;
400 goto LOOP; 401 goto LOOP;
401 } 402 }
diff --git a/networking/ipcalc.c b/networking/ipcalc.c
index 2f1c02b7b..d75c883b8 100644
--- a/networking/ipcalc.c
+++ b/networking/ipcalc.c
@@ -119,8 +119,7 @@ int ipcalc_main(int argc, char **argv)
119 if (*prefixstr) { 119 if (*prefixstr) {
120 unsigned int msk; 120 unsigned int msk;
121 121
122 netprefix = atol(prefixstr); 122 if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
123 if (netprefix > 32) {
124 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr), 123 IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr),
125 exit(EXIT_FAILURE)); 124 exit(EXIT_FAILURE));
126 } 125 }
diff --git a/networking/route.c b/networking/route.c
index 083149a3d..19942f421 100644
--- a/networking/route.c
+++ b/networking/route.c
@@ -15,7 +15,7 @@
15 * Foundation; either version 2 of the License, or (at 15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version. 16 * your option) any later version.
17 * 17 *
18 * $Id: route.c,v 1.22 2003/03/19 09:12:39 mjn3 Exp $ 18 * $Id: route.c,v 1.23 2004/03/06 22:11:44 andersen Exp $
19 * 19 *
20 * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru> 20 * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
21 * adjustments by Larry Doolittle <LRDoolittle@lbl.gov> 21 * adjustments by Larry Doolittle <LRDoolittle@lbl.gov>
@@ -351,8 +351,7 @@ static int INET6_setroute(int action, int options, char **args)
351 memset(&sa6, 0, sizeof(sa6)); 351 memset(&sa6, 0, sizeof(sa6));
352 } else { 352 } else {
353 if ((cp = strchr(target, '/'))) { 353 if ((cp = strchr(target, '/'))) {
354 prefix_len = atol(cp + 1); 354 if (safe_strtod(cp + 1, &prefix_len) || (prefix_len < 0) || (prefix_len > 128))
355 if ((prefix_len < 0) || (prefix_len > 128))
356 bb_show_usage(); 355 bb_show_usage();
357 *cp = 0; 356 *cp = 0;
358 } else { 357 } else {
diff --git a/networking/wget.c b/networking/wget.c
index 823a053db..cb0790ea7 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -385,7 +385,11 @@ read_response:
385 */ 385 */
386 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) { 386 while ((s = gethdr(buf, sizeof(buf), sfp, &n)) != NULL) {
387 if (strcasecmp(buf, "content-length") == 0) { 387 if (strcasecmp(buf, "content-length") == 0) {
388 filesize = atol(s); 388 unsigned long value;
389 if (safe_strtoul(s, &value)) {
390 close_delete_and_die("content-length %s is garbage", s);
391 }
392 filesize = value;
389 got_clen = 1; 393 got_clen = 1;
390 continue; 394 continue;
391 } 395 }
@@ -452,7 +456,11 @@ read_response:
452 * Querying file size 456 * Querying file size
453 */ 457 */
454 if (ftpcmd("SIZE /", target.path, sfp, buf) == 213) { 458 if (ftpcmd("SIZE /", target.path, sfp, buf) == 213) {
455 filesize = atol(buf+4); 459 unsigned long value;
460 if (safe_strtoul(buf+4, &value)) {
461 close_delete_and_die("SIZE value is garbage");
462 }
463 filesize = value;
456 got_clen = 1; 464 got_clen = 1;
457 } 465 }
458 466
@@ -838,7 +846,7 @@ progressmeter(int flag)
838 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 846 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
839 * SUCH DAMAGE. 847 * SUCH DAMAGE.
840 * 848 *
841 * $Id: wget.c,v 1.69 2004/02/22 00:27:34 bug1 Exp $ 849 * $Id: wget.c,v 1.70 2004/03/06 22:11:44 andersen Exp $
842 */ 850 */
843 851
844 852