From 165a922a95ad4436e1242523e28af6ba1f2e0c6b Mon Sep 17 00:00:00 2001 From: Emmanuel Fleury Date: Mon, 4 Feb 2019 13:31:28 +0100 Subject: [PATCH] Fixing various warnings in glib/gstring.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In file included from glib/glibconfig.h:9, from glib/gtypes.h:32, from glib/gstring.h:32, from glib/gstring.c:37: glib/gstring.c: In function ‘g_string_insert_len’: glib/gstring.c:441:31: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] g_return_val_if_fail (pos <= string->len, string); ^~ glib/gmacros.h:455:25: note: in definition of macro ‘G_LIKELY’ #define G_LIKELY(expr) (expr) ^~~~ glib/gstring.c:441:5: note: in expansion of macro ‘g_return_val_if_fail’ g_return_val_if_fail (pos <= string->len, string); ^~~~~~~~~~~~~~~~~~~~ glib/gstring.c:458:15: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] if (pos < string->len) ^ glib/gstring.c:462:18: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gssize’ {aka ‘long int’} [-Werror=sign-compare] if (offset < pos) ^ In file included from glib/glibconfig.h:9, from glib/gtypes.h:32, from glib/gstring.h:32, from glib/gstring.c:37: glib/gmacros.h:351:26: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘long unsigned int’ [-Werror=sign-compare] #define MIN(a, b) (((a) < (b)) ? (a) : (b)) ^ glib/gstring.c:464:22: note: in expansion of macro ‘MIN’ precount = MIN (len, pos - offset); ^~~ glib/gmacros.h:351:35: error: operand of ?: changes signedness from ‘gssize’ {aka ‘long int’} to ‘long unsigned int’ due to unsignedness of other operand [-Werror=sign-compare] #define MIN(a, b) (((a) < (b)) ? (a) : (b)) ^~~ glib/gstring.c:464:22: note: in expansion of macro ‘MIN’ precount = MIN (len, pos - offset); ^~~ glib/gstring.c:469:15: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] if (len > precount) ^ glib/gstring.c:481:15: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] if (pos < string->len) ^ In file included from glib/glibconfig.h:9, from glib/gtypes.h:32, from glib/gstring.h:32, from glib/gstring.c:37: glib/gstring.c: In function ‘g_string_insert_c’: glib/gstring.c:782:31: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] g_return_val_if_fail (pos <= string->len, string); ^~ glib/gmacros.h:455:25: note: in definition of macro ‘G_LIKELY’ #define G_LIKELY(expr) (expr) ^~~~ glib/gstring.c:782:5: note: in expansion of macro ‘g_return_val_if_fail’ g_return_val_if_fail (pos <= string->len, string); ^~~~~~~~~~~~~~~~~~~~ glib/gstring.c:785:11: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] if (pos < string->len) ^ In file included from glib/glibconfig.h:9, from glib/gtypes.h:32, from glib/gstring.h:32, from glib/gstring.c:37: glib/gstring.c: In function ‘g_string_insert_unichar’: glib/gstring.c:857:31: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] g_return_val_if_fail (pos <= string->len, string); ^~ glib/gmacros.h:455:25: note: in definition of macro ‘G_LIKELY’ #define G_LIKELY(expr) (expr) ^~~~ glib/gstring.c:857:5: note: in expansion of macro ‘g_return_val_if_fail’ g_return_val_if_fail (pos <= string->len, string); ^~~~~~~~~~~~~~~~~~~~ glib/gstring.c:860:11: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] if (pos < string->len) ^ In file included from glib/glibconfig.h:9, from glib/gtypes.h:32, from glib/gstring.h:32, from glib/gstring.c:37: glib/gstring.c: In function ‘g_string_erase’: glib/gstring.c:969:29: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] g_return_val_if_fail (pos <= string->len, string); ^~ glib/gmacros.h:455:25: note: in definition of macro ‘G_LIKELY’ #define G_LIKELY(expr) (expr) ^~~~ glib/gstring.c:969:3: note: in expansion of macro ‘g_return_val_if_fail’ g_return_val_if_fail (pos <= string->len, string); ^~~~~~~~~~~~~~~~~~~~ glib/gstring.c:975:39: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] g_return_val_if_fail (pos + len <= string->len, string); ^~ glib/gmacros.h:455:25: note: in definition of macro ‘G_LIKELY’ #define G_LIKELY(expr) (expr) ^~~~ glib/gstring.c:975:7: note: in expansion of macro ‘g_return_val_if_fail’ g_return_val_if_fail (pos + len <= string->len, string); ^~~~~~~~~~~~~~~~~~~~ glib/gstring.c:977:21: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} [-Werror=sign-compare] if (pos + len < string->len) ^ --- glib/gstring.c | 82 +++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/glib/gstring.c b/glib/gstring.c index c083c7e0e..24692f135 100644 --- a/glib/gstring.c +++ b/glib/gstring.c @@ -428,6 +428,8 @@ g_string_insert_len (GString *string, const gchar *val, gssize len) { + gsize len_unsigned, pos_unsigned; + g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (len == 0 || val != NULL, string); @@ -436,11 +438,15 @@ g_string_insert_len (GString *string, if (len < 0) len = strlen (val); + len_unsigned = len; if (pos < 0) - pos = string->len; + pos_unsigned = string->len; else - g_return_val_if_fail (pos <= string->len, string); + { + pos_unsigned = pos; + g_return_val_if_fail (pos_unsigned <= string->len, string); + } /* Check whether val represents a substring of string. * This test probably violates chapter and verse of the C standards, @@ -452,45 +458,48 @@ g_string_insert_len (GString *string, gsize offset = val - string->str; gsize precount = 0; - g_string_maybe_expand (string, len); + g_string_maybe_expand (string, len_unsigned); val = string->str + offset; /* At this point, val is valid again. */ /* Open up space where we are going to insert. */ - if (pos < string->len) - memmove (string->str + pos + len, string->str + pos, string->len - pos); + if (pos_unsigned < string->len) + memmove (string->str + pos_unsigned + len_unsigned, + string->str + pos_unsigned, string->len - pos_unsigned); /* Move the source part before the gap, if any. */ - if (offset < pos) + if (offset < pos_unsigned) { - precount = MIN (len, pos - offset); - memcpy (string->str + pos, val, precount); + precount = MIN (len_unsigned, pos_unsigned - offset); + memcpy (string->str + pos_unsigned, val, precount); } /* Move the source part after the gap, if any. */ - if (len > precount) - memcpy (string->str + pos + precount, - val + /* Already moved: */ precount + /* Space opened up: */ len, - len - precount); + if (len_unsigned > precount) + memcpy (string->str + pos_unsigned + precount, + val + /* Already moved: */ precount + + /* Space opened up: */ len_unsigned, + len_unsigned - precount); } else { - g_string_maybe_expand (string, len); + g_string_maybe_expand (string, len_unsigned); /* If we aren't appending at the end, move a hunk * of the old string to the end, opening up space */ - if (pos < string->len) - memmove (string->str + pos + len, string->str + pos, string->len - pos); + if (pos_unsigned < string->len) + memmove (string->str + pos_unsigned + len_unsigned, + string->str + pos_unsigned, string->len - pos_unsigned); /* insert the new string */ - if (len == 1) - string->str[pos] = *val; + if (len_unsigned == 1) + string->str[pos_unsigned] = *val; else - memcpy (string->str + pos, val, len); + memcpy (string->str + pos_unsigned, val, len_unsigned); } - string->len += len; + string->len += len_unsigned; string->str[string->len] = 0; @@ -778,6 +787,8 @@ g_string_insert_c (GString *string, gssize pos, gchar c) { + gsize pos_unsigned; + g_return_val_if_fail (string != NULL, NULL); g_string_maybe_expand (string, 1); @@ -785,13 +796,15 @@ g_string_insert_c (GString *string, if (pos < 0) pos = string->len; else - g_return_val_if_fail (pos <= string->len, string); + g_return_val_if_fail ((gsize) pos <= string->len, string); + pos_unsigned = pos; /* If not just an append, move the old stuff */ - if (pos < string->len) - memmove (string->str + pos + 1, string->str + pos, string->len - pos); + if (pos_unsigned < string->len) + memmove (string->str + pos_unsigned + 1, + string->str + pos_unsigned, string->len - pos_unsigned); - string->str[pos] = c; + string->str[pos_unsigned] = c; string->len += 1; @@ -860,10 +873,10 @@ g_string_insert_unichar (GString *string, if (pos < 0) pos = string->len; else - g_return_val_if_fail (pos <= string->len, string); + g_return_val_if_fail ((gsize) pos <= string->len, string); /* If not just an append, move the old stuff */ - if (pos < string->len) + if ((gsize) pos < string->len) memmove (string->str + pos + charlen, string->str + pos, string->len - pos); dest = string->str + pos; @@ -970,21 +983,28 @@ g_string_erase (GString *string, gssize pos, gssize len) { + gsize len_unsigned, pos_unsigned; + g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (pos >= 0, string); - g_return_val_if_fail (pos <= string->len, string); + pos_unsigned = pos; + + g_return_val_if_fail (pos_unsigned <= string->len, string); if (len < 0) - len = string->len - pos; + len_unsigned = string->len - pos_unsigned; else { - g_return_val_if_fail (pos + len <= string->len, string); + len_unsigned = len; + g_return_val_if_fail (pos_unsigned + len_unsigned <= string->len, string); - if (pos + len < string->len) - memmove (string->str + pos, string->str + pos + len, string->len - (pos + len)); + if (pos_unsigned + len_unsigned < string->len) + memmove (string->str + pos_unsigned, + string->str + pos_unsigned + len_unsigned, + string->len - (pos_unsigned + len_unsigned)); } - string->len -= len; + string->len -= len_unsigned; string->str[string->len] = 0;