From peterjeremy@optushome.com.au  Wed Mar 12 01:08:29 2003
Return-Path: <peterjeremy@optushome.com.au>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 35B9137B401
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 12 Mar 2003 01:08:29 -0800 (PST)
Received: from cirb503493.alcatel.com.au (c18609.belrs1.nsw.optusnet.com.au [210.49.80.204])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 8E96043F93
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 12 Mar 2003 01:08:26 -0800 (PST)
	(envelope-from peterjeremy@optushome.com.au)
Received: from cirb503493.alcatel.com.au (localhost.alcatel.com.au [127.0.0.1])
	by cirb503493.alcatel.com.au (8.12.6/8.12.5) with ESMTP id h2C98OiM006832;
	Wed, 12 Mar 2003 20:08:25 +1100 (EST)
	(envelope-from jeremyp@cirb503493.alcatel.com.au)
Received: (from jeremyp@localhost)
	by cirb503493.alcatel.com.au (8.12.6/8.12.5/Submit) id h2C98N5Z006831;
	Wed, 12 Mar 2003 20:08:23 +1100 (EST)
Message-Id: <200303120908.h2C98N5Z006831@cirb503493.alcatel.com.au>
Date: Wed, 12 Mar 2003 20:08:23 +1100 (EST)
From: Peter Jeremy <peterjeremy@optushome.com.au>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [patch] hcreate(3) man page unclear
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         49951
>Category:       bin
>Synopsis:       [patch] hcreate(3) man page unclear
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    dwmalone
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          doc-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Mar 12 01:10:01 PST 2003
>Closed-Date:    Sat Apr 05 05:53:38 PST 2003
>Last-Modified:  Sat Apr 05 05:53:38 PST 2003
>Originator:     Peter Jeremy
>Release:        FreeBSD 4.6-STABLE i386
>Organization:
n/a
>Environment:
System: FreeBSD cirb503493.alcatel.com.au 4.6-STABLE FreeBSD 4.6-STABLE #0: Mon Jul 22 21:45:58 EST 2002 root@cirb503493.alcatel.com.au:/usr/obj/usr/src/sys/pj1592 i386

	The man page is identical in 5-CURRENT

>Description:
	The description of hdestroy() states that the search table is
	destroyed but is vague about the disposition of the table elements.
	The actual implementation of hdestroy() calls free(ie->ent.key)
	but not on ie->ent.data.  This implies that the key (but not the
	data) passed to hsearch(..., ENTER) should be malloc()d.

	The sample program is also misleading.  Whilst it is safe as is,
	it is incompatible with hdestroy() and could lead the user to
	conclude that allocating keys from an static buffer is appropriate
	in all cases (ie, even when hdestroy() is called).

	The attached patch explicitly states that the key (but not the
	data) is free()d by hdestroy() and should therefore be malloc()d.
	The sample program is updated to passed heap-allocated keys
	and call hdestroy().

>How-To-Repeat:
	Code inspection.  Adding an hdestroy() to the sample program
	should cause it to blow up.
>Fix:

Index: hcreate.3
===================================================================
RCS file: /usr/ncvs/src/lib/libc/stdlib/hcreate.3,v
retrieving revision 1.2.2.1
diff -u -r1.2.2.1 hcreate.3
--- hcreate.3	2 Oct 2001 11:22:56 -0000	1.2.2.1
+++ hcreate.3	12 Mar 2003 08:31:55 -0000
@@ -44,6 +44,12 @@
 After the call to
 .Fn hdestroy ,
 the data can no longer be considered accessible.
+The
+.Fn hdestroy
+function calls
+.Xr free 3
+for each comparison key in the search table
+but not the data item associated with the key.
 .Pp
 The
 .Fn hsearch
@@ -88,6 +94,20 @@
 indicated by the return of a
 .Dv NULL
 pointer.
+.Pp
+The comparison key (passed to
+.Fn hsearch
+as
+.Fa item.key )
+must be allocated using
+.Xr malloc 3
+if
+.Fa action
+is
+.Dv ENTER
+and
+.Fn hdestroy
+is called.
 .Sh RETURN VALUES
 The
 .Fn hcreate
@@ -132,6 +152,7 @@
 #include <stdio.h>
 #include <search.h>
 #include <string.h>
+#include <stdlib.h>
 
 struct info {			/* This is the info stored in the table */
 	int age, room;		/* other than the key. */
@@ -142,9 +163,8 @@
 int
 main(void)
 {
-	char string_space[NUM_EMPL*20]; /* Space to store strings. */
+	char str[BUFSIZ]; /* Space to read string */
 	struct info info_space[NUM_EMPL]; /* Space to store employee info. */
-	char *str_ptr = string_space; /* Next space in string_space. */
 	struct info *info_ptr = info_space; /* Next space in info_space. */
 	ENTRY item;
 	ENTRY *found_item; /* Name to look for in table. */
@@ -154,12 +174,11 @@
 	/* Create table; no error checking is performed. */
 	(void) hcreate(NUM_EMPL);
 
-	while (scanf("%s%d%d", str_ptr, &info_ptr->age,
+	while (scanf("%s%d%d", str, &info_ptr->age,
 	    &info_ptr->room) != EOF && i++ < NUM_EMPL) {
 		/* Put information in structure, and structure in item. */
-		item.key = str_ptr;
+		item.key = strdup(str);
 		item.data = info_ptr;
-		str_ptr += strlen(str_ptr) + 1;
 		info_ptr++;
 		/* Put item into table. */
 		(void) hsearch(item, ENTER);
@@ -177,6 +196,7 @@
 		} else
 			(void)printf("no such employee %s\en", name_to_find);
 	}
+	hdestroy();
 	return 0;
 }
 .Ed
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->dwmalone 
Responsible-Changed-By: dwmalone 
Responsible-Changed-When: Wed Mar 12 06:18:30 PST 2003 
Responsible-Changed-Why:  
I've committed the patch to -current. Take the PR as a reminder to MFC it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=49951 
State-Changed-From-To: open->closed 
State-Changed-By: dwmalone 
State-Changed-When: Sat Apr 5 05:53:14 PST 2003 
State-Changed-Why:  
Patch committed to -current and -stable. Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=49951 
>Unformatted:
