Skip to content

GTree: Handle node counter overflow and return it as an unsigned value

Maciej S. Szmigiero requested to merge (removed):gtree-overflow into main

Currently, when adding new elements to GTree we blindly increment the node counter, which is only of guint size (so 32-bit even on 64-bit Unix platforms).

This is even more problematic because the only way to check whether particular GTree is empty is to check whether its node count is zero. This will obviously give wrong answer if this counter overflows.

Let's fix this by adding an appropriate check when adding a new tree node.

For the recently added g_tree_{insert,replace}_node () API we can simply return NULL in such case.

However, the older g_tree_{insert,replace} () API doesn't have any ability to return an error so for them we follow the example of g_ptr_array_extend () and g_ptr_array_set_size () by calling g_error () when this happens.

In addition to the above overflow issue, g_tree_nnodes () was returning the counter value as a signed integer type (gint), which meant that the returned size was negative if the container held more than G_MAXINT elements.

Change the returned type to guint instead to match the GTree internal counter type. This only changes the behavior for sizes above G_MAXINT and these were returned incorrectly (negative) anyway.

Merge request reports