summaryrefslogtreecommitdiff
path: root/src/tool_paramhlp.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2012-07-10 23:11:30 +0200
committerDaniel Stenberg <daniel@haxx.se>2012-07-10 23:11:30 +0200
commitf2b6ebed7bfcfcbe65bcadbce1c5ae7e6134c11e (patch)
tree29ecfad1b06a38d9f05dbe01afda078edb705cc5 /src/tool_paramhlp.c
parente5843470e8a77d43604884cdf34ccf3f4799a7f5 (diff)
downloadcurl-f2b6ebed7bfcfcbe65bcadbce1c5ae7e6134c11e.tar.gz
cmdline: parse numerical options stricter
1 - str2offset() no longer accepts negative numbers since offsets are by nature positive. 2 - introduced str2unum() for the command line parser that accepts numericals which are not supposed to be negative, so that it will properly complain on apparent bad uses and mistakes. Bug: http://curl.haxx.se/mail/archive-2012-07/0013.html
Diffstat (limited to 'src/tool_paramhlp.c')
-rw-r--r--src/tool_paramhlp.c46
1 files changed, 35 insertions, 11 deletions
diff --git a/src/tool_paramhlp.c b/src/tool_paramhlp.c
index 4d1d17a52..85912a2ef 100644
--- a/src/tool_paramhlp.c
+++ b/src/tool_paramhlp.c
@@ -146,8 +146,8 @@ void cleanarg(char *str)
}
/*
- * Parse the string and write the integer in the given address. Return
- * non-zero on failure, zero on success.
+ * Parse the string and write the long in the given address. Return non-zero
+ * on failure, zero on success.
*
* Since this function gets called with the 'nextarg' pointer from within the
* getparameter a lot, we must check it for NULL before accessing the str
@@ -161,10 +161,26 @@ int str2num(long *val, const char *str)
long num = strtol(str, &endptr, 10);
if((endptr != str) && (endptr == str + strlen(str))) {
*val = num;
- return 0; /* Ok */
+ return PARAM_OK; /* Ok */
}
}
- return 1; /* badness */
+ return PARAM_BAD_NUMERIC; /* badness */
+}
+
+/*
+ * Parse the string and write the long in the given address. Return non-zero
+ * on failure, zero on success. ONLY ACCEPTS POSITIVE NUMBERS!
+ *
+ * Since this function gets called with the 'nextarg' pointer from within the
+ * getparameter a lot, we must check it for NULL before accessing the str
+ * data.
+ */
+
+int str2unum(long *val, const char *str)
+{
+ if(str[0]=='-')
+ return PARAM_NEGATIVE_NUMERIC; /* badness */
+ return str2num(val, str);
}
/*
@@ -274,8 +290,8 @@ long proto2num(struct Configurable *config, long *val, const char *str)
}
/**
- * Parses the given string looking for an offset (which may be
- * a larger-than-integer value).
+ * Parses the given string looking for an offset (which may be a
+ * larger-than-integer value). The offset CANNOT be negative!
*
* @param val the offset to populate
* @param str the buffer containing the offset
@@ -283,16 +299,24 @@ long proto2num(struct Configurable *config, long *val, const char *str)
*/
int str2offset(curl_off_t *val, const char *str)
{
+ char *endptr;
+ if(str[0] == '-')
+ /* offsets aren't negative, this indicates weird input */
+ return PARAM_NEGATIVE_NUMERIC;
+
#if(CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
- *val = curlx_strtoofft(str, NULL, 0);
+ *val = curlx_strtoofft(str, &endptr, 0);
if((*val == CURL_OFF_T_MAX || *val == CURL_OFF_T_MIN) && (ERRNO == ERANGE))
- return 1;
+ return PARAM_BAD_NUMERIC;
#else
- *val = strtol(str, NULL, 0);
+ *val = strtol(str, &endptr, 0);
if((*val == LONG_MIN || *val == LONG_MAX) && ERRNO == ERANGE)
- return 1;
+ return PARAM_BAD_NUMERIC;
#endif
- return 0;
+ if((endptr != str) && (endptr == str + strlen(str)))
+ return 0; /* Ok */
+
+ return PARAM_BAD_NUMERIC;
}
ParameterError checkpasswd(const char *kind, /* for what purpose */