Issue of the Implementation # S0791


Regression in hsearch_r(): Segmentation fault over internal invariant violation

Detailed Description

The modifications of hsearch_r() [1] in glibc-2.9 leads to segmentation fault in some circumstances as a consequence of internal invariant violation.

Description of the hsearch_r() in glibc sources reads:

We use an trick to speed up the lookup. The table is created by hcreate with one more element available. This enables us to use the index zero special. This index will never be used because we store the first hash index in the field used where zero means not used. Every other value means used. The used field can be used as a first fast comparison for equality of the stored and the parameter value. This helps to prevent unnecessary expensive calls of strcmp.

But the new version stores hash value in the 'used' field instead of table index, which can be zero. As a result this invariant can be violated and dereference of uninitialized memory may happen. The example below demonstrates this issue.


Problem location(s) in the standard

Linux Standard Base Core Specification 3.1, Chapter 13. Base Libraries, 13.3. Interfaces for libc, 13.3.17. Standard Library, Interfaces for Standard Library, Table 13-22. libc - Standard Library Function Interfaces, descriptions of hcreate(), hsearch() and hdestroy() functions.


#include <stdio.h>
#include <search.h>

int main(void)
	char * key[] = { "key2", "gda|yvsp", "" };
	char * data = "data";
	int i;
	ENTRY item; = data;
	for(i = 0; i < 3; i++){
		item.key = key[i];
		printf("try to insert '%s'='%s'\n", item.key, (char *);
		hsearch(item, ENTER);
	return 0;

Possible solutions

Quick fix is:

   hval = len;
   count = len;
   while (count-- > 0)
       hval <<= 4;
       hval += item.key[count];
+  /* Make sure hash value is not zero */
+  if (!hval) hval=1; 
   /* First hash function: simply take the modul but prevent zero. */
   idx = hval % htab->size + 1;
But if zero table index is not used special in the new scheme, why do we need to allocate extra element? So, long term solution requires some more investigation.


glibc 2.9


Red Hat Bugzilla, 10100


Fixed in glibc-2.10