summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorDustin Sallings <dustin@spy.net>2009-03-18 19:36:52 -0700
committerDustin Sallings <dustin@spy.net>2009-03-19 01:52:54 -0700
commit63ba0e14d0aed8d2d82c10d0b4706f204927da0c (patch)
treed33cff43be95863ffaeb15f3dc88326db3678033 /util.c
parenta977b5563ab73b764c5b257d6ed1f182b5cd92ca (diff)
downloadmemcached-63ba0e14d0aed8d2d82c10d0b4706f204927da0c.tar.gz
Added safe_strtou?l functions.
Some of the tests for this were written but commented out because they fail on some platforms.
Diffstat (limited to 'util.c')
-rw-r--r--util.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/util.c b/util.c
index a2d0728..307f7f0 100644
--- a/util.c
+++ b/util.c
@@ -44,3 +44,47 @@ bool safe_strtoll(const char *str, int64_t *out) {
}
return false;
}
+
+bool safe_strtoul(const char *str, uint32_t *out) {
+ char *endptr = NULL;
+ unsigned long l = 0;
+ assert(out);
+ assert(str);
+ *out = 0;
+ errno = 0;
+
+ l = strtoul(str, &endptr, 10);
+ if (errno == ERANGE) {
+ return false;
+ }
+
+ if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
+ if ((long) l < 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 = l;
+ return true;
+ }
+
+ return false;
+}
+
+bool safe_strtol(const char *str, int32_t *out) {
+ assert(out != NULL);
+ errno = 0;
+ *out = 0;
+ char *endptr;
+ long l = strtol(str, &endptr, 10);
+ if (errno == ERANGE)
+ return false;
+ if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
+ *out = l;
+ return true;
+ }
+ return false;
+}