MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
util.c
1 
2 /* NDBMEMCACHE NOTE */
3 /* This file has diverged from the original memcache source */
4 
5 
6 
7 #include "config.h"
8 #include <assert.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <stdlib.h>
13 
14 #include <memcached/util.h>
15 
16 /* Avoid warnings on solaris, where isspace() is an index into an array, and gcc uses signed chars */
17 #define xisspace(c) isspace((unsigned char)c)
18 
19 bool safe_strtoull(const char *str, uint64_t *out) {
20  assert(out != NULL);
21  errno = 0;
22  *out = 0;
23  char *endptr;
24  unsigned long long ull = strtoull(str, &endptr, 10);
25  if (errno == ERANGE)
26  return false;
27  if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
28  if ((long long) ull < 0) {
29  /* only check for negative signs in the uncommon case when
30  * the unsigned number is so big that it's negative as a
31  * signed number. */
32  if (strchr(str, '-') != NULL) {
33  return false;
34  }
35  }
36  *out = ull;
37  return true;
38  }
39  return false;
40 }
41 
42 bool safe_strtoul(const char *str, uint32_t *out) {
43  char *endptr = NULL;
44  unsigned long l = 0;
45  assert(out);
46  assert(str);
47  *out = 0;
48  errno = 0;
49 
50  l = strtoul(str, &endptr, 10);
51  if (errno == ERANGE) {
52  return false;
53  }
54 
55  if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
56  if ((long) l < 0) {
57  /* only check for negative signs in the uncommon case when
58  * the unsigned number is so big that it's negative as a
59  * signed number. */
60  if (strchr(str, '-') != NULL) {
61  return false;
62  }
63  }
64  *out = l;
65  return true;
66  }
67 
68  return false;
69 }