In this version I also check for errors from integer parsing and print the offending value, if any.

Florian
>From 160741273589a574a8443378aab9077605788e35 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Florian=20H=C3=A4nel?= <florian.hae...@lge.com>
Date: Thu, 12 May 2016 14:40:44 -0700
Subject: [PATCH] Catch missing commandline values

Exit if commandline options are missing values.

Tested with:

missing = and value
$ weston --tty
missing =
$ weston --tty 2
missing value
$ weston --tty=
no digits found:
$ weston --tty=ADASDASDAS
overflow:
$ weston --tty=99999999999999999999999999999999999999999999999999
hex prefix is allowed:
$ weston --tty=0x2
---
 shared/option-parser.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/shared/option-parser.c b/shared/option-parser.c
index 7061268..2ac6c69 100644
--- a/shared/option-parser.c
+++ b/shared/option-parser.c
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <errno.h>
 
 #include "config-parser.h"
 
@@ -34,20 +35,32 @@ static int
 handle_option(const struct weston_option *option, char *value)
 {
 	char* p;
+	if (strlen(value) == 0) {
+		fprintf(stderr, "\nERROR: expected value after %s=\n", option->name);
+		exit(EXIT_FAILURE);
+	}
 
 	switch (option->type) {
 	case WESTON_OPTION_INTEGER:
 		* (int32_t *) option->data = strtol(value, &p, 0);
+        if(errno == EINVAL || errno == ERANGE || p == value) goto err_parse;
 		return *value && !*p;
 	case WESTON_OPTION_UNSIGNED_INTEGER:
 		* (uint32_t *) option->data = strtoul(value, &p, 0);
+        if(errno == EINVAL || errno == ERANGE || p == value) goto err_parse;
 		return *value && !*p;
 	case WESTON_OPTION_STRING:
 		* (char **) option->data = strdup(value);
+        if(errno == ENOMEM) goto err_parse;
 		return 1;
 	default:
-		assert(0);
+		fprintf(stderr, "\nERROR: internal commandline parsing error after option %s.\n",option->name);
+		exit(EXIT_FAILURE);
 	}
+    err_parse:
+		fprintf(stderr, "\nERROR: invalid value (%s) for option %s.\n", value, option->name);
+		exit(EXIT_FAILURE);
+
 }
 
 static int
@@ -71,6 +84,9 @@ long_option(const struct weston_option *options, int count, char *arg)
 			}
 		} else if (arg[len+2] == '=') {
 			return handle_option(options + k, arg + len + 3);
+		} else {
+			fprintf(stderr, "\nERROR: expected syntax: --%s=<value>\n", options[k].name);
+			exit(EXIT_FAILURE);
 		}
 	}
 
-- 
2.7.4

_______________________________________________
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/wayland-devel

Reply via email to