Fix heap-buffer-overflow error detected by address sanitization on a fuzzed binary.
[deliverable/binutils-gdb.git] / gdb / common / common-utils.c
CommitLineData
d26e3629
KY
1/* Shared general utility routines for GDB, the GNU debugger.
2
618f726f 3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
d26e3629
KY
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
727605ca 20#include "common-defs.h"
03aef70f
JK
21#include "host-defs.h"
22#include <ctype.h>
d26e3629 23
d26e3629
KY
24/* The xmalloc() (libiberty.h) family of memory management routines.
25
26 These are like the ISO-C malloc() family except that they implement
27 consistent semantics and guard against typical memory management
28 problems. */
29
30/* NOTE: These are declared using PTR to ensure consistency with
31 "libiberty.h". xfree() is GDB local. */
32
33PTR /* ARI: PTR */
34xmalloc (size_t size)
35{
36 void *val;
37
38 /* See libiberty/xmalloc.c. This function need's to match that's
39 semantics. It never returns NULL. */
40 if (size == 0)
41 size = 1;
42
43 val = malloc (size); /* ARI: malloc */
44 if (val == NULL)
45 malloc_failure (size);
46
47 return val;
48}
49
50PTR /* ARI: PTR */
51xrealloc (PTR ptr, size_t size) /* ARI: PTR */
52{
53 void *val;
54
55 /* See libiberty/xmalloc.c. This function need's to match that's
56 semantics. It never returns NULL. */
57 if (size == 0)
58 size = 1;
59
60 if (ptr != NULL)
61 val = realloc (ptr, size); /* ARI: realloc */
62 else
63 val = malloc (size); /* ARI: malloc */
64 if (val == NULL)
65 malloc_failure (size);
66
67 return val;
68}
69
70PTR /* ARI: PTR */
71xcalloc (size_t number, size_t size)
72{
73 void *mem;
74
75 /* See libiberty/xmalloc.c. This function need's to match that's
76 semantics. It never returns NULL. */
77 if (number == 0 || size == 0)
78 {
79 number = 1;
80 size = 1;
81 }
82
83 mem = calloc (number, size); /* ARI: xcalloc */
84 if (mem == NULL)
85 malloc_failure (number * size);
86
87 return mem;
88}
89
90void *
91xzalloc (size_t size)
92{
93 return xcalloc (1, size);
94}
95
96void
97xfree (void *ptr)
98{
99 if (ptr != NULL)
100 free (ptr); /* ARI: free */
101}
102
51403f74
NC
103void
104xmalloc_failed (size_t size)
105{
106 malloc_failure (size);
107}
108
d26e3629
KY
109/* Like asprintf/vasprintf but get an internal_error if the call
110 fails. */
111
112char *
113xstrprintf (const char *format, ...)
114{
115 char *ret;
116 va_list args;
117
118 va_start (args, format);
119 ret = xstrvprintf (format, args);
120 va_end (args);
121 return ret;
122}
123
124char *
125xstrvprintf (const char *format, va_list ap)
126{
127 char *ret = NULL;
128 int status = vasprintf (&ret, format, ap);
129
130 /* NULL is returned when there was a memory allocation problem, or
131 any other error (for instance, a bad format string). A negative
132 status (the printed length) with a non-NULL buffer should never
133 happen, but just to be sure. */
134 if (ret == NULL || status < 0)
135 internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
136 return ret;
137}
138
d26e3629
KY
139int
140xsnprintf (char *str, size_t size, const char *format, ...)
141{
142 va_list args;
143 int ret;
144
145 va_start (args, format);
146 ret = vsnprintf (str, size, format, args);
147 gdb_assert (ret < size);
148 va_end (args);
149
150 return ret;
151}
baea0dae
PA
152
153char *
154savestring (const char *ptr, size_t len)
155{
156 char *p = (char *) xmalloc (len + 1);
157
158 memcpy (p, ptr, len);
159 p[len] = 0;
160 return p;
161}
03aef70f
JK
162
163/* The bit offset of the highest byte in a ULONGEST, for overflow
164 checking. */
165
166#define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
167
168/* True (non-zero) iff DIGIT is a valid digit in radix BASE,
169 where 2 <= BASE <= 36. */
170
171static int
172is_digit_in_base (unsigned char digit, int base)
173{
174 if (!isalnum (digit))
175 return 0;
176 if (base <= 10)
177 return (isdigit (digit) && digit < base + '0');
178 else
179 return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
180}
181
182static int
183digit_to_int (unsigned char c)
184{
185 if (isdigit (c))
186 return c - '0';
187 else
188 return tolower (c) - 'a' + 10;
189}
190
191/* As for strtoul, but for ULONGEST results. */
192
193ULONGEST
194strtoulst (const char *num, const char **trailer, int base)
195{
196 unsigned int high_part;
197 ULONGEST result;
198 int minus = 0;
199 int i = 0;
200
201 /* Skip leading whitespace. */
202 while (isspace (num[i]))
203 i++;
204
205 /* Handle prefixes. */
206 if (num[i] == '+')
207 i++;
208 else if (num[i] == '-')
209 {
210 minus = 1;
211 i++;
212 }
213
214 if (base == 0 || base == 16)
215 {
216 if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
217 {
218 i += 2;
219 if (base == 0)
220 base = 16;
221 }
222 }
223
224 if (base == 0 && num[i] == '0')
225 base = 8;
226
227 if (base == 0)
228 base = 10;
229
230 if (base < 2 || base > 36)
231 {
232 errno = EINVAL;
233 return 0;
234 }
235
236 result = high_part = 0;
237 for (; is_digit_in_base (num[i], base); i += 1)
238 {
239 result = result * base + digit_to_int (num[i]);
240 high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
241 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
242 if (high_part > 0xff)
243 {
244 errno = ERANGE;
245 result = ~ (ULONGEST) 0;
246 high_part = 0;
247 minus = 0;
248 break;
249 }
250 }
251
252 if (trailer != NULL)
253 *trailer = &num[i];
254
255 result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
256 if (minus)
257 return -result;
258 else
259 return result;
260}
261
41548caa 262/* See documentation in common-utils.h. */
03aef70f
JK
263
264char *
265skip_spaces (char *chp)
266{
267 if (chp == NULL)
268 return NULL;
269 while (*chp && isspace (*chp))
270 chp++;
271 return chp;
272}
273
274/* A const-correct version of the above. */
275
276const char *
277skip_spaces_const (const char *chp)
278{
279 if (chp == NULL)
280 return NULL;
281 while (*chp && isspace (*chp))
282 chp++;
283 return chp;
284}
285
41548caa 286/* See documentation in common-utils.h. */
03aef70f
JK
287
288const char *
289skip_to_space_const (const char *chp)
290{
291 if (chp == NULL)
292 return NULL;
293 while (*chp && !isspace (*chp))
294 chp++;
295 return chp;
296}
This page took 0.365308 seconds and 4 git commands to generate.