Sat Mar 12 08:26:01 2022 UTC ()
hcreate(3): use reallocarr instead of malloc(x * y)


(nia)
diff -r1.10 -r1.11 src/lib/libc/stdlib/hcreate.c

cvs diff -r1.10 -r1.11 src/lib/libc/stdlib/hcreate.c (expand / switch to unified diff)

--- src/lib/libc/stdlib/hcreate.c 2014/07/20 20:17:21 1.10
+++ src/lib/libc/stdlib/hcreate.c 2022/03/12 08:26:01 1.11
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: hcreate.c,v 1.10 2014/07/20 20:17:21 christos Exp $ */ 1/* $NetBSD: hcreate.c,v 1.11 2022/03/12 08:26:01 nia Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2001 Christopher G. Demetriou 4 * Copyright (c) 2001 Christopher G. Demetriou
5 * All rights reserved. 5 * All rights reserved.
6 *  6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -33,27 +33,27 @@ @@ -33,27 +33,27 @@
33 * hcreate() / hsearch() / hdestroy() 33 * hcreate() / hsearch() / hdestroy()
34 * 34 *
35 * SysV/XPG4 hash table functions. 35 * SysV/XPG4 hash table functions.
36 * 36 *
37 * Implementation done based on NetBSD manual page and Solaris manual page, 37 * Implementation done based on NetBSD manual page and Solaris manual page,
38 * plus my own personal experience about how they're supposed to work. 38 * plus my own personal experience about how they're supposed to work.
39 * 39 *
40 * I tried to look at Knuth (as cited by the Solaris manual page), but 40 * I tried to look at Knuth (as cited by the Solaris manual page), but
41 * nobody had a copy in the office, so... 41 * nobody had a copy in the office, so...
42 */ 42 */
43 43
44#include <sys/cdefs.h> 44#include <sys/cdefs.h>
45#if defined(LIBC_SCCS) && !defined(lint) 45#if defined(LIBC_SCCS) && !defined(lint)
46__RCSID("$NetBSD: hcreate.c,v 1.10 2014/07/20 20:17:21 christos Exp $"); 46__RCSID("$NetBSD: hcreate.c,v 1.11 2022/03/12 08:26:01 nia Exp $");
47#endif /* LIBC_SCCS and not lint */ 47#endif /* LIBC_SCCS and not lint */
48 48
49#if !defined(lint) 49#if !defined(lint)
50__COPYRIGHT("@(#) Copyright (c) 2001\ 50__COPYRIGHT("@(#) Copyright (c) 2001\
51 Christopher G. Demetriou. All rights reserved."); 51 Christopher G. Demetriou. All rights reserved.");
52#endif /* not lint */ 52#endif /* not lint */
53 53
54#include "namespace.h" 54#include "namespace.h"
55#include <assert.h> 55#include <assert.h>
56#include <errno.h> 56#include <errno.h>
57#include <inttypes.h> 57#include <inttypes.h>
58#include <search.h> 58#include <search.h>
59#include <stdlib.h> 59#include <stdlib.h>
@@ -115,28 +115,28 @@ hcreate_r(size_t nel, struct hsearch_dat @@ -115,28 +115,28 @@ hcreate_r(size_t nel, struct hsearch_dat
115 nel = MAX_BUCKETS; 115 nel = MAX_BUCKETS;
116 116
117 /* If it's is not a power of two in size, round up. */ 117 /* If it's is not a power of two in size, round up. */
118 if ((nel & (nel - 1)) != 0) { 118 if ((nel & (nel - 1)) != 0) {
119 for (p2 = 0; nel != 0; p2++) 119 for (p2 = 0; nel != 0; p2++)
120 nel >>= 1; 120 nel >>= 1;
121 _DIAGASSERT(p2 <= MAX_BUCKETS_LG2); 121 _DIAGASSERT(p2 <= MAX_BUCKETS_LG2);
122 nel = 1 << p2; 122 nel = 1 << p2;
123 } 123 }
124  124
125 /* Allocate the table. */ 125 /* Allocate the table. */
126 head->size = nel; 126 head->size = nel;
127 head->filled = 0; 127 head->filled = 0;
128 p = malloc(nel * sizeof table[0]); 128 p = NULL;
129 if (p == NULL) { 129 if (reallocarr(&p, nel, sizeof(table[0])) != 0) {
130 errno = ENOMEM; 130 errno = ENOMEM;
131 return 0; 131 return 0;
132 } 132 }
133 head->table = p; 133 head->table = p;
134 table = p; 134 table = p;
135 135
136 /* Initialize it. */ 136 /* Initialize it. */
137 for (idx = 0; idx < nel; idx++) 137 for (idx = 0; idx < nel; idx++)
138 SLIST_INIT(&table[idx]); 138 SLIST_INIT(&table[idx]);
139 139
140 return 1; 140 return 1;
141} 141}
142 142