]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/xstrto.cc
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_XSTRTO_C_
10 #define SQUID_XSTRTO_C_
13 * Shamelessly duplicated from the netfilter iptables sources
14 * for use by the Squid Project under GNU Public License.
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."
28 * Update/Maintenance History:
30 * 12-Sep-2010 : Copied from iptables xtables.c
31 * - xtables_strtoui renamed to xstrtoui
32 * - xtables_strtoul renamed to xstrtoul
34 * Original License and code follows.
38 #include "compat/xstrto.h"
41 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
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.
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.
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
55 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
61 xstrtoul(const char *s
, char **end
, unsigned long *value
,
62 unsigned long min
, unsigned long max
)
67 unsigned long v
= strtoul(s
, &my_end
, 0);
74 if (errno
!= ERANGE
&& min
<= v
&& (max
== 0 || v
<= max
)) {
78 return *my_end
== '\0';
86 xstrtoui(const char *s
, char **end
, unsigned int *value
,
87 unsigned int min
, unsigned int max
)
90 bool ret
= xstrtoul(s
, end
, &v
, min
, max
);
94 if (v
!= static_cast<unsigned long>(*value
)) {
102 #endif /* SQUID_XSTRTO_C_ */