[PATCH 1/3] parser: be more pick for integer values
Bill Spitzak
spitzak at gmail.com
Mon Mar 12 17:49:51 PDT 2012
This will break using 0xabcd for hex numbers.
A better way that reuses the library functions test is this:
char* p;
unsigned x = strtoul(value, NULL, &p);
if (*p || (!x && value[0]!='0')) { number_was_bad };
This is not perfect because it is inconsistent about leading whitespace
but probably good enough.
Tiago Vignatti wrote:
> 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:
More information about the wayland-devel
mailing list