summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorOran Agra <oran@redislabs.com>2019-11-04 08:50:29 +0200
committerOran Agra <oran@redislabs.com>2019-11-04 08:50:29 +0200
commit04233097688ee35d451c6f5cd64c28e57ca81b53 (patch)
tree5082d820bb59819eb02307e099ee47c9106b6bfe /src/util.c
parentdeebed23e16c2cab9e0362e94bca8545d6e33596 (diff)
downloadredis-04233097688ee35d451c6f5cd64c28e57ca81b53.tar.gz
Add RM_ServerInfoGetFieldUnsigned
rename RM_ServerInfoGetFieldNumerical RM_ServerInfoGetFieldSigned move string2ull to util.c fix leak in RM_GetServerInfo when duplicate info fields exist
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 6e9dc2117..f0f1a4ed3 100644
--- a/src/util.c
+++ b/src/util.c
@@ -423,6 +423,26 @@ int string2ll(const char *s, size_t slen, long long *value) {
return 1;
}
+/* Helper function to convert a string to an unsigned long long value.
+ * The function attempts to use the faster string2ll() function inside
+ * Redis: if it fails, strtoull() is used instead. The function returns
+ * 1 if the conversion happened successfully or 0 if the number is
+ * invalid or out of range. */
+int string2ull(const char *s, unsigned long long *value) {
+ long long ll;
+ if (string2ll(s,strlen(s),&ll)) {
+ if (ll < 0) return 0; /* Negative values are out of range. */
+ *value = ll;
+ return 1;
+ }
+ errno = 0;
+ char *endptr = NULL;
+ *value = strtoull(s,&endptr,10);
+ if (errno == EINVAL || errno == ERANGE || !(*s != '\0' && *endptr == '\0'))
+ return 0; /* strtoull() failed. */
+ return 1; /* Conversion done! */
+}
+
/* Convert a string into a long. Returns 1 if the string could be parsed into a
* (non-overflowing) long, 0 otherwise. The value will be set to the parsed
* value when appropriate. */