Bug 103061 - hsearch_r needs better example code
Summary: hsearch_r needs better example code
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: man-pages
Version: 9
Hardware: All
OS: Linux
medium
low
Target Milestone: ---
Assignee: Eido Inoue
QA Contact: Ben Levenson
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2003-08-26 07:06 UTC by P Fudd
Modified: 2007-04-18 16:57 UTC (History)
0 users

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2004-04-16 14:13:22 UTC
Embargoed:


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

Description P Fudd 2003-08-26 07:06:02 UTC
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):
man-pages-1.53-3

How reproducible:
Always

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 07:10:48 UTC
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 14:13:22 UTC
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.