diff options
author | schwarze <> | 2014-09-13 20:10:12 +0000 |
---|---|---|
committer | schwarze <> | 2014-09-13 20:10:12 +0000 |
commit | 231db4657473247f753eff3dda736712538c9fd7 (patch) | |
tree | ccfd3c6e1313069066f96d0ac6a84b0d3540704b /src/lib | |
parent | b7268ee743cd942f45105664ec1245058b7346d6 (diff) | |
download | openbsd-231db4657473247f753eff3dda736712538c9fd7.tar.gz openbsd-231db4657473247f753eff3dda736712538c9fd7.tar.bz2 openbsd-231db4657473247f753eff3dda736712538c9fd7.zip |
Make sure that the following functions return 0 and EINVAL as
required by the C standard when called with an invalid base:
strtoll(), strtoimax(), strtoul(), strtoull(), and strtoumax().
Same behaviour for strtoq() and strtouq() even though not standardized.
No functional change in strtol(), it was the only one already correct.
While here, simplify the conditional expression for checking the base
and sync whitespace and comments among the six files.
ok millert@
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libc/stdlib/strtoimax.c | 16 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtol.c | 7 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtoll.c | 15 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtoul.c | 11 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtoull.c | 15 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtoumax.c | 16 |
6 files changed, 60 insertions, 20 deletions
diff --git a/src/lib/libc/stdlib/strtoimax.c b/src/lib/libc/stdlib/strtoimax.c index 2c77f41650..2fc04e4850 100644 --- a/src/lib/libc/stdlib/strtoimax.c +++ b/src/lib/libc/stdlib/strtoimax.c | |||
@@ -1,6 +1,5 @@ | |||
1 | /* $OpenBSD: strtoimax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ | 1 | /* $OpenBSD: strtoimax.c,v 1.2 2014/09/13 20:10:12 schwarze Exp $ */ |
2 | 2 | /* | |
3 | /*- | ||
4 | * Copyright (c) 1992 The Regents of the University of California. | 3 | * Copyright (c) 1992 The Regents of the University of California. |
5 | * All rights reserved. | 4 | * All rights reserved. |
6 | * | 5 | * |
@@ -48,6 +47,17 @@ strtoimax(const char *nptr, char **endptr, int base) | |||
48 | int neg, any, cutlim; | 47 | int neg, any, cutlim; |
49 | 48 | ||
50 | /* | 49 | /* |
50 | * Ensure that base is between 2 and 36 inclusive, or the special | ||
51 | * value of 0. | ||
52 | */ | ||
53 | if (base < 0 || base == 1 || base > 36) { | ||
54 | if (endptr != 0) | ||
55 | *endptr = (char *)nptr; | ||
56 | errno = EINVAL; | ||
57 | return 0; | ||
58 | } | ||
59 | |||
60 | /* | ||
51 | * Skip white space and pick up leading +/- sign if any. | 61 | * Skip white space and pick up leading +/- sign if any. |
52 | * If base is 0, allow 0x for hex and 0 for octal, else | 62 | * If base is 0, allow 0x for hex and 0 for octal, else |
53 | * assume decimal; if base is already 16, allow 0x. | 63 | * assume decimal; if base is already 16, allow 0x. |
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c index dc2cf8871c..86cec35086 100644 --- a/src/lib/libc/stdlib/strtol.c +++ b/src/lib/libc/stdlib/strtol.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* $OpenBSD: strtol.c,v 1.9 2013/04/17 17:40:35 tedu Exp $ */ | 1 | /* $OpenBSD: strtol.c,v 1.10 2014/09/13 20:10:12 schwarze Exp $ */ |
2 | /*- | 2 | /* |
3 | * Copyright (c) 1990 The Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
4 | * All rights reserved. | 4 | * All rights reserved. |
5 | * | 5 | * |
@@ -33,7 +33,6 @@ | |||
33 | #include <limits.h> | 33 | #include <limits.h> |
34 | #include <stdlib.h> | 34 | #include <stdlib.h> |
35 | 35 | ||
36 | |||
37 | /* | 36 | /* |
38 | * Convert a string to a long integer. | 37 | * Convert a string to a long integer. |
39 | * | 38 | * |
@@ -52,7 +51,7 @@ strtol(const char *nptr, char **endptr, int base) | |||
52 | * Ensure that base is between 2 and 36 inclusive, or the special | 51 | * Ensure that base is between 2 and 36 inclusive, or the special |
53 | * value of 0. | 52 | * value of 0. |
54 | */ | 53 | */ |
55 | if (base != 0 && (base < 2 || base > 36)) { | 54 | if (base < 0 || base == 1 || base > 36) { |
56 | if (endptr != 0) | 55 | if (endptr != 0) |
57 | *endptr = (char *)nptr; | 56 | *endptr = (char *)nptr; |
58 | errno = EINVAL; | 57 | errno = EINVAL; |
diff --git a/src/lib/libc/stdlib/strtoll.c b/src/lib/libc/stdlib/strtoll.c index 4bcc5565be..cf82c8e1a6 100644 --- a/src/lib/libc/stdlib/strtoll.c +++ b/src/lib/libc/stdlib/strtoll.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* $OpenBSD: strtoll.c,v 1.7 2013/03/28 18:09:38 martynas Exp $ */ | 1 | /* $OpenBSD: strtoll.c,v 1.8 2014/09/13 20:10:12 schwarze Exp $ */ |
2 | /*- | 2 | /* |
3 | * Copyright (c) 1992 The Regents of the University of California. | 3 | * Copyright (c) 1992 The Regents of the University of California. |
4 | * All rights reserved. | 4 | * All rights reserved. |
5 | * | 5 | * |
@@ -50,6 +50,17 @@ strtoll(const char *nptr, char **endptr, int base) | |||
50 | int neg, any, cutlim; | 50 | int neg, any, cutlim; |
51 | 51 | ||
52 | /* | 52 | /* |
53 | * Ensure that base is between 2 and 36 inclusive, or the special | ||
54 | * value of 0. | ||
55 | */ | ||
56 | if (base < 0 || base == 1 || base > 36) { | ||
57 | if (endptr != 0) | ||
58 | *endptr = (char *)nptr; | ||
59 | errno = EINVAL; | ||
60 | return 0; | ||
61 | } | ||
62 | |||
63 | /* | ||
53 | * Skip white space and pick up leading +/- sign if any. | 64 | * Skip white space and pick up leading +/- sign if any. |
54 | * If base is 0, allow 0x for hex and 0 for octal, else | 65 | * If base is 0, allow 0x for hex and 0 for octal, else |
55 | * assume decimal; if base is already 16, allow 0x. | 66 | * assume decimal; if base is already 16, allow 0x. |
diff --git a/src/lib/libc/stdlib/strtoul.c b/src/lib/libc/stdlib/strtoul.c index a236365d2f..2aa41b76e4 100644 --- a/src/lib/libc/stdlib/strtoul.c +++ b/src/lib/libc/stdlib/strtoul.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* $OpenBSD: strtoul.c,v 1.8 2013/04/17 17:40:35 tedu Exp $ */ | 1 | /* $OpenBSD: strtoul.c,v 1.9 2014/09/13 20:10:12 schwarze Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 1990 Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
4 | * All rights reserved. | 4 | * All rights reserved. |
5 | * | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | 6 | * Redistribution and use in source and binary forms, with or without |
@@ -50,6 +50,13 @@ strtoul(const char *nptr, char **endptr, int base) | |||
50 | /* | 50 | /* |
51 | * See strtol for comments as to the logic used. | 51 | * See strtol for comments as to the logic used. |
52 | */ | 52 | */ |
53 | if (base < 0 || base == 1 || base > 36) { | ||
54 | if (endptr != 0) | ||
55 | *endptr = (char *)nptr; | ||
56 | errno = EINVAL; | ||
57 | return 0; | ||
58 | } | ||
59 | |||
53 | s = nptr; | 60 | s = nptr; |
54 | do { | 61 | do { |
55 | c = (unsigned char) *s++; | 62 | c = (unsigned char) *s++; |
diff --git a/src/lib/libc/stdlib/strtoull.c b/src/lib/libc/stdlib/strtoull.c index 28f613a087..846417630f 100644 --- a/src/lib/libc/stdlib/strtoull.c +++ b/src/lib/libc/stdlib/strtoull.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* $OpenBSD: strtoull.c,v 1.6 2013/03/28 18:09:38 martynas Exp $ */ | 1 | /* $OpenBSD: strtoull.c,v 1.7 2014/09/13 20:10:12 schwarze Exp $ */ |
2 | /*- | 2 | /* |
3 | * Copyright (c) 1992 The Regents of the University of California. | 3 | * Copyright (c) 1992 The Regents of the University of California. |
4 | * All rights reserved. | 4 | * All rights reserved. |
5 | * | 5 | * |
@@ -50,8 +50,15 @@ strtoull(const char *nptr, char **endptr, int base) | |||
50 | int neg, any, cutlim; | 50 | int neg, any, cutlim; |
51 | 51 | ||
52 | /* | 52 | /* |
53 | * See strtoq for comments as to the logic used. | 53 | * See strtoll for comments as to the logic used. |
54 | */ | 54 | */ |
55 | if (base < 0 || base == 1 || base > 36) { | ||
56 | if (endptr != 0) | ||
57 | *endptr = (char *)nptr; | ||
58 | errno = EINVAL; | ||
59 | return 0; | ||
60 | } | ||
61 | |||
55 | s = nptr; | 62 | s = nptr; |
56 | do { | 63 | do { |
57 | c = (unsigned char) *s++; | 64 | c = (unsigned char) *s++; |
@@ -59,7 +66,7 @@ strtoull(const char *nptr, char **endptr, int base) | |||
59 | if (c == '-') { | 66 | if (c == '-') { |
60 | neg = 1; | 67 | neg = 1; |
61 | c = *s++; | 68 | c = *s++; |
62 | } else { | 69 | } else { |
63 | neg = 0; | 70 | neg = 0; |
64 | if (c == '+') | 71 | if (c == '+') |
65 | c = *s++; | 72 | c = *s++; |
diff --git a/src/lib/libc/stdlib/strtoumax.c b/src/lib/libc/stdlib/strtoumax.c index ce6e2c00f1..c73f7e507c 100644 --- a/src/lib/libc/stdlib/strtoumax.c +++ b/src/lib/libc/stdlib/strtoumax.c | |||
@@ -1,6 +1,5 @@ | |||
1 | /* $OpenBSD: strtoumax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ | 1 | /* $OpenBSD: strtoumax.c,v 1.2 2014/09/13 20:10:12 schwarze Exp $ */ |
2 | 2 | /* | |
3 | /*- | ||
4 | * Copyright (c) 1992 The Regents of the University of California. | 3 | * Copyright (c) 1992 The Regents of the University of California. |
5 | * All rights reserved. | 4 | * All rights reserved. |
6 | * | 5 | * |
@@ -48,8 +47,15 @@ strtoumax(const char *nptr, char **endptr, int base) | |||
48 | int neg, any, cutlim; | 47 | int neg, any, cutlim; |
49 | 48 | ||
50 | /* | 49 | /* |
51 | * See strtoq for comments as to the logic used. | 50 | * See strtoimax for comments as to the logic used. |
52 | */ | 51 | */ |
52 | if (base < 0 || base == 1 || base > 36) { | ||
53 | if (endptr != 0) | ||
54 | *endptr = (char *)nptr; | ||
55 | errno = EINVAL; | ||
56 | return 0; | ||
57 | } | ||
58 | |||
53 | s = nptr; | 59 | s = nptr; |
54 | do { | 60 | do { |
55 | c = (unsigned char) *s++; | 61 | c = (unsigned char) *s++; |
@@ -57,7 +63,7 @@ strtoumax(const char *nptr, char **endptr, int base) | |||
57 | if (c == '-') { | 63 | if (c == '-') { |
58 | neg = 1; | 64 | neg = 1; |
59 | c = *s++; | 65 | c = *s++; |
60 | } else { | 66 | } else { |
61 | neg = 0; | 67 | neg = 0; |
62 | if (c == '+') | 68 | if (c == '+') |
63 | c = *s++; | 69 | c = *s++; |