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