]>
Commit | Line | Data |
---|---|---|
37be9888 | 1 | /* |
bf95c10a | 2 | * Copyright (C) 1996-2022 The Squid Software Foundation and contributors |
37be9888 AJ |
3 | * |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
605f2c3e AJ |
9 | #ifndef SQUID_XSTRTO_C_ |
10 | #define SQUID_XSTRTO_C_ | |
425de4c8 AJ |
11 | |
12 | /* | |
13 | * Shamelessly duplicated from the netfilter iptables sources | |
14 | * for use by the Squid Project under GNU Public License. | |
15 | * | |
16 | * Reason for use as explained by Luciano Coelho: | |
17 | * "I found that there is a bug in strtoul (and strtoull for | |
18 | * that matter) that causes the long to overflow if there are valid digits | |
19 | * after the maximum possible digits for the base. For example if you try | |
20 | * to strtoul 0xfffffffff (with 9 f's) the strtoul will overflow and come | |
21 | * up with a bogus result. I can't easily truncate the string to avoid | |
22 | * this problem, because with decimal or octal, the same valid value would | |
23 | * take more spaces. I could do some magic here, checking whether it's a | |
24 | * hex, dec or oct and truncate appropriately, but that would be very ugly. | |
25 | * So the simplest way I came up with was to use strtoull and return | |
26 | * -EINVAL if the value exceeds 32 bits." | |
27 | * | |
28 | * Update/Maintenance History: | |
29 | * | |
30 | * 12-Sep-2010 : Copied from iptables xtables.c | |
f53969cc SM |
31 | * - xtables_strtoui renamed to xstrtoui |
32 | * - xtables_strtoul renamed to xstrtoul | |
425de4c8 | 33 | * |
425de4c8 AJ |
34 | * Original License and code follows. |
35 | */ | |
36 | ||
f7f3304a | 37 | #include "squid.h" |
25f98340 | 38 | #include "compat/xstrto.h" |
425de4c8 | 39 | |
ab745b44 A |
40 | /* |
41 | * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>: | |
425de4c8 AJ |
42 | * |
43 | * This program is free software; you can redistribute it and/or modify | |
44 | * it under the terms of the GNU General Public License as published by | |
45 | * the Free Software Foundation; either version 2 of the License, or | |
46 | * (at your option) any later version. | |
47 | * | |
48 | * This program is distributed in the hope that it will be useful, | |
49 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
50 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
51 | * GNU General Public License for more details. | |
52 | * | |
53 | * You should have received a copy of the GNU General Public License | |
54 | * along with this program; if not, write to the Free Software | |
ab745b44 | 55 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
425de4c8 AJ |
56 | */ |
57 | ||
1a30fdf5 | 58 | #include <cerrno> |
425de4c8 AJ |
59 | |
60 | bool | |
61 | xstrtoul(const char *s, char **end, unsigned long *value, | |
62 | unsigned long min, unsigned long max) | |
63 | { | |
f70cf39b | 64 | char *my_end = nullptr; |
425de4c8 AJ |
65 | |
66 | errno = 0; | |
3c320e68 | 67 | unsigned long v = strtoul(s, &my_end, 0); |
425de4c8 AJ |
68 | |
69 | if (my_end == s) | |
70 | return false; | |
f70cf39b | 71 | if (end) |
425de4c8 AJ |
72 | *end = my_end; |
73 | ||
74 | if (errno != ERANGE && min <= v && (max == 0 || v <= max)) { | |
f70cf39b | 75 | if (value) |
425de4c8 | 76 | *value = v; |
f70cf39b | 77 | if (!end) |
425de4c8 AJ |
78 | return *my_end == '\0'; |
79 | return true; | |
80 | } | |
81 | ||
82 | return false; | |
83 | } | |
84 | ||
85 | bool | |
86 | xstrtoui(const char *s, char **end, unsigned int *value, | |
87 | unsigned int min, unsigned int max) | |
88 | { | |
3c320e68 RS |
89 | unsigned long v = 0; |
90 | bool ret = xstrtoul(s, end, &v, min, max); | |
f70cf39b | 91 | if (value) { |
425de4c8 | 92 | *value = v; |
54a063a2 | 93 | |
925a2a9f AJ |
94 | if (v != static_cast<unsigned long>(*value)) { |
95 | return false; | |
96 | } | |
54a063a2 TX |
97 | } |
98 | ||
425de4c8 AJ |
99 | return ret; |
100 | } | |
101 | ||
605f2c3e | 102 | #endif /* SQUID_XSTRTO_C_ */ |
f53969cc | 103 |