public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* ld: Avoid overflows in string merging
@ 2023-11-07 16:51 Michael Matz
  2023-11-08  7:30 ` Jan Beulich
  0 siblings, 1 reply; 8+ messages in thread
From: Michael Matz @ 2023-11-07 16:51 UTC (permalink / raw)
  To: binutils

as the bug report shows we had an overflow in the test if
hash table resizing is needed.  Reorder the expression to avoid
that.  There's still a bug somewhere in gracefully handling
failure in resizing (e.g. out of memory), but this pushes the
boundary for that occurring somewhen into the future and
immediately helps the reporter.

    bfd/

    PR ld/31009
    * merge.c (sec_merge_maybe_resize): Avoid overflow in expression.
    (sec_merge_hash_insert): Adjust assert.
---

regtested on many targets, okay for master?


 bfd/merge.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/bfd/merge.c b/bfd/merge.c
index 722e6659486..4aa2f838679 100644
--- a/bfd/merge.c
+++ b/bfd/merge.c
@@ -167,7 +167,7 @@ static bool
 sec_merge_maybe_resize (struct sec_merge_hash *table, unsigned added)
 {
   struct bfd_hash_table *bfdtab = &table->table;
-  if (bfdtab->count + added > table->nbuckets * 2 / 3)
+  if (bfdtab->count + added > table->nbuckets / 3 * 2)
     {
       unsigned i;
       unsigned long newnb = table->nbuckets * 2;
@@ -175,7 +175,7 @@ sec_merge_maybe_resize (struct sec_merge_hash *table, unsigned added)
       uint64_t *newl;
       unsigned long alloc;
 
-      while (bfdtab->count + added > newnb * 2 / 3)
+      while (bfdtab->count + added > newnb / 3 * 2)
 	{
 	  newnb *= 2;
 	  if (!newnb)
@@ -240,7 +240,7 @@ sec_merge_hash_insert (struct sec_merge_hash *table,
   hashp->u.suffix = NULL;
   hashp->next = NULL;
   // We must not need resizing, otherwise _index is wrong
-  BFD_ASSERT (bfdtab->count + 1 <= table->nbuckets * 2 / 3);
+  BFD_ASSERT (bfdtab->count + 1 <= table->nbuckets / 3 * 2);
   bfdtab->count++;
   table->key_lens[_index] = (hash << 32) | (uint32_t)len;
   table->values[_index] = hashp;
-- 
2.42.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-11-10 10:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-07 16:51 ld: Avoid overflows in string merging Michael Matz
2023-11-08  7:30 ` Jan Beulich
2023-11-08 14:31   ` Michael Matz
2023-11-08 16:16     ` Michael Matz
2023-11-09  7:55       ` Jan Beulich
2023-11-09  7:52     ` Jan Beulich
2023-11-09 16:27       ` Michael Matz
2023-11-10 10:10         ` Jan Beulich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).