glib: Fix various implicit conversions from size_t to smaller types

Basically various trivial instances of the following MSVC compiler
warning:
```
../gio/gio-tool-set.c(50): warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data
```

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
Philip Withnall 2024-04-25 00:42:57 +01:00
parent ec36370dcb
commit 362f92b693
No known key found for this signature in database
GPG Key ID: DCDF5885B1F3ED73
23 changed files with 58 additions and 47 deletions

View File

@ -424,7 +424,8 @@ guchar *
g_base64_decode_inplace (gchar *text, g_base64_decode_inplace (gchar *text,
gsize *out_len) gsize *out_len)
{ {
gint input_length, state = 0; gint state = 0;
size_t input_length;
guint save = 0; guint save = 0;
g_return_val_if_fail (text != NULL, NULL); g_return_val_if_fail (text != NULL, NULL);

View File

@ -2258,6 +2258,7 @@ win32_strftime_helper (const GDate *d,
gchar *convbuf; gchar *convbuf;
glong convlen = 0; glong convlen = 0;
gsize retval; gsize retval;
size_t format_len = strlen (format);
systemtime.wYear = tm->tm_year + 1900; systemtime.wYear = tm->tm_year + 1900;
systemtime.wMonth = tm->tm_mon + 1; systemtime.wMonth = tm->tm_mon + 1;
@ -2269,7 +2270,8 @@ win32_strftime_helper (const GDate *d,
systemtime.wMilliseconds = 0; systemtime.wMilliseconds = 0;
lcid = GetThreadLocale (); lcid = GetThreadLocale ();
result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2)); result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t),
(format_len <= 64) ? (guint) format_len * 2 : 128);
p = format; p = format;
while (*p) while (*p)

View File

@ -3268,7 +3268,7 @@ g_date_time_format_utf8 (GDateTime *datetime,
GString *outstr, GString *outstr,
gboolean locale_is_utf8) gboolean locale_is_utf8)
{ {
guint len; size_t len;
guint colons; guint colons;
gunichar c; gunichar c;
gboolean alt_digits = FALSE; gboolean alt_digits = FALSE;

View File

@ -647,7 +647,7 @@ g_get_environ (void)
{ {
gunichar2 *strings; gunichar2 *strings;
gchar **result; gchar **result;
gint i, n; size_t i, n;
strings = GetEnvironmentStringsW (); strings = GetEnvironmentStringsW ();
for (n = 0, i = 0; strings[n]; i++) for (n = 0, i = 0; strings[n]; i++)

View File

@ -373,7 +373,7 @@ g_file_test (const gchar *filename,
{ {
const gchar *lastdot = strrchr (filename, '.'); const gchar *lastdot = strrchr (filename, '.');
const gchar *pathext = NULL, *p; const gchar *pathext = NULL, *p;
int extlen; size_t extlen;
if (lastdot == NULL) if (lastdot == NULL)
break; break;
@ -401,7 +401,7 @@ g_file_test (const gchar *filename,
const gchar *q = strchr (p, ';'); const gchar *q = strchr (p, ';');
if (q == NULL) if (q == NULL)
q = p + strlen (p); q = p + strlen (p);
if (extlen == q - p && if (extlen == (size_t) (q - p) &&
memcmp (lastdot, p, extlen) == 0) memcmp (lastdot, p, extlen) == 0)
{ {
g_free ((gchar *) pathext); g_free ((gchar *) pathext);

View File

@ -1420,9 +1420,9 @@ win32_is_pipe_tty (int fd)
gboolean result = FALSE; gboolean result = FALSE;
HANDLE h_fd; HANDLE h_fd;
FILE_NAME_INFO *info = NULL; FILE_NAME_INFO *info = NULL;
gint info_size = sizeof (FILE_NAME_INFO) + sizeof (WCHAR) * MAX_PATH; size_t info_size = sizeof (FILE_NAME_INFO) + sizeof (WCHAR) * MAX_PATH;
wchar_t *name = NULL; wchar_t *name = NULL;
gint length; size_t length;
h_fd = (HANDLE) _get_osfhandle (fd); h_fd = (HANDLE) _get_osfhandle (fd);

View File

@ -1461,7 +1461,7 @@ parse_long_option (GOptionContext *context,
} }
else else
{ {
gint len = strlen (group->entries[j].long_name); size_t len = strlen (group->entries[j].long_name);
if (strncmp (arg, group->entries[j].long_name, len) == 0 && if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
(arg[len] == '=' || arg[len] == 0)) (arg[len] == '=' || arg[len] == 0))

View File

@ -65,9 +65,9 @@ typedef enum
struct _GPatternSpec struct _GPatternSpec
{ {
GMatchType match_type; GMatchType match_type;
guint pattern_length; size_t pattern_length;
guint min_length; size_t min_length;
guint max_length; size_t max_length;
gchar *pattern; gchar *pattern;
}; };

View File

@ -930,7 +930,7 @@ g_ascii_formatd (gchar *buffer,
const char *decimal_point; const char *decimal_point;
gsize decimal_point_len; gsize decimal_point_len;
gchar *p; gchar *p;
int rest_len; size_t rest_len;
gchar format_char; gchar format_char;
g_return_val_if_fail (buffer != NULL, NULL); g_return_val_if_fail (buffer != NULL, NULL);

View File

@ -134,7 +134,7 @@ g_string_new (const gchar *init)
string = g_string_sized_new (2); string = g_string_sized_new (2);
else else
{ {
gint len; size_t len;
len = strlen (init); len = strlen (init);
string = g_string_sized_new (len + 2); string = g_string_sized_new (len + 2);

View File

@ -3035,7 +3035,7 @@ static gboolean
path_has_prefix (const char *path, path_has_prefix (const char *path,
const char *prefix) const char *prefix)
{ {
int prefix_len = strlen (prefix); size_t prefix_len = strlen (prefix);
return (strncmp (path, prefix, prefix_len) == 0 && return (strncmp (path, prefix, prefix_len) == 0 &&
(path[prefix_len] == '\0' || (path[prefix_len] == '\0' ||

View File

@ -187,11 +187,11 @@ lookup_item_id_for_locale (const gchar *locale)
{ {
gchar key[MAX_LOCALE_NAME + 1]; gchar key[MAX_LOCALE_NAME + 1];
const gchar *language; const gchar *language;
guint language_len; size_t language_len;
const gchar *territory = NULL; const gchar *territory = NULL;
guint territory_len = 0; size_t territory_len = 0;
const gchar *modifier = NULL; const gchar *modifier = NULL;
guint modifier_len = 0; size_t modifier_len = 0;
const gchar *next_char; const gchar *next_char;
guint id; guint id;

View File

@ -805,7 +805,7 @@ output_special_case (gchar *out_buffer,
int which) int which)
{ {
const gchar *p = special_case_table + offset; const gchar *p = special_case_table + offset;
gint len; size_t len;
if (type != G_UNICODE_TITLECASE_LETTER) if (type != G_UNICODE_TITLECASE_LETTER)
p = g_utf8_next_char (p); p = g_utf8_next_char (p);

View File

@ -1250,7 +1250,7 @@ g_utf16_to_ucs4 (const gunichar2 *str,
const gunichar2 *in; const gunichar2 *in;
gchar *out; gchar *out;
gchar *result = NULL; gchar *result = NULL;
gint n_bytes; size_t n_bytes;
gunichar high_surrogate; gunichar high_surrogate;
g_return_val_if_fail (str != NULL, NULL); g_return_val_if_fail (str != NULL, NULL);

View File

@ -196,7 +196,7 @@ g_find_program_in_path (const gchar *program)
strchr (last_dot, '\\') != NULL || strchr (last_dot, '\\') != NULL ||
strchr (last_dot, '/') != NULL) strchr (last_dot, '/') != NULL)
{ {
const gint program_length = strlen (program); const size_t program_length = strlen (program);
gchar *pathext = g_build_path (";", gchar *pathext = g_build_path (";",
".exe;.cmd;.bat;.com", ".exe;.cmd;.bat;.com",
g_getenv ("PATHEXT"), g_getenv ("PATHEXT"),

View File

@ -327,10 +327,10 @@ static gboolean
token_stream_peek_string (TokenStream *stream, token_stream_peek_string (TokenStream *stream,
const gchar *token) const gchar *token)
{ {
gint length = strlen (token); size_t length = strlen (token);
return token_stream_prepare (stream) && return token_stream_prepare (stream) &&
stream->stream - stream->this == length && (size_t) (stream->stream - stream->this) == length &&
memcmp (stream->this, token, length) == 0; memcmp (stream->this, token, length) == 0;
} }

View File

@ -133,7 +133,7 @@ array_new_zero_terminated (void)
garray = g_array_new (TRUE, FALSE, sizeof (gchar)); garray = g_array_new (TRUE, FALSE, sizeof (gchar));
g_assert_cmpuint (garray->len, ==, 0); g_assert_cmpuint (garray->len, ==, 0);
g_array_append_vals (garray, "hello", strlen ("hello")); g_array_append_vals (garray, "hello", (guint) strlen ("hello"));
g_assert_cmpuint (garray->len, ==, 5); g_assert_cmpuint (garray->len, ==, 5);
g_assert_cmpstr (garray->data, ==, "hello"); g_assert_cmpstr (garray->data, ==, "hello");

View File

@ -313,7 +313,7 @@ invalid_mutation (const gchar *type_string)
/* else, perform a random mutation at a random point */ /* else, perform a random mutation at a random point */
{ {
gint length, n; size_t length, n;
gchar *new; gchar *new;
gchar p; gchar p;

View File

@ -37,9 +37,9 @@ typedef enum
struct _GPatternSpec struct _GPatternSpec
{ {
GMatchType match_type; GMatchType match_type;
guint pattern_length; size_t pattern_length;
guint min_length; size_t min_length;
guint max_length; size_t max_length;
gchar *pattern; gchar *pattern;
}; };
@ -50,7 +50,7 @@ struct _CompileTest
const gchar *src; const gchar *src;
GMatchType match_type; GMatchType match_type;
gchar *pattern; gchar *pattern;
guint min; size_t min;
}; };
static CompileTest compile_tests[] = { static CompileTest compile_tests[] = {
@ -80,8 +80,8 @@ test_compilation (gconstpointer d)
g_assert_cmpint (spec->match_type, ==, test->match_type); g_assert_cmpint (spec->match_type, ==, test->match_type);
g_assert_cmpstr (spec->pattern, ==, test->pattern); g_assert_cmpstr (spec->pattern, ==, test->pattern);
g_assert_cmpint (spec->pattern_length, ==, strlen (spec->pattern)); g_assert_cmpuint (spec->pattern_length, ==, strlen (spec->pattern));
g_assert_cmpint (spec->min_length, ==, test->min); g_assert_cmpuint (spec->min_length, ==, test->min);
g_pattern_spec_free (spec); g_pattern_spec_free (spec);
} }
@ -97,8 +97,8 @@ test_copy (gconstpointer d)
g_assert_cmpint (p2->match_type, ==, test->match_type); g_assert_cmpint (p2->match_type, ==, test->match_type);
g_assert_cmpstr (p2->pattern, ==, test->pattern); g_assert_cmpstr (p2->pattern, ==, test->pattern);
g_assert_cmpint (p2->pattern_length, ==, strlen (p1->pattern)); g_assert_cmpuint (p2->pattern_length, ==, strlen (p1->pattern));
g_assert_cmpint (p2->min_length, ==, test->min); g_assert_cmpuint (p2->min_length, ==, test->min);
g_pattern_spec_free (p1); g_pattern_spec_free (p1);
g_pattern_spec_free (p2); g_pattern_spec_free (p2);
@ -186,7 +186,8 @@ test_match (gconstpointer d)
r = g_utf8_strreverse (test->string, -1); r = g_utf8_strreverse (test->string, -1);
g_assert_cmpint (g_pattern_spec_match (p, strlen (test->string), test->string, r), ==, test->match); g_assert_cmpint (g_pattern_spec_match (p, strlen (test->string), test->string, r), ==, test->match);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS G_GNUC_BEGIN_IGNORE_DEPRECATIONS
g_assert_cmpint (g_pattern_match (p, strlen (test->string), test->string, r), ==, test->match); g_assert_cmpuint (strlen (test->string), <=, G_MAXUINT);
g_assert_cmpint (g_pattern_match (p, (guint) strlen (test->string), test->string, r), ==, test->match);
G_GNUC_END_IGNORE_DEPRECATIONS G_GNUC_END_IGNORE_DEPRECATIONS
g_free (r); g_free (r);

View File

@ -111,7 +111,7 @@ test_scanner_tokens (ScannerFixture *fix,
gconstpointer test_data) gconstpointer test_data)
{ {
gchar buf[] = "(\t\n\r\\){}"; gchar buf[] = "(\t\n\r\\){}";
const gint buflen = strlen (buf); const size_t buflen = strlen (buf);
gchar tokbuf[] = "(\\){}"; gchar tokbuf[] = "(\\){}";
const gsize tokbuflen = strlen (tokbuf); const gsize tokbuflen = strlen (tokbuf);
gsize i; gsize i;

View File

@ -36,7 +36,8 @@ WinMain (struct HINSTANCE__ *hInstance,
{ {
int infd = atoi (__argv[2]); int infd = atoi (__argv[2]);
int outfd = atoi (__argv[3]); int outfd = atoi (__argv[3]);
int k, n; SSIZE_T k;
size_t n;
char buf[100] = {0}; char buf[100] = {0};
if (infd < 0 || outfd < 0) if (infd < 0 || outfd < 0)
@ -55,22 +56,28 @@ WinMain (struct HINSTANCE__ *hInstance,
exit (1); exit (1);
} }
if ((k = read (infd, &n, sizeof (n))) != sizeof (n)) k = read (infd, &n, sizeof (n));
if (k < 0 || (size_t) k != sizeof (n))
{ {
fprintf (stderr, "spawn-test-win32-gui: Got only %d bytes, wanted %d\n", int errsv = errno;
k, (int)sizeof (n)); if (k < 0)
fprintf (stderr, "spawn-test-win32-gui: Read error: %s\n", strerror (errsv));
else
fprintf (stderr, "spawn-test-win32-gui: Got only %zu bytes, wanted %zu\n",
(size_t) k, sizeof (n));
exit (1); exit (1);
} }
fprintf (stderr, "spawn-test-win32-gui: Parent says %d bytes to read\n", n); fprintf (stderr, "spawn-test-win32-gui: Parent says %zu bytes to read\n", n);
if ((k = read (infd, buf, n)) != n) k = read (infd, buf, n);
if (k < 0 || (size_t) k != n)
{ {
int errsv = errno; int errsv = errno;
if (k == -1) if (k < 0)
fprintf (stderr, "spawn-test-win32-gui: Read error: %s\n", strerror (errsv)); fprintf (stderr, "spawn-test-win32-gui: Read error: %s\n", strerror (errsv));
else else
fprintf (stderr, "spawn-test-win32-gui: Got only %d bytes\n", k); fprintf (stderr, "spawn-test-win32-gui: Got only %zu bytes\n", (size_t) k);
exit (1); exit (1);
} }

View File

@ -198,7 +198,7 @@ test_spawn_basics (void)
gchar *output = NULL; gchar *output = NULL;
gchar *erroutput = NULL; gchar *erroutput = NULL;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
int n; size_t n;
char buf[100]; char buf[100];
int pipedown[2], pipeup[2]; int pipedown[2], pipeup[2];
gchar **argv = NULL; gchar **argv = NULL;

View File

@ -455,7 +455,7 @@ test_tree_insert (void)
{ {
GTree *tree; GTree *tree;
gchar *p; gchar *p;
gint i; size_t i;
gchar *scrambled; gchar *scrambled;
tree = g_tree_new (my_compare); tree = g_tree_new (my_compare);
@ -468,8 +468,8 @@ test_tree_insert (void)
g_tree_unref (tree); g_tree_unref (tree);
tree = g_tree_new (my_compare); tree = g_tree_new (my_compare);
for (i = strlen (chars) - 1; i >= 0; i--) for (i = strlen (chars); i > 0; i--)
g_tree_insert (tree, &chars[i], &chars[i]); g_tree_insert (tree, &chars[i - 1], &chars[i - 1]);
p = chars; p = chars;
g_tree_foreach (tree, check_order, &p); g_tree_foreach (tree, check_order, &p);