Bug 103061 - hsearch_r needs better example code
hsearch_r needs better example code
Product: Red Hat Linux
Classification: Retired
Component: man-pages (Show other bugs)
All Linux
medium Severity low
: ---
: ---
Assigned To: Eido Inoue
Ben Levenson
Depends On:
  Show dependency treegraph
Reported: 2003-08-26 03:06 EDT by P Fudd
Modified: 2007-04-18 12:57 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2004-04-16 10:13:22 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)
example program for inclusion in the man page (1.41 KB, text/plain)
2003-08-26 03:10 EDT, P Fudd
no flags Details

  None (edit)
Description P Fudd 2003-08-26 03:06:02 EDT
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225

Description of problem:
I'm converting a perl program to C, and wanted to use some hash tables.  I found
the hcreate_r, hsearch_r, hdestroy_r functions in the man pages.  

There is an example on how to use the old (non-reentrant) versions of these
functions, but when I tried to upgrade it to use the reentrant functions the
test program died a horrible death, mysteriously.

After much debugging, I found the problem and the solution.

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. Cut-n-paste the example from the man page
2. Define a 'struct hsearch_data foo;' in main()
3. Convert the old functions to the new functions.
4. Compile and run.


Actual Results:  Crash with 'Segmentation fault' or 'Floating point error'.

Expected Results:  Should have run without an error.

Additional info:

It turns out that the example program doesn't check the return value of the
hcreate call.  If it fails to create the hash table, a divide-by-zero error will
occur in the hsearch function.

The other problem is that I didn't zero out the hsearch_data before calling
hcreate_r, which causes hcreate_r to assume that a table was already created and
so it refuses to clobber an existing hash table.

I'm including an example for the new functions.
Comment 1 P Fudd 2003-08-26 03:10:48 EDT
Created attachment 93928 [details]
example program for inclusion in the man page

This code shows a working example for hcreate_r/hsearch_r.  It is almost
identical to the hcreate/hsearch example, except that it clears a data
structure before use, and it checks to see if the hash table was created.
Comment 2 Eido Inoue 2004-04-16 10:13:22 EDT
h_search and Co. is not in the latest man-pages package

Note You need to log in before you can comment on or make changes to this bug.