Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing UB in hsearch #471

Closed
wants to merge 1 commit into from
Closed

Fixing UB in hsearch #471

wants to merge 1 commit into from

Conversation

anon767
Copy link

@anon767 anon767 commented Jan 16, 2023

Referenced by issue: #470

Referenced by issue: #470
@rofl0r
Copy link
Contributor

rofl0r commented Feb 1, 2023

i'd prefer the following fix

diff --git a/src/hsearch.c b/src/hsearch.c
index be0434c..dfe1404 100644
--- a/src/hsearch.c
+++ b/src/hsearch.c
@@ -80,9 +80,10 @@ static int resize(struct htab *htab, size_t nel)
 {
 	size_t newsize;
 	size_t i, j;
+	size_t oldmask = htab->mask;
 	struct elem *e, *newe;
 	struct elem *oldtab = htab->elems;
-	struct elem *oldend = htab->elems + htab->mask + 1;
+	struct elem *oldend;
 
 	if (nel > MAXSIZE)
 		nel = MAXSIZE;
@@ -95,6 +96,8 @@ static int resize(struct htab *htab, size_t nel)
 	htab->mask = newsize - 1;
 	if (!oldtab)
 		return 1;
+
+	oldend = oldtab + oldmask + 1;
 	for (e = oldtab; e < oldend; e++)
 		if (e->item.key) {
 			for (i=e->hash,j=1; ; i+=j++) {

your fix makes a pointer out of some random integers even though there's no pointer (in this case NULL) added to it. can you give it a try ?

@anon767
Copy link
Author

anon767 commented Feb 1, 2023

yes perfect, that does the trick

@rofl0r rofl0r closed this in 6ffd9af Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants