Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 12 Mar 2003 20:08:23 +1100 (EST)
From:      Peter Jeremy <peterjeremy@optushome.com.au>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   bin/49951: [patch] hcreate(3) man page unclear
Message-ID:  <200303120908.h2C98N5Z006831@cirb503493.alcatel.com.au>

next in thread | raw e-mail | index | archive | help

>Number:         49951
>Category:       bin
>Synopsis:       [patch] hcreate(3) man page unclear
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          doc-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Mar 12 01:10:01 PST 2003
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200303120908.h2C98N5Z006831>