@@ -194,7 +194,7 @@ zhash_destroy (zhash_t **self_p)
194194 item_t * cur_item = self -> items [index ];
195195 while (cur_item ) {
196196 item_t * next_item = cur_item -> next ;
197- s_item_destroy (self , cur_item , TRUE );
197+ s_item_destroy (self , cur_item , true );
198198 cur_item = next_item ;
199199 }
200200 }
@@ -246,6 +246,10 @@ zhash_insert (zhash_t *self, const char *key, void *value)
246246 self -> items = new_items ;
247247 self -> limit = new_limit ;
248248 }
249+ // If necessary, take duplicate of item (string) value
250+ if (self -> autofree )
251+ value = strdup ((char * ) value );
252+
249253 return s_item_insert (self , key , value )? 0 : -1 ;
250254}
251255
@@ -268,6 +272,10 @@ zhash_update (zhash_t *self, const char *key, void *value)
268272 else
269273 if (self -> autofree )
270274 free (item -> value );
275+
276+ // If necessary, take duplicate of item (string) value
277+ if (self -> autofree )
278+ value = strdup ((char * ) value );
271279 item -> value = value ;
272280 }
273281 else
@@ -287,7 +295,7 @@ zhash_delete (zhash_t *self, const char *key)
287295
288296 item_t * item = s_item_lookup (self , key );
289297 if (item )
290- s_item_destroy (self , item , TRUE );
298+ s_item_destroy (self , item , true );
291299}
292300
293301
@@ -317,7 +325,7 @@ zhash_rename (zhash_t *self, const char *old_key, const char *new_key)
317325{
318326 item_t * item = s_item_lookup (self , old_key );
319327 if (item ) {
320- s_item_destroy (self , item , FALSE );
328+ s_item_destroy (self , item , false );
321329 item_t * new_item = s_item_lookup (self , new_key );
322330 if (new_item == NULL ) {
323331 free (item -> key );
@@ -389,7 +397,7 @@ zhash_dup (zhash_t *self)
389397 for (index = 0 ; index != self -> limit ; index ++ ) {
390398 item_t * item = self -> items [index ];
391399 while (item ) {
392- zhash_insert (copy , item -> key , strdup ( item -> value ) );
400+ zhash_insert (copy , item -> key , item -> value );
393401 item = item -> next ;
394402 }
395403 }
@@ -412,7 +420,7 @@ zhash_keys (zhash_t *self)
412420 for (index = 0 ; index != self -> limit ; index ++ ) {
413421 item_t * item = self -> items [index ];
414422 while (item ) {
415- zlist_append (keys , strdup ( item -> key ) );
423+ zlist_append (keys , item -> key );
416424 item = item -> next ;
417425 }
418426 }
@@ -498,7 +506,7 @@ zhash_load (zhash_t *self, char *filename)
498506 if (!equals )
499507 break ; // Some error, stop parsing it
500508 * equals ++ = 0 ;
501- zhash_update (self , buffer , strdup ( equals ) );
509+ zhash_update (self , buffer , equals );
502510 }
503511 fclose (handle );
504512 return 0 ;
@@ -617,12 +625,12 @@ zhash_test (int verbose)
617625 item = zhash_lookup (hash , testset [testnbr ].name );
618626 assert (item );
619627 zhash_delete (hash , testset [testnbr ].name );
620- testset [testnbr ].exists = FALSE ;
628+ testset [testnbr ].exists = false ;
621629 }
622630 else {
623631 sprintf (testset [testnbr ].name , "%x-%x" , rand (), rand ());
624632 if (zhash_insert (hash , testset [testnbr ].name , "" ) == 0 )
625- testset [testnbr ].exists = TRUE ;
633+ testset [testnbr ].exists = true ;
626634 }
627635 }
628636 // Test 10K lookups
0 commit comments