hash resize broken for large sizes
Submitted by Deniz Yuret
Link to original bug (#694501)
Description
_GHashTable.size is a 32 bit signed integer. The resize policy is to double the table size. This is done updating size using a left shift (1 << shift), then allocating memory using g_new0. When shift reaches 31, size becomes -2147483648, this is then interpreted as a 64 bit integer 18446744071562067968 and malloc gives the following cryptic error:
(process:47597): GLib-ERROR **: /build/buildd/glib2.0-2.32.3/./glib/gmem.c:382: overflow allocating 18446744071562067968*8 bytes Trace/breakpoint trap (core dumped)
This condition should be detected and a more accurate error message should be given. Ideally, in the longer term, with todays 64 bit computers and large RAM sizes, larger hashes and arrays should be allowed.
#0 g_logv (log_domain=0x7ffff7b6b74e "GLib", log_level=<optimized out>,
format=0x7ffff7b748b8 "%s: overflow allocating %lu*%lu bytes",
args1=0x7ffffff2e5b8)
at /build/buildd/glib2.0-2.32.3/./glib/gmessages.c:765
#1 0x00007ffff7b341b2 in g_log (log_domain=<optimized out>,
log_level=<optimized out>,
format=<optimized out>) at
/build/buildd/glib2.0-2.32.3/./glib/gmessages.c:792
#2 0x00007ffff7b32d11 in g_malloc0_n (n_blocks=<optimized out>,
n_block_bytes=<optimized out>)
at /build/buildd/glib2.0-2.32.3/./glib/gmem.c:381
#3 0x00007ffff7b1c241 in g_hash_table_resize (hash_table=0x607520)
at /build/buildd/glib2.0-2.32.3/./glib/ghash.c:570
#4 0x00007ffff7b1c5eb in g_hash_table_maybe_resize (hash_table=0x607520)
at /build/buildd/glib2.0-2.32.3/./glib/ghash.c:630
#5 g_hash_table_insert_node (hash_table=0x607520,
node_index=836379211, key_hash=1910120997,
key=0x5485e45e0, value=<optimized out>, keep_new_key=<optimized
out>, reusing_key=0)
at /build/buildd/glib2.0-2.32.3/./glib/ghash.c:907
#6 0x00007ffff7b1c98a in g_hash_table_insert_internal
(hash_table=0x607520, key=0x5485e45e0,
value=0x5485e45f4, keep_new_key=0) at
/build/buildd/glib2.0-2.32.3/./glib/ghash.c:1153
#7 0x0000000000403629 in lmheap_count ()
#8 0x00007ffff7b1d4e0 in g_hash_table_foreach (hash_table=0x607400,
func=0x403560 <lmheap_count>,
user_data=0x607520) at /build/buildd/glib2.0-2.32.3/./glib/ghash.c:1524
#9 0x00000000004032b2 in lmheap_init ()
#10 0x0000000000402664 in fastsubs ()
#11 0x0000000000401878 in main ()
Version: 2.54.x