From 7f99c1e58842613573da51737a126bdeec835f1a Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Fri, 5 Apr 2019 13:41:08 +0200 Subject: [PATCH] cssparser: Split integer gettig into has/consume functions We can't try to get an integer because ultimately integer getters support the same shenanigans that numbers and percentages do with calc() and whatnot. --- gtk/gtkcsseasevalue.c | 3 +-- gtk/gtkcssfontfeaturesvalue.c | 15 +++++++++++++-- gtk/gtkcssimagescaled.c | 6 ++++-- gtk/gtkcssparser.c | 31 +++++++++++++++++++++++++------ gtk/gtkcssparserprivate.h | 5 +++-- gtk/gtkcssselector.c | 16 ++++++++++++++-- 6 files changed, 60 insertions(+), 16 deletions(-) diff --git a/gtk/gtkcsseasevalue.c b/gtk/gtkcsseasevalue.c index 7dc851cd5b..cdf8e96bde 100644 --- a/gtk/gtkcsseasevalue.c +++ b/gtk/gtkcsseasevalue.c @@ -273,9 +273,8 @@ gtk_css_ease_value_parse_steps_arg (GtkCssParser *parser, switch (arg) { case 0: - if (!_gtk_css_parser_try_int (parser, &data->n_steps)) + if (!gtk_css_parser_consume_integer (parser, &data->n_steps)) { - _gtk_css_parser_error (parser, "Expected number of steps"); return 0; } else if (data->n_steps < 1) diff --git a/gtk/gtkcssfontfeaturesvalue.c b/gtk/gtkcssfontfeaturesvalue.c index a64ee3fce5..6f7c2894f0 100644 --- a/gtk/gtkcssfontfeaturesvalue.c +++ b/gtk/gtkcssfontfeaturesvalue.c @@ -256,8 +256,19 @@ gtk_css_font_features_value_parse (GtkCssParser *parser) val = _gtk_css_number_value_new (1.0, GTK_CSS_NUMBER); else if (gtk_css_parser_try_ident (parser, "off")) val = _gtk_css_number_value_new (0.0, GTK_CSS_NUMBER); - else if (_gtk_css_parser_try_int (parser, &num)) - val = _gtk_css_number_value_new ((double)num, GTK_CSS_NUMBER); + else if (gtk_css_parser_has_integer (parser)) + { + if (gtk_css_parser_consume_integer (parser, &num)) + { + val = _gtk_css_number_value_new ((double)num, GTK_CSS_NUMBER); + } + else + { + g_free (name); + _gtk_css_value_unref (result); + return NULL; + } + } else val = _gtk_css_number_value_new (1.0, GTK_CSS_NUMBER); diff --git a/gtk/gtkcssimagescaled.c b/gtk/gtkcssimagescaled.c index a5d4e22898..6e01464a6f 100644 --- a/gtk/gtkcssimagescaled.c +++ b/gtk/gtkcssimagescaled.c @@ -160,10 +160,12 @@ gtk_css_image_scaled_parse_arg (GtkCssParser *parser, child = _gtk_css_image_new_parse (parser); if (child == NULL) - return FALSE; + return 0; - if (!_gtk_css_parser_try_int (parser, &scale)) + if (!gtk_css_parser_has_integer (parser)) scale = arg > 0 ? g_array_index (data->scales, int, arg - 1) + 1 : 1; + else if (!gtk_css_parser_consume_integer (parser, &scale)) + return 0; g_ptr_array_add (data->images, child); g_array_append_val (data->scales, scale); diff --git a/gtk/gtkcssparser.c b/gtk/gtkcssparser.c index 7599cf558c..2b05cb0e24 100644 --- a/gtk/gtkcssparser.c +++ b/gtk/gtkcssparser.c @@ -673,6 +673,21 @@ gtk_css_parser_has_ident (GtkCssParser *parser, parser->data[len] != '('; } +gboolean +gtk_css_parser_has_integer (GtkCssParser *parser) +{ + guint i; + + i = 0; + if (parser->data[0] == '-') + i++; + + if (parser->data[i] >= '0' && parser->data[i] <= '9') + return TRUE; + + return FALSE; +} + gboolean gtk_css_parser_has_function (GtkCssParser *parser, const char *name) @@ -755,8 +770,8 @@ gtk_css_parser_consume_string (GtkCssParser *parser) } gboolean -_gtk_css_parser_try_int (GtkCssParser *parser, - int *value) +gtk_css_parser_consume_integer (GtkCssParser *parser, + int *value) { gint64 result; char *end; @@ -766,16 +781,16 @@ _gtk_css_parser_try_int (GtkCssParser *parser, /* strtoll parses a plus, but we are not allowed to */ if (*parser->data == '+') - return FALSE; + goto fail; errno = 0; result = g_ascii_strtoll (parser->data, &end, 10); if (errno) - return FALSE; + goto fail; if (result > G_MAXINT || result < G_MININT) - return FALSE; + goto fail; if (parser->data == end) - return FALSE; + goto fail; parser->data = end; *value = result; @@ -783,6 +798,10 @@ _gtk_css_parser_try_int (GtkCssParser *parser, _gtk_css_parser_skip_whitespace (parser); return TRUE; + +fail: + _gtk_css_parser_error (parser, "Expected an integer"); + return FALSE; } gboolean diff --git a/gtk/gtkcssparserprivate.h b/gtk/gtkcssparserprivate.h index 5354f7a0f6..0bcba1159c 100644 --- a/gtk/gtkcssparserprivate.h +++ b/gtk/gtkcssparserprivate.h @@ -66,6 +66,7 @@ gboolean gtk_css_parser_has_token (GtkCssParser *parser GtkCssTokenType token_type); gboolean gtk_css_parser_has_ident (GtkCssParser *parser, const char *name); +gboolean gtk_css_parser_has_integer (GtkCssParser *parser); gboolean gtk_css_parser_has_function (GtkCssParser *parser, const char *name); @@ -92,8 +93,6 @@ char * _gtk_css_parser_try_ident (GtkCssParser *parser gboolean skip_whitespace); char * _gtk_css_parser_try_name (GtkCssParser *parser, gboolean skip_whitespace); -gboolean _gtk_css_parser_try_int (GtkCssParser *parser, - int *value); gboolean _gtk_css_parser_try_hash_color (GtkCssParser *parser, GdkRGBA *rgba); @@ -101,6 +100,8 @@ char * gtk_css_parser_consume_ident (GtkCssParser *self); char * gtk_css_parser_consume_string (GtkCssParser *self); gboolean gtk_css_parser_consume_number (GtkCssParser *self, double *number); +gboolean gtk_css_parser_consume_integer (GtkCssParser *parser, + int *value); gboolean gtk_css_parser_consume_function (GtkCssParser *self, guint min_args, guint max_args, diff --git a/gtk/gtkcssselector.c b/gtk/gtkcssselector.c index 6658f5eb33..30de213cc7 100644 --- a/gtk/gtkcssselector.c +++ b/gtk/gtkcssselector.c @@ -1025,8 +1025,14 @@ parse_selector_pseudo_class_nth_child (GtkCssParser *parser, else multiplier = 1; - if (_gtk_css_parser_try_int (parser, &a)) + if (gtk_css_parser_has_integer (parser)) { + if (!gtk_css_parser_consume_integer (parser, &a)) + { + if (selector) + _gtk_css_selector_free (selector); + return NULL; + } if (a < 0) { _gtk_css_parser_error (parser, "Expected an integer"); @@ -1057,8 +1063,14 @@ parse_selector_pseudo_class_nth_child (GtkCssParser *parser, else multiplier = 1; - if (_gtk_css_parser_try_int (parser, &b)) + if (gtk_css_parser_has_integer (parser)) { + if (!gtk_css_parser_consume_integer (parser, &b)) + { + if (selector) + _gtk_css_selector_free (selector); + return NULL; + } if (b < 0) { _gtk_css_parser_error (parser, "Expected an integer");