diff options
| -rw-r--r-- | common/flatpak-dir.c | 6 | ||||
| -rw-r--r-- | common/flatpak-glib-backports-private.h | 21 | ||||
| -rw-r--r-- | common/flatpak-glib-backports.c | 46 | ||||
| -rw-r--r-- | tests/testcommon.c | 64 |
4 files changed, 72 insertions, 65 deletions
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c index 2f80a70f..1fc04f32 100644 --- a/common/flatpak-dir.c +++ b/common/flatpak-dir.c @@ -4194,9 +4194,9 @@ _flatpak_dir_ensure_repo (FlatpakDir *self, /* Scrap previously written min-free-space-percent=0 and replace it with min-free-space-size */ if (orig_min_free_space_size == NULL && orig_min_free_space_percent != NULL && - flatpak_utils_ascii_string_to_unsigned (orig_min_free_space_percent, 10, - 0, G_MAXUINT64, - &min_free_space_percent_int, &my_error)) + g_ascii_string_to_unsigned (orig_min_free_space_percent, 10, + 0, G_MAXUINT64, + &min_free_space_percent_int, &my_error)) { if (min_free_space_percent_int == 0) { diff --git a/common/flatpak-glib-backports-private.h b/common/flatpak-glib-backports-private.h index fff93693..d030aaf9 100644 --- a/common/flatpak-glib-backports-private.h +++ b/common/flatpak-glib-backports-private.h @@ -87,15 +87,20 @@ g_ptr_array_find_with_equal_func (GPtrArray *haystack, return FALSE; } -#endif -/* From GLib 2.54, currently used unconditionally */ -gboolean flatpak_utils_ascii_string_to_unsigned (const gchar *str, - guint base, - guint64 min, - guint64 max, - guint64 *out_num, - GError **error); +/* We're non-specific about the error behaviour, so this is good enough */ +#define G_NUMBER_PARSER_ERROR (G_IO_ERROR) +#define G_NUMBER_PARSER_ERROR_INVALID (G_IO_ERROR_INVALID_ARGUMENT) +#define G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS (G_IO_ERROR_INVALID_ARGUMENT) +#define GNumberParserError (GIOErrorEnum) + +gboolean g_ascii_string_to_unsigned (const gchar *str, + guint base, + guint64 min, + guint64 max, + guint64 *out_num, + GError **error); +#endif #if !GLIB_CHECK_VERSION (2, 56, 0) typedef void (* GClearHandleFunc) (guint handle_id); diff --git a/common/flatpak-glib-backports.c b/common/flatpak-glib-backports.c index a4f84169..1774963c 100644 --- a/common/flatpak-glib-backports.c +++ b/common/flatpak-glib-backports.c @@ -31,8 +31,8 @@ /* Please sort this file by the GLib version where it originated, * oldest first. */ -/* From GLib 2.54, currently used unconditionally */ -#if 1 +#if !GLIB_CHECK_VERSION (2, 54, 0) +/* All this code is backported directly from GLib 2.76.2 */ static gboolean str_has_sign (const gchar *str) { @@ -46,12 +46,12 @@ str_has_hex_prefix (const gchar *str) } gboolean -flatpak_utils_ascii_string_to_unsigned (const gchar *str, - guint base, - guint64 min, - guint64 max, - guint64 *out_num, - GError **error) +g_ascii_string_to_unsigned (const gchar *str, + guint base, + guint64 min, + guint64 max, + guint64 *out_num, + GError **error) { guint64 number; const gchar *end_ptr = NULL; @@ -65,33 +65,33 @@ flatpak_utils_ascii_string_to_unsigned (const gchar *str, if (str[0] == '\0') { g_set_error_literal (error, - G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, + G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_INVALID, _("Empty string is not a number")); return FALSE; } errno = 0; - number = g_ascii_strtoull (str, (gchar **) &end_ptr, base); + number = g_ascii_strtoull (str, (gchar **)&end_ptr, base); saved_errno = errno; if (/* We do not allow leading whitespace, but g_ascii_strtoull * accepts it and just skips it, so we need to check for it * ourselves. */ - g_ascii_isspace (str[0]) || - /* Unsigned number should have no sign. - */ - str_has_sign (str) || - /* We don't support hexadecimal numbers prefixed with 0x or - * 0X. - */ - (base == 16 && str_has_hex_prefix (str)) || - (saved_errno != 0 && saved_errno != ERANGE) || - end_ptr == NULL || - *end_ptr != '\0') + g_ascii_isspace (str[0]) || + /* Unsigned number should have no sign. + */ + str_has_sign (str) || + /* We don't support hexadecimal numbers prefixed with 0x or + * 0X. + */ + (base == 16 && str_has_hex_prefix (str)) || + (saved_errno != 0 && saved_errno != ERANGE) || + end_ptr == NULL || + *end_ptr != '\0') { g_set_error (error, - G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, + G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_INVALID, _("ā%sā is not an unsigned number"), str); return FALSE; } @@ -101,7 +101,7 @@ flatpak_utils_ascii_string_to_unsigned (const gchar *str, gchar *max_str = g_strdup_printf ("%" G_GUINT64_FORMAT, max); g_set_error (error, - G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, + G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS, _("Number ā%sā is out of bounds [%s, %s]"), str, min_str, max_str); g_free (min_str); diff --git a/tests/testcommon.c b/tests/testcommon.c index 7c96d8ed..bb6d7ecf 100644 --- a/tests/testcommon.c +++ b/tests/testcommon.c @@ -492,42 +492,43 @@ typedef struct gint max; gint expected; gboolean should_fail; + GNumberParserError code; } TestData; const TestData test_data[] = { /* typical cases for unsigned */ - { "-1", 10, 0, 2, 0, TRUE }, - { "1", 10, 0, 2, 1, FALSE }, - { "+1", 10, 0, 2, 0, TRUE }, - { "0", 10, 0, 2, 0, FALSE }, - { "+0", 10, 0, 2, 0, TRUE }, - { "-0", 10, 0, 2, 0, TRUE }, - { "2", 10, 0, 2, 2, FALSE }, - { "+2", 10, 0, 2, 0, TRUE }, - { "3", 10, 0, 2, 0, TRUE }, - { "+3", 10, 0, 2, 0, TRUE }, + { "-1", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "1", 10, 0, 2, 1, FALSE, 0}, + { "+1", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "0", 10, 0, 2, 0, FALSE, 0}, + { "+0", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "-0", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "2", 10, 0, 2, 2, FALSE, 0}, + { "+2", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "3", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS }, + { "+3", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, /* min == max cases for unsigned */ - { "2", 10, 2, 2, 2, FALSE }, - { "3", 10, 2, 2, 0, TRUE }, - { "1", 10, 2, 2, 0, TRUE }, + { "2", 10, 2, 2, 2, FALSE, 0 }, + { "3", 10, 2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS }, + { "1", 10, 2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS }, /* invalid inputs */ - { "", 10, 0, 2, 0, TRUE }, - { "a", 10, 0, 2, 0, TRUE }, - { "1a", 10, 0, 2, 0, TRUE }, + { "", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "a", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "1a", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, /* leading/trailing whitespace */ - { " 1", 10, 0, 2, 0, TRUE }, - { "1 ", 10, 0, 2, 0, TRUE }, + { " 1", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "1 ", 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, /* hexadecimal numbers */ - { "a", 16, 0, 15, 10, FALSE }, - { "0xa", 16, 0, 15, 0, TRUE }, - { "-0xa", 16, 0, 15, 0, TRUE }, - { "+0xa", 16, 0, 15, 0, TRUE }, - { "- 0xa", 16, 0, 15, 0, TRUE }, - { "+ 0xa", 16, 0, 15, 0, TRUE }, + { "a", 16, 0, 15, 10, FALSE, 0 }, + { "0xa", 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "-0xa", 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "+0xa", 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "- 0xa", 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, + { "+ 0xa", 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID }, }; static void @@ -542,19 +543,19 @@ test_string_to_unsigned (void) gboolean result; gint value; guint64 value64 = 0; - result = flatpak_utils_ascii_string_to_unsigned (data->str, - data->base, - data->min, - data->max, - &value64, - &error); + result = g_ascii_string_to_unsigned (data->str, + data->base, + data->min, + data->max, + &value64, + &error); value = value64; g_assert_cmpint (value, ==, value64); if (data->should_fail) { g_assert_false (result); - g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT); + g_assert_error (error, G_NUMBER_PARSER_ERROR, data->code); g_clear_error (&error); } else @@ -562,6 +563,7 @@ test_string_to_unsigned (void) g_assert_true (result); g_assert_no_error (error); g_assert_cmpint (value, ==, data->expected); + g_assert_cmpint (data->code, ==, 0); } } } |
