[PATCH v2] parser: be more picky for integer values
Tiago Vignatti
tiago.vignatti at intel.com
Wed Mar 14 16:08:30 PDT 2012
It was silently accepting "-i=3", "-i=3/2", "--idle-time=*3" and similar
unwanted type of arguments, not changing anything internally; this
gives the wrong impression for the user. Now it explicitly ignores.
Signed-off-by: Tiago Vignatti <tiago.vignatti at intel.com>
---
since v1:
- use strtol() built-in features for checking - thanks Pekka!
- updated with a more clear commit message
shared/option-parser.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 52 insertions(+), 2 deletions(-)
diff --git a/shared/option-parser.c b/shared/option-parser.c
index 600f110..6efde9d 100644
--- a/shared/option-parser.c
+++ b/shared/option-parser.c
@@ -25,18 +25,68 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
+#include <limits.h>
+#include <errno.h>
#include "config-parser.h"
+static int
+get_int(const char *value)
+{
+ char *end;
+ long int v;
+
+ errno = 0;
+ v = strtol(value, &end, 0);
+ if ((errno == ERANGE && (v == LONG_MAX || v == LONG_MIN)) ||
+ (errno != 0 && v == 0) ||
+ (end == value) ||
+ (*end != '\0')) {
+ fprintf(stderr, "arg %s is not a valid number\n", value);
+ assert(0);
+ }
+
+ if (v < INT_MIN || v > INT_MAX) {
+ fprintf(stderr, "arg %s doesn't fit into integer\n", value);
+ assert(0);
+ }
+
+ return (int32_t) v;
+}
+
+static unsigned int
+get_uint(const char *value)
+{
+ char *end;
+ long int v;
+
+ errno = 0;
+ v = strtol(value, &end, 0);
+ if ((errno == ERANGE && (v == ULONG_MAX)) ||
+ (errno != 0 && v == 0) ||
+ (end == value) ||
+ (*end != '\0')) {
+ fprintf(stderr, "arg %s is not a valid number\n", value);
+ assert(0);
+ }
+
+ if (v < 0 || v > INT_MAX) {
+ fprintf(stderr, "arg %s doesn't fit into integer\n", value);
+ assert(0);
+ }
+
+ return (uint32_t) v;
+}
+
static void
handle_option(const struct weston_option *option, char *value)
{
switch (option->type) {
case WESTON_OPTION_INTEGER:
- * (int32_t *) option->data = strtol(value, NULL, 0);
+ * (int32_t *) option->data = get_int(value);
return;
case WESTON_OPTION_UNSIGNED_INTEGER:
- * (uint32_t *) option->data = strtoul(value, NULL, 0);
+ * (uint32_t *) option->data = get_uint(value);
return;
case WESTON_OPTION_STRING:
* (char **) option->data = strdup(value);
--
1.7.5.4
More information about the wayland-devel
mailing list