xstrto.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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 
9 #ifndef SQUID_XSTRTO_C_
10 #define SQUID_XSTRTO_C_
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
31  * - xtables_strtoui renamed to xstrtoui
32  * - xtables_strtoul renamed to xstrtoul
33  *
34  * Original License and code follows.
35  */
36 
37 #include "squid.h"
38 #include "compat/xstrto.h"
39 
40 /*
41  * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
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
55  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
56  */
57 
58 #include <cerrno>
59 
60 static bool
61 xstrtoul(const char *s, char **end, unsigned long *value,
62  unsigned long min, unsigned long max)
63 {
64  char *my_end = nullptr;
65 
66  errno = 0;
67  unsigned long v = strtoul(s, &my_end, 0);
68 
69  if (my_end == s)
70  return false;
71  if (end)
72  *end = my_end;
73 
74  if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
75  if (value)
76  *value = v;
77  if (!end)
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 {
89  unsigned long v = 0;
90  bool ret = xstrtoul(s, end, &v, min, max);
91  if (value) {
92  *value = v;
93 
94  if (v != static_cast<unsigned long>(*value)) {
95  return false;
96  }
97  }
98 
99  return ret;
100 }
101 
102 #endif /* SQUID_XSTRTO_C_ */
103 
bool xstrtoui(const char *s, char **end, unsigned int *value, unsigned int min, unsigned int max)
Definition: xstrto.cc:86
const A & max(A const &lhs, A const &rhs)
static bool xstrtoul(const char *s, char **end, unsigned long *value, unsigned long min, unsigned long max)
Definition: xstrto.cc:61
const A & min(A const &lhs, A const &rhs)

 

Introduction

Documentation

Support

Miscellaneous