[PATCH 1/3] parser: be more pick for integer values

Tiago Vignatti tiago.vignatti at intel.com
Mon Mar 12 15:06:38 PDT 2012


It was accepting "-i=3", "-i=3/2", "--idle-time=*3" and similar unwanted type
of arguments, giving wrong impression for the user. Now it explicitly ignores.

Signed-off-by: Tiago Vignatti <tiago.vignatti at intel.com>
---
these three patches are here:
http://cgit.freedesktop.org/~vignatti/weston/?h=options

 shared/option-parser.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/shared/option-parser.c b/shared/option-parser.c
index 600f110..8b16b5d 100644
--- a/shared/option-parser.c
+++ b/shared/option-parser.c
@@ -29,13 +29,57 @@
 #include "config-parser.h"
 
 static void
+check_int(const char *value)
+{
+	int count = 0, i = 1;
+
+	if (isdigit(value[0]) || value[0] == '-' || value[0] == '+')
+		count++;
+
+	while (value[i]) {
+		if (isdigit(value[i]))
+			count++;
+		i++;
+	}
+
+	if (count != strlen(value)) {
+		fprintf(stderr, "%s: invalid character in %s\n",
+				__func__, value);
+		assert(0);
+	}
+}
+
+static void
+check_uint(const char *value)
+{
+	int count = 0, i = 1;
+
+	if (isdigit(value[0]) || value[0] == '+')
+		count++;
+
+	while (value[i]) {
+		if (isdigit(value[i]))
+			count++;
+		i++;
+	}
+
+	if (count != strlen(value)) {
+		fprintf(stderr, "%s: invalid character in %s\n",
+				__func__, value);
+		assert(0);
+	}
+}
+
+static void
 handle_option(const struct weston_option *option, char *value)
 {
 	switch (option->type) {
 	case WESTON_OPTION_INTEGER:
+		check_int(value);
 		* (int32_t *) option->data = strtol(value, NULL, 0);
 		return;
 	case WESTON_OPTION_UNSIGNED_INTEGER:
+		check_uint(value);
 		* (uint32_t *) option->data = strtoul(value, NULL, 0);
 		return;
 	case WESTON_OPTION_STRING:
-- 
1.7.5.4



More information about the wayland-devel mailing list