[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 5/6] scm_i_utf8_string_hash: don't overrun when len is zero
From: |
Rob Browning |
Subject: |
[PATCH 5/6] scm_i_utf8_string_hash: don't overrun when len is zero |
Date: |
Mon, 1 Jul 2024 21:45:19 -0500 |
When the length is zero, the previous code would include the byte after
the end of the string in the hash. Fix that (the wide an narrow hashers
also guard against it via "case 0"), and while we're there, switch to
u8_mbtouc since the unsafe variant is now the same (see the info pages),
and don't bother mutating length for the trailing bytes, since we don't
need to.
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
---
libguile/hash.c | 28 +++++++++++++++-------------
1 file changed, 15 insertions(+), 13 deletions(-)
diff --git a/libguile/hash.c b/libguile/hash.c
index a038a11bf..d92f60df8 100644
--- a/libguile/hash.c
+++ b/libguile/hash.c
@@ -195,32 +195,34 @@ scm_i_utf8_string_hash (const char *str, size_t len)
/* Handle most of the key. */
while (length > 3)
{
- ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
+ ustr += u8_mbtouc (&u32, ustr, end - ustr);
a += u32;
- ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
+ ustr += u8_mbtouc (&u32, ustr, end - ustr);
b += u32;
- ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
+ ustr += u8_mbtouc (&u32, ustr, end - ustr);
c += u32;
mix (a, b, c);
length -= 3;
}
/* Handle the last 3 elements's. */
- ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
- a += u32;
- if (--length)
+ if (length)
{
- ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
- b += u32;
- if (--length)
+ ustr += u8_mbtouc (&u32, ustr, end - ustr);
+ a += u32;
+ if (length > 1)
{
- ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
- c += u32;
+ ustr += u8_mbtouc (&u32, ustr, end - ustr);
+ b += u32;
+ if (length > 2)
+ {
+ ustr += u8_mbtouc (&u32, ustr, end - ustr);
+ c += u32;
+ }
}
+ final (a, b, c);
}
- final (a, b, c);
-
if (sizeof (unsigned long) == 8)
ret = (((unsigned long) c) << 32) | b;
else
--
2.43.0
- [PATCH 0/6] A handful of post 3.0.10 fixups, Rob Browning, 2024/07/01
- [PATCH 3/6] Add guile-procedures.txt to BUILT_SOURCES, Rob Browning, 2024/07/01
- [PATCH 1/6] define-meta-command: mention effects of a missing category, Rob Browning, 2024/07/01
- [PATCH 4/6] test-hashing: support 32-bit, Rob Browning, 2024/07/01
- [PATCH 2/6] Ensure GUILE-VERSION changes propagate to .version and Makefiles, Rob Browning, 2024/07/01
- [PATCH 5/6] scm_i_utf8_string_hash: don't overrun when len is zero,
Rob Browning <=
- [PATCH 6/6] scm_i_utf8_string_hash: optimize ASCII, Rob Browning, 2024/07/01