summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorBrad Fitzpatrick <brad@danga.com>2008-10-14 22:51:46 -0700
committerDustin Sallings <dustin@spy.net>2009-03-19 01:52:53 -0700
commit674166c002fe2e32f0385d12351091d738150a12 (patch)
tree11d85df4a9c52b18b35f98a2f12a3c619f06d03e /util.c
parent420aa2d992093b78c1bba6cbb3577d5b53a19ec8 (diff)
downloadmemcached-674166c002fe2e32f0385d12351091d738150a12.tar.gz
flesh out safe_strto* util funcs, and make incrdecr use them
Diffstat (limited to 'util.c')
-rw-r--r--util.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/util.c b/util.c
index dbd62a1..f580669 100644
--- a/util.c
+++ b/util.c
@@ -1,16 +1,45 @@
#include <stdlib.h>
#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
#include "memcached.h"
bool safe_strtoull(const char *str, unsigned long long *out) {
assert(out != NULL);
+ errno = 0;
*out = 0;
char *endptr;
unsigned long long ull = strtoull(str, &endptr, 10);
- if (*endptr == '\0' && endptr != str) {
+ if (errno == ERANGE)
+ return false;
+ if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
+ if ((long long) ull < 0) {
+ // only check for negative signs in the uncommon case when the unsigned
+ // number is so big that it's negative as a signed number.
+ if (strchr(str, '-') != NULL) {
+ return false;
+ }
+ }
*out = ull;
return true;
}
return false;
}
+
+bool safe_strtoll(const char *str, long long *out) {
+ assert(out != NULL);
+ errno = 0;
+ *out = 0;
+ char *endptr;
+ long long ll = strtoll(str, &endptr, 10);
+ if (errno == ERANGE)
+ return false;
+ if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
+ *out = ll;
+ return true;
+ }
+ return false;
+}