From owner-freebsd-bugs Wed Mar 12 1:10: 7 2003 Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 44C7537B404 for ; Wed, 12 Mar 2003 01:10:03 -0800 (PST) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id D5ADC43FCB for ; Wed, 12 Mar 2003 01:10:01 -0800 (PST) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.6/8.12.6) with ESMTP id h2C9A1NS043754 for ; Wed, 12 Mar 2003 01:10:01 -0800 (PST) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.6/8.12.6/Submit) id h2C9A12a043753; Wed, 12 Mar 2003 01:10:01 -0800 (PST) Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 35B9137B401 for ; 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 ; 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 To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Subject: bin/49951: [patch] hcreate(3) man page unclear Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org >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 #include #include +#include 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