]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/xstrto.cc
1 #ifndef SQUID_XSTRTO_C_
2 #define SQUID_XSTRTO_C_
5 * Shamelessly duplicated from the netfilter iptables sources
6 * for use by the Squid Project under GNU Public License.
8 * Reason for use as explained by Luciano Coelho:
9 * "I found that there is a bug in strtoul (and strtoull for
10 * that matter) that causes the long to overflow if there are valid digits
11 * after the maximum possible digits for the base. For example if you try
12 * to strtoul 0xfffffffff (with 9 f's) the strtoul will overflow and come
13 * up with a bogus result. I can't easily truncate the string to avoid
14 * this problem, because with decimal or octal, the same valid value would
15 * take more spaces. I could do some magic here, checking whether it's a
16 * hex, dec or oct and truncate appropriately, but that would be very ugly.
17 * So the simplest way I came up with was to use strtoull and return
18 * -EINVAL if the value exceeds 32 bits."
20 * Update/Maintenance History:
22 * 12-Sep-2010 : Copied from iptables xtables.c
23 * - xtables_strtoui renamed to xstrtoui
24 * - xtables_strtoul renamed to xstrtoul
28 * Original License and code follows.
32 #include "compat/xstrto.h"
35 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
57 xstrtoul(const char *s
, char **end
, unsigned long *value
,
58 unsigned long min
, unsigned long max
)
64 v
= strtoul(s
, &my_end
, 0);
71 if (errno
!= ERANGE
&& min
<= v
&& (max
== 0 || v
<= max
)) {
75 return *my_end
== '\0';
83 xstrtoui(const char *s
, char **end
, unsigned int *value
,
84 unsigned int min
, unsigned int max
)
89 ret
= xstrtoul(s
, end
, &v
, min
, max
);
95 #endif /* SQUID_XSTRTO_C_ */