gdataset: avoid initializing "out_idx" if datalist_find() fails

In some sense, it might be desirable to always initialize out parameters
to something.

However, this fallback index G_MAXUINT32 is not really useful for any
caller. That is, no caller can blindly access the index without checking
whether datalist_find() found (and returned) an entry.

Also, a caller cannot check for whether a entry was found via `if (idx
== G_MAXUINT32)`, because that theoretically is also a valid index.

The only sensible way to use datalist_find() is by accessing "idx"
if-and-only-if an entry was found. And all callers did that.

There is no point for initializing the value.

Uninitialized values can be even preferable over a non-useful
initialization to G_MAXUINT32, because if there is a buggy access them
under valgrind, there will be at least a warning.
This commit is contained in:
Thomas Haller 2025-02-21 10:11:14 +01:00
parent 3024d9b47e
commit 70623887ad

View File

@ -464,8 +464,8 @@ datalist_find (GData *data, GQuark key_id, guint32 *out_idx)
GHashTable *index;
guint32 i;
if (!data)
goto out_not_found;
if (G_UNLIKELY (!data))
return NULL;
index = datalist_index_get (data);
@ -483,12 +483,12 @@ datalist_find (GData *data, GQuark key_id, guint32 *out_idx)
}
}
goto out_not_found;
return NULL;
}
data_elt = g_hash_table_lookup (index, &key_id);
if (!data_elt)
goto out_not_found;
return NULL;
#if G_ENABLE_DEBUG
g_assert (data_elt >= data->data && data_elt < &data->data[data->len]);
@ -497,11 +497,6 @@ datalist_find (GData *data, GQuark key_id, guint32 *out_idx)
if (out_idx)
*out_idx = (data_elt - data->data);
return data_elt;
out_not_found:
if (out_idx)
*out_idx = G_MAXUINT32;
return NULL;
}
/**