girepository: Fix -Wsign-conversion warnings with string arithmetic

There are a few `g_strndup()` calls which use a length calculated from
the return value of `strchr()` minus the original string. That’s fine,
as long as `strchr()` doesn’t return `NULL`. Add some asserts to ensure
that.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
This commit is contained in:
Philip Withnall
2025-04-11 22:05:42 +01:00
parent f44511bc54
commit 8ab78a66a4
2 changed files with 11 additions and 4 deletions

View File

@@ -607,7 +607,8 @@ load_dependencies_recurse (GIRepository *repository,
const char *dependency_version;
last_dash = strrchr (dependency, '-');
dependency_namespace = g_strndup (dependency, last_dash - dependency);
g_assert (last_dash != NULL); /* get_typelib_dependencies() guarantees this */
dependency_namespace = g_strndup (dependency, (size_t) (last_dash - dependency));
dependency_version = last_dash+1;
if (!gi_repository_require (repository, dependency_namespace, dependency_version,
@@ -785,7 +786,8 @@ get_typelib_dependencies_transitive (GIRepository *repository,
/* Recurse for this namespace. */
last_dash = strrchr (dependency, '-');
dependency_namespace = g_strndup (dependency, last_dash - dependency);
g_assert (last_dash != NULL); /* get_typelib_dependencies() guarantees this */
dependency_namespace = g_strndup (dependency, (size_t) (last_dash - dependency));
typelib = get_registered (repository, dependency_namespace, NULL);
g_return_if_fail (typelib != NULL);
@@ -1742,7 +1744,12 @@ enumerate_namespace_versions (const char *namespace,
name_end = strrchr (entry, '.');
last_dash = strrchr (entry, '-');
version = g_strndup (last_dash+1, name_end-(last_dash+1));
/* These are guaranteed by the suffix and prefix checks above: */
g_assert (name_end != NULL);
g_assert (last_dash != NULL);
version = g_strndup (last_dash + 1, (size_t) (name_end - (last_dash + 1u)));
if (!parse_version (version, &major, &minor))
{
g_free (version);

View File

@@ -707,7 +707,7 @@ parse_type_internal (GIIrModule *module,
*str == ':')
(str)++;
type->giinterface = g_strndup (start, str - start);
type->giinterface = g_strndup (start, (size_t) (str - start));
}
if (next)