• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GNU Binutils with patches for OS216


Commit MetaInfo

修订版5f8199783433ce8f5b924c2640953b21b5d71620 (tree)
时间2019-09-23 22:12:54
作者Nick Alcock <nick.alcock@orac...>
CommiterNick Alcock

Log Message

libctf: don't leak hash keys or values on value replacement

When a ctf_dynhash_insert() finds a slot already existing, it should
call the key and value free functions on the existing key and value and
move the passed-in key into place, so that the lifetime rules for hash
keys are always the same no matter whether the key existed or not but
neither are the keys or values leaked.

New in v3.

libctf/
* ctf-hash.c (ctf_hashtab_insert): Pass in the key and value
freeing functions: if set, free the key and value if the slot
already exists. Always reassign the key.

(ctf_dynhash_insert): Adjust call appropriately.
(ctf_hash_insert_type): Likewise.

更改概述

差异

--- a/libctf/ChangeLog
+++ b/libctf/ChangeLog
@@ -1,3 +1,11 @@
1+2019-07-30 Nick Alcock <nick.alcock@oracle.com>
2+
3+ * ctf-hash.c (ctf_hashtab_insert): Pass in the key and value
4+ freeing functions: if set, free the key and value if the slot
5+ already exists. Always reassign the key.
6+ (ctf_dynhash_insert): Adjust call appropriately.
7+ (ctf_hash_insert_type): Likewise.
8+
19 2019-08-03 Nick Alcock <nick.alcock@oracle.com>
210
311 * ctf-create.c (ctf_add_type): Look up and use the forwarded-to
--- a/libctf/ctf-hash.c
+++ b/libctf/ctf-hash.c
@@ -152,7 +152,9 @@ ctf_hashtab_lookup (struct htab *htab, const void *key, enum insert_option inser
152152 }
153153
154154 static ctf_helem_t *
155-ctf_hashtab_insert (struct htab *htab, void *key, void *value)
155+ctf_hashtab_insert (struct htab *htab, void *key, void *value,
156+ ctf_hash_free_fun key_free,
157+ ctf_hash_free_fun value_free)
156158 {
157159 ctf_helem_t **slot;
158160
@@ -169,8 +171,15 @@ ctf_hashtab_insert (struct htab *htab, void *key, void *value)
169171 *slot = malloc (sizeof (ctf_helem_t));
170172 if (!*slot)
171173 return NULL;
172- (*slot)->key = key;
173174 }
175+ else
176+ {
177+ if (key_free)
178+ key_free ((*slot)->key);
179+ if (value_free)
180+ value_free ((*slot)->value);
181+ }
182+ (*slot)->key = key;
174183 (*slot)->value = value;
175184 return *slot;
176185 }
@@ -180,7 +189,8 @@ ctf_dynhash_insert (ctf_dynhash_t *hp, void *key, void *value)
180189 {
181190 ctf_helem_t *slot;
182191
183- slot = ctf_hashtab_insert (hp->htab, key, value);
192+ slot = ctf_hashtab_insert (hp->htab, key, value,
193+ hp->key_free, hp->value_free);
184194
185195 if (!slot)
186196 return errno;
@@ -317,7 +327,7 @@ ctf_hash_insert_type (ctf_hash_t *hp, ctf_file_t *fp, uint32_t type,
317327 return 0; /* Just ignore empty strings on behalf of caller. */
318328
319329 if (ctf_hashtab_insert ((struct htab *) hp, (char *) str,
320- (void *) (ptrdiff_t) type) != NULL)
330+ (void *) (ptrdiff_t) type, NULL, NULL) != NULL)
321331 return 0;
322332 return errno;
323333 }