Merge branch '3527-Wshorten-64-to-32' into 'main'

Fix various -Wshorten-64-to-32 warnings

See merge request GNOME/glib!4387
This commit is contained in:
Philip Withnall
2025-04-03 15:07:09 +00:00
12 changed files with 541 additions and 271 deletions

View File

@@ -129,7 +129,7 @@ journal_stream_fd (const char *identifier,
if (fd < 0)
goto fail;
salen = offsetof (struct sockaddr_un, sun_path) + strlen (sa.un.sun_path) + 1;
salen = offsetof (struct sockaddr_un, sun_path) + (socklen_t) strlen (sa.un.sun_path) + 1;
if (connect (fd, &sa.sa, salen) < 0)
goto fail;

View File

@@ -35,6 +35,7 @@
#include "gmessages.h"
#include "gstring.h"
#include "gstrfuncs.h"
#include "gtestutils.h"
#include "glibintl.h"
#ifdef G_PLATFORM_WIN32
@@ -208,7 +209,7 @@ punycode_encode (const gchar *input_utf8,
*/
static gchar *
remove_junk (const gchar *str,
gint len)
gssize len)
{
GString *cleaned = NULL;
const gchar *p;
@@ -237,7 +238,7 @@ remove_junk (const gchar *str,
static inline gboolean
contains_uppercase_letters (const gchar *str,
gint len)
gssize len)
{
const gchar *p;
@@ -251,7 +252,7 @@ contains_uppercase_letters (const gchar *str,
static inline gboolean
contains_non_ascii (const gchar *str,
gint len)
gssize len)
{
const gchar *p;
@@ -298,10 +299,11 @@ idna_is_prohibited (gunichar ch)
/* RFC 3491 IDN cleanup algorithm. */
static gchar *
nameprep (const gchar *hostname,
gint len,
gssize len,
gboolean *is_unicode)
{
gchar *name, *tmp = NULL, *p;
const char *name, *p;
char *name_owned = NULL, *name_normalized = NULL;
/* It would be nice if we could do this without repeatedly
* allocating strings and converting back and forth between
@@ -311,21 +313,20 @@ nameprep (const gchar *hostname,
*/
/* Remove presentation-only characters */
name = remove_junk (hostname, len);
name = name_owned = remove_junk (hostname, len);
if (name)
{
tmp = name;
len = -1;
}
len = -1;
else
name = (gchar *)hostname;
name = hostname;
/* Convert to lowercase */
if (contains_uppercase_letters (name, len))
{
name = g_utf8_strdown (name, len);
g_free (tmp);
tmp = name;
char *name_owned_lower = NULL;
name = name_owned_lower = g_utf8_strdown (name, len);
g_free (name_owned);
name_owned = g_steal_pointer (&name_owned_lower);
len = -1;
}
@@ -333,18 +334,19 @@ nameprep (const gchar *hostname,
if (!contains_non_ascii (name, len))
{
*is_unicode = FALSE;
if (name == (gchar *)hostname)
if (name == hostname)
return len == -1 ? g_strdup (hostname) : g_strndup (hostname, len);
else
return name;
return g_steal_pointer (&name_owned);
}
*is_unicode = TRUE;
/* Normalize */
name = g_utf8_normalize (name, len, G_NORMALIZE_NFKC);
g_free (tmp);
tmp = name;
name = name_normalized = g_utf8_normalize (name, len, G_NORMALIZE_NFKC);
g_free (name_owned);
name_owned = g_steal_pointer (&name_normalized);
len = -1;
if (!name)
return NULL;
@@ -356,11 +358,14 @@ nameprep (const gchar *hostname,
* same as tolower(nfkc(X)), then we could skip the first tolower,
* but I'm not sure it is.)
*/
if (contains_uppercase_letters (name, -1))
if (contains_uppercase_letters (name, len))
{
name = g_utf8_strdown (name, -1);
g_free (tmp);
tmp = name;
char *name_owned_lower = NULL;
name = name_owned_lower = g_utf8_strdown (name, len);
g_free (name_owned);
name_owned = g_steal_pointer (&name_owned_lower);
len = -1;
}
/* Check for prohibited characters */
@@ -369,7 +374,8 @@ nameprep (const gchar *hostname,
if (idna_is_prohibited (g_utf8_get_char (p)))
{
name = NULL;
g_free (tmp);
g_clear_pointer (&name_owned, g_free);
len = -1;
goto done;
}
}
@@ -379,7 +385,7 @@ nameprep (const gchar *hostname,
*/
done:
return name;
return g_steal_pointer (&name_owned);
}
/* RFC 3490, section 3.1 says '.', 0x3002, 0xFF0E, and 0xFF61 count as
@@ -582,8 +588,10 @@ punycode_decode (const gchar *input,
split--;
if (split > input)
{
g_assert ((guint) (split - input) <= G_MAXUINT);
output_chars = g_array_sized_new (FALSE, FALSE, sizeof (gunichar),
split - input);
(guint) (split - input));
input_length -= (split - input) + 1;
while (input < split)
{

View File

@@ -234,7 +234,7 @@ GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
static gboolean
debug_key_matches (const gchar *key,
const gchar *token,
guint length)
size_t length)
{
/* may not call GLib functions: see note in g_parse_debug_string() */
for (; length; length--, key++, token++)

View File

@@ -2397,7 +2397,7 @@ journal_sendv (struct iovec *iov,
memset (&mh, 0, sizeof (mh));
mh.msg_name = &sa;
mh.msg_namelen = offsetof (struct sockaddr_un, sun_path) + strlen (sa.sun_path);
mh.msg_namelen = offsetof (struct sockaddr_un, sun_path) + (socklen_t) strlen (sa.sun_path);
mh.msg_iov = iov;
mh.msg_iovlen = iovlen;

View File

@@ -513,7 +513,7 @@ g_option_context_add_main_entries (GOptionContext *context,
g_option_group_set_translation_domain (context->main_group, translation_domain);
}
static gint
static size_t
calculate_max_length (GOptionGroup *group,
GHashTable *aliases)
{
@@ -553,7 +553,7 @@ calculate_max_length (GOptionGroup *group,
static void
print_entry (GOptionGroup *group,
gint max_length,
size_t max_length,
const GOptionEntry *entry,
GString *string,
GHashTable *aliases)
@@ -601,7 +601,7 @@ group_has_visible_entries (GOptionContext *context,
{
GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
GOptionEntry *entry;
gint i, l;
size_t i, l;
gboolean main_group = group == context->main_group;
if (!main_entries)
@@ -691,7 +691,7 @@ g_option_context_get_help (GOptionContext *context,
GOptionGroup *group)
{
GList *list;
gint max_length = 0, len;
size_t max_length = 0, len;
gsize i;
GOptionEntry *entry;
GHashTable *shadow_map;
@@ -833,6 +833,8 @@ g_option_context_get_help (GOptionContext *context,
/* Add a bit of padding */
max_length += 4;
g_assert (max_length <= G_MAXINT);
if (!group && context->help_enabled)
{
list = context->groups;
@@ -840,13 +842,13 @@ g_option_context_get_help (GOptionContext *context,
token = context_has_h_entry (context) ? '?' : 'h';
g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
_("Help Options:"), token, max_length - 4, "help",
_("Help Options:"), token, (int) max_length - 4, "help",
_("Show help options"));
/* We only want --help-all when there are groups */
if (list)
g_string_append_printf (string, " --%-*s %s\n",
max_length, "help-all",
(int) max_length, "help-all",
_("Show all help options"));
while (list)
@@ -855,7 +857,7 @@ g_option_context_get_help (GOptionContext *context,
if (group_has_visible_entries (context, g, FALSE))
g_string_append_printf (string, " --help-%-*s %s\n",
max_length - 5, g->name,
(int) max_length - 5, g->name,
TRANSLATE (g, g->help_description));
list = list->next;
@@ -981,7 +983,7 @@ parse_int (const gchar *arg_name,
return FALSE;
}
*result = tmp;
*result = (int) tmp;
if (*result != tmp || errno == ERANGE)
{
g_set_error (error,
@@ -1810,7 +1812,7 @@ g_option_context_parse (GOptionContext *context,
gchar ***argv,
GError **error)
{
gint i, j, k;
gint i, k;
GList *list;
g_return_val_if_fail (context != NULL, FALSE);
@@ -1959,13 +1961,14 @@ g_option_context_parse (GOptionContext *context,
}
else
{ /* short option */
gint new_i = i, arg_length;
gint new_i = i;
size_t arg_length;
gboolean *nulled_out = NULL;
gboolean has_h_entry = context_has_h_entry (context);
arg = (*argv)[i] + 1;
arg_length = strlen (arg);
nulled_out = g_newa0 (gboolean, arg_length);
for (j = 0; j < arg_length; j++)
for (size_t j = 0; j < arg_length; j++)
{
if (context->help_enabled && (arg[j] == '?' ||
(arg[j] == 'h' && !has_h_entry)))
@@ -2004,7 +2007,7 @@ g_option_context_parse (GOptionContext *context,
{
gchar *new_arg = NULL;
gint arg_index = 0;
for (j = 0; j < arg_length; j++)
for (size_t j = 0; j < arg_length; j++)
{
if (!nulled_out[j])
{
@@ -2093,7 +2096,7 @@ g_option_context_parse (GOptionContext *context,
if (k > i)
{
k -= i;
for (j = i + k; j < *argc; j++)
for (int j = i + k; j < *argc; j++)
{
(*argv)[j-k] = (*argv)[j];
(*argv)[j] = NULL;

View File

@@ -289,12 +289,13 @@ g_pattern_spec_new (const gchar *pattern)
{
GPatternSpec *pspec;
gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
size_t hw_pos = 0, tw_pos = 0, hj_pos = 0, tj_pos = 0;
gboolean hw_pos_set = FALSE, hj_pos_set = FALSE;
gboolean follows_wildcard = FALSE;
guint pending_jokers = 0;
const gchar *s;
gchar *d;
guint i;
size_t i;
g_return_val_if_fail (pattern != NULL, NULL);
@@ -316,8 +317,11 @@ g_pattern_spec_new (const gchar *pattern)
continue;
}
follows_wildcard = TRUE;
if (hw_pos < 0)
hw_pos = i;
if (!hw_pos_set)
{
hw_pos = i;
hw_pos_set = TRUE;
}
tw_pos = i;
break;
case '?':
@@ -328,8 +332,11 @@ g_pattern_spec_new (const gchar *pattern)
default:
for (; pending_jokers; pending_jokers--, i++) {
*d++ = '?';
if (hj_pos < 0)
hj_pos = i;
if (!hj_pos_set)
{
hj_pos = i;
hj_pos_set = TRUE;
}
tj_pos = i;
}
follows_wildcard = FALSE;
@@ -342,13 +349,16 @@ g_pattern_spec_new (const gchar *pattern)
}
for (; pending_jokers; pending_jokers--) {
*d++ = '?';
if (hj_pos < 0)
hj_pos = i;
if (!hj_pos_set)
{
hj_pos = i;
hj_pos_set = TRUE;
}
tj_pos = i;
}
*d++ = 0;
seen_joker = hj_pos >= 0;
seen_wildcard = hw_pos >= 0;
seen_joker = hj_pos_set;
seen_wildcard = hw_pos_set;
more_wildcards = seen_wildcard && hw_pos != tw_pos;
if (seen_wildcard)
pspec->max_length = G_MAXUINT;

View File

@@ -185,7 +185,7 @@ g_rand_new (void)
if (dev_urandom)
{
int r;
size_t r;
setvbuf (dev_urandom, NULL, _IONBF, 0);
do
@@ -207,7 +207,7 @@ g_rand_new (void)
if (!dev_urandom_exists)
{
gint64 now_us = g_get_real_time ();
seed[0] = now_us / G_USEC_PER_SEC;
seed[0] = (guint32) (now_us / G_USEC_PER_SEC);
seed[1] = now_us % G_USEC_PER_SEC;
seed[2] = getpid ();
seed[3] = getppid ();

View File

@@ -1168,7 +1168,7 @@ g_scanner_peek_next_char (GScanner *scanner)
}
else if (scanner->input_fd >= 0)
{
gint count;
gssize count;
gchar *buffer;
buffer = scanner->buffer;
@@ -1218,7 +1218,7 @@ g_scanner_sync_file_offset (GScanner *scanner)
if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
{
gint buffered;
goffset buffered;
buffered = scanner->text_end - scanner->text;
if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
@@ -1243,7 +1243,7 @@ g_scanner_get_char (GScanner *scanner,
fchar = *(scanner->text++);
else if (scanner->input_fd >= 0)
{
gint count;
gssize count;
gchar *buffer;
buffer = scanner->buffer;
@@ -1694,12 +1694,12 @@ g_scanner_get_token_i (GScanner *scanner,
* by copying between potentially-overlapping union members. */
if (scanner->config->store_int64)
{
gint64 temp = value_p->v_int64;
guint64 temp = value_p->v_int64;
value_p->v_float = temp;
}
else
{
gint temp = value_p->v_int;
gulong temp = value_p->v_int;
value_p->v_float = temp;
}
}

View File

@@ -56,6 +56,7 @@ static const struct {
/* uppercase characters */
{ "EXAMPLE.COM", "example.com", FALSE, FALSE },
{ "\xc3\x89XAMPLE.COM", "xn--xample-9ua.com", TRUE, TRUE },
{ "Ⅷ.com", "viii.com", TRUE, FALSE },
/* unicode that decodes to ascii */
{ "\xe2\x93\x94\xe2\x93\xa7\xe2\x93\x90\xe2\x93\x9c\xe2\x93\x9f\xe2\x93\x9b\xe2\x93\x94.com", "example.com", TRUE, FALSE },

View File

@@ -613,6 +613,20 @@ test_default_handler (void)
g_test_trap_assert_stdout ("*foo-DEBUG*baz*");
}
static void
test_journald_handler (void)
{
/* We cant require that the journal exists on the test system. But if it
* does, we can check that the writer doesnt crash. */
const GLogField fields[] = {
{ "MESSAGE", "This is a test message.", -1 },
{ "MESSAGE_ID", "7187d27ad7f84351b76b7612eef52cd6", -1 },
{ "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 },
};
g_log_writer_journald (G_LOG_LEVEL_DEBUG, fields, G_N_ELEMENTS (fields), NULL);
}
static void
test_fatal_log_mask (void)
{
@@ -1151,6 +1165,7 @@ main (int argc, char *argv[])
g_test_add_func ("/logging/default-handler/subprocess/would-drop-env-systemd", test_default_handler_would_drop_env_systemd);
g_test_add_func ("/logging/default-handler/subprocess/would-drop-robustness", test_default_handler_would_drop_robustness);
g_test_add_func ("/logging/default-handler/subprocess/structured-logging-non-null-terminated-strings", test_default_handler_structured_logging_non_nul_terminated_strings);
g_test_add_func ("/logging/journald-handler", test_journald_handler);
g_test_add_func ("/logging/warnings", test_warnings);
g_test_add_func ("/logging/fatal-log-mask", test_fatal_log_mask);
g_test_add_func ("/logging/set-handler", test_set_handler);

File diff suppressed because it is too large Load Diff

View File

@@ -18,10 +18,15 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <glib/gstdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef G_OS_WIN32
#include <io.h>
#endif
/* GScanner fixture */
typedef struct {
@@ -129,6 +134,169 @@ test_scanner_tokens (ScannerFixture *fix,
return;
}
static void
test_scanner_multiline_comment (void)
{
GScanner *scanner = NULL;
const char buf[] = "/** this\n * is\n * multilined */";
const size_t buflen = strlen (buf);
scanner = g_scanner_new (NULL);
scanner->config->skip_comment_multi = FALSE;
g_scanner_input_text (scanner, buf, buflen);
g_assert_cmpint (g_scanner_cur_token (scanner), ==, G_TOKEN_NONE);
g_scanner_get_next_token (scanner);
g_assert_cmpint (g_scanner_cur_token (scanner), ==, G_TOKEN_COMMENT_MULTI);
g_assert_cmpint (g_scanner_cur_line (scanner), ==, 3);
g_assert_cmpstr (g_scanner_cur_value (scanner).v_comment, ==, "* this\n * is\n * multilined ");
g_assert_cmpint (g_scanner_get_next_token (scanner), ==, G_TOKEN_EOF);
g_scanner_destroy (scanner);
}
static void
test_scanner_int_to_float (void)
{
GScanner *scanner = NULL;
const char buf[] = "4294967295";
const size_t buflen = strlen (buf);
scanner = g_scanner_new (NULL);
scanner->config->int_2_float = TRUE;
g_scanner_input_text (scanner, buf, buflen);
g_assert_cmpint (g_scanner_cur_token (scanner), ==, G_TOKEN_NONE);
g_scanner_get_next_token (scanner);
g_assert_cmpint (g_scanner_cur_token (scanner), ==, G_TOKEN_FLOAT);
g_assert_cmpint (g_scanner_cur_line (scanner), ==, 1);
g_assert_cmpfloat (g_scanner_cur_value (scanner).v_float, ==, 4294967295.0);
g_assert_cmpint (g_scanner_get_next_token (scanner), ==, G_TOKEN_EOF);
g_scanner_destroy (scanner);
}
static void
test_scanner_fd_input (void)
{
/* GScanner does some internal buffering when reading from an FD, reading in
* chunks of `READ_BUFFER_SIZE` (currently 4000B) to an internal buffer. The
* parsing codepaths differ significantly depending on whether theres data in
* the buffer, so all parsing operations have to be tested with chunk
* boundaries on each of the characters in that operation.
*
* Do that by prefixing the buffer were testing with a variable amount of
* whitespace. Whitespace is (by default) ignored by the scanner, so wont
* affect the test other than by letting us choose where in the test string
* the chunk boundaries fall. */
const size_t whitespace_lens[] = { 0, 3998, 3999, 4000, 4001 };
for (size_t i = 0; i < G_N_ELEMENTS (whitespace_lens); i++)
{
GScanner *scanner = NULL;
char *filename = NULL;
int fd = -1;
char *buf = NULL;
const size_t whitespace_len = whitespace_lens[i];
size_t buflen;
const char *buf_suffix = "/** this\n * is\n * multilined */";
buflen = whitespace_len + strlen (buf_suffix) + 1;
buf = g_malloc (buflen);
memset (buf, ' ', whitespace_len);
memcpy (buf + whitespace_len, buf_suffix, strlen (buf_suffix));
buf[buflen - 1] = '\0';
scanner = g_scanner_new (NULL);
scanner->config->skip_comment_multi = FALSE;
filename = g_strdup ("scanner-fd-input-XXXXXX");
fd = g_mkstemp (filename);
g_assert_cmpint (fd, >=, 0);
g_assert_cmpint (write (fd, buf, buflen), ==, buflen);
g_assert_no_errno (lseek (fd, 0, SEEK_SET));
g_scanner_input_file (scanner, fd);
g_assert_cmpint (g_scanner_cur_token (scanner), ==, G_TOKEN_NONE);
g_scanner_get_next_token (scanner);
g_assert_cmpint (g_scanner_cur_token (scanner), ==, G_TOKEN_COMMENT_MULTI);
g_assert_cmpint (g_scanner_cur_line (scanner), ==, 3);
g_assert_cmpstr (g_scanner_cur_value (scanner).v_comment, ==, "* this\n * is\n * multilined ");
g_assert_cmpint (g_scanner_get_next_token (scanner), ==, G_TOKEN_EOF);
g_close (fd, NULL);
g_free (filename);
g_free (buf);
g_scanner_destroy (scanner);
}
}
static void
test_scanner_fd_input_rewind (void)
{
/* GScanner does some internal buffering when reading from an FD, reading in
* chunks of `READ_BUFFER_SIZE` (currently 4000B) to an internal buffer. It
* allows the input FDs file offset to be positioned to match the current
* parsing position. Test that works. */
GScanner *scanner = NULL;
char *filename = NULL;
int fd = -1;
char *buf = NULL;
const size_t whitespace_len = 4000;
size_t buflen;
const char *buf_suffix = "({})";
const struct
{
GTokenType expected_token;
off_t expected_offset; /* offset after consuming @expected_token */
}
tokens_and_offsets[] =
{
{ G_TOKEN_LEFT_PAREN, whitespace_len + 1 },
{ G_TOKEN_LEFT_CURLY, whitespace_len + 2 },
{ G_TOKEN_RIGHT_CURLY, whitespace_len + 3 },
{ G_TOKEN_RIGHT_PAREN, whitespace_len + 4 },
};
buflen = whitespace_len + strlen (buf_suffix) + 1;
buf = g_malloc (buflen);
memset (buf, ' ', whitespace_len);
memcpy (buf + whitespace_len, buf_suffix, strlen (buf_suffix));
buf[buflen - 1] = '\0';
scanner = g_scanner_new (NULL);
filename = g_strdup ("scanner-fd-input-XXXXXX");
fd = g_mkstemp (filename);
g_assert_cmpint (fd, >=, 0);
g_assert_cmpint (write (fd, buf, buflen), ==, buflen);
g_assert_no_errno (lseek (fd, 0, SEEK_SET));
g_scanner_input_file (scanner, fd);
g_assert_cmpint (g_scanner_cur_token (scanner), ==, G_TOKEN_NONE);
for (size_t i = 0; i < G_N_ELEMENTS (tokens_and_offsets); i++)
{
g_scanner_get_next_token (scanner);
g_scanner_sync_file_offset (scanner);
g_assert_cmpint (g_scanner_cur_token (scanner), ==, tokens_and_offsets[i].expected_token);
g_assert_cmpint (lseek (fd, 0, SEEK_CUR), ==, tokens_and_offsets[i].expected_offset);
}
g_assert_cmpint (g_scanner_get_next_token (scanner), ==, G_TOKEN_EOF);
g_close (fd, NULL);
g_free (filename);
g_free (buf);
g_scanner_destroy (scanner);
}
int
main (int argc,
char *argv[])
@@ -139,6 +307,10 @@ main (int argc,
g_test_add ("/scanner/error", ScannerFixture, 0, scanner_fixture_setup, test_scanner_error, scanner_fixture_teardown);
g_test_add ("/scanner/symbols", ScannerFixture, 0, scanner_fixture_setup, test_scanner_symbols, scanner_fixture_teardown);
g_test_add ("/scanner/tokens", ScannerFixture, 0, scanner_fixture_setup, test_scanner_tokens, scanner_fixture_teardown);
g_test_add_func ("/scanner/multiline-comment", test_scanner_multiline_comment);
g_test_add_func ("/scanner/int-to-float", test_scanner_int_to_float);
g_test_add_func ("/scanner/fd-input", test_scanner_fd_input);
g_test_add_func ("/scanner/fd-input/rewind", test_scanner_fd_input_rewind);
return g_test_run();
}