list.c
wrong size for the calloc() in list_data_dup and list_get_iterator()
Showing
1 changed file
with
35 additions
and
28 deletions
... | @@ -31,7 +31,7 @@ list_create (list_t *plist) | ... | @@ -31,7 +31,7 @@ list_create (list_t *plist) |
31 | { | 31 | { |
32 | list_t list; | 32 | list_t list; |
33 | int status; | 33 | int status; |
34 | 34 | ||
35 | if (plist == NULL) | 35 | if (plist == NULL) |
36 | return MU_ERR_OUT_PTR_NULL; | 36 | return MU_ERR_OUT_PTR_NULL; |
37 | list = calloc (sizeof (*list), 1); | 37 | list = calloc (sizeof (*list), 1); |
... | @@ -57,7 +57,7 @@ list_destroy (list_t *plist) | ... | @@ -57,7 +57,7 @@ list_destroy (list_t *plist) |
57 | list_t list = *plist; | 57 | list_t list = *plist; |
58 | struct list_data *current; | 58 | struct list_data *current; |
59 | struct list_data *previous; | 59 | struct list_data *previous; |
60 | 60 | ||
61 | monitor_wrlock (list->monitor); | 61 | monitor_wrlock (list->monitor); |
62 | for (current = list->head.next; current != &(list->head);) | 62 | for (current = list->head.next; current != &(list->head);) |
63 | { | 63 | { |
... | @@ -79,7 +79,7 @@ list_append (list_t list, void *item) | ... | @@ -79,7 +79,7 @@ list_append (list_t list, void *item) |
79 | { | 79 | { |
80 | struct list_data *ldata; | 80 | struct list_data *ldata; |
81 | struct list_data *last; | 81 | struct list_data *last; |
82 | 82 | ||
83 | if (list == NULL) | 83 | if (list == NULL) |
84 | return EINVAL; | 84 | return EINVAL; |
85 | last = list->head.prev; | 85 | last = list->head.prev; |
... | @@ -102,7 +102,7 @@ list_prepend (list_t list, void *item) | ... | @@ -102,7 +102,7 @@ list_prepend (list_t list, void *item) |
102 | { | 102 | { |
103 | struct list_data *ldata; | 103 | struct list_data *ldata; |
104 | struct list_data *first; | 104 | struct list_data *first; |
105 | 105 | ||
106 | if (list == NULL) | 106 | if (list == NULL) |
107 | return EINVAL; | 107 | return EINVAL; |
108 | first = list->head.next; | 108 | first = list->head.next; |
... | @@ -124,7 +124,7 @@ int | ... | @@ -124,7 +124,7 @@ int |
124 | list_is_empty (list_t list) | 124 | list_is_empty (list_t list) |
125 | { | 125 | { |
126 | size_t n = 0; | 126 | size_t n = 0; |
127 | 127 | ||
128 | list_count (list, &n); | 128 | list_count (list, &n); |
129 | return (n == 0); | 129 | return (n == 0); |
130 | } | 130 | } |
... | @@ -144,13 +144,13 @@ list_comparator_t | ... | @@ -144,13 +144,13 @@ list_comparator_t |
144 | list_set_comparator (list_t list, list_comparator_t comp) | 144 | list_set_comparator (list_t list, list_comparator_t comp) |
145 | { | 145 | { |
146 | list_comparator_t old_comp; | 146 | list_comparator_t old_comp; |
147 | 147 | ||
148 | if (list == NULL) | 148 | if (list == NULL) |
149 | return NULL; | 149 | return NULL; |
150 | old_comp = list->comp; | 150 | old_comp = list->comp; |
151 | list->comp = comp; | 151 | list->comp = comp; |
152 | return old_comp; | 152 | return old_comp; |
153 | } | 153 | } |
154 | 154 | ||
155 | static int | 155 | static int |
156 | def_comp (const void *item, const void *value) | 156 | def_comp (const void *item, const void *value) |
... | @@ -164,7 +164,7 @@ list_locate (list_t list, void *item, void **ret_item) | ... | @@ -164,7 +164,7 @@ list_locate (list_t list, void *item, void **ret_item) |
164 | struct list_data *current, *previous; | 164 | struct list_data *current, *previous; |
165 | list_comparator_t comp; | 165 | list_comparator_t comp; |
166 | int status = MU_ERR_NOENT; | 166 | int status = MU_ERR_NOENT; |
167 | 167 | ||
168 | if (list == NULL) | 168 | if (list == NULL) |
169 | return EINVAL; | 169 | return EINVAL; |
170 | comp = list->comp ? list->comp : def_comp; | 170 | comp = list->comp ? list->comp : def_comp; |
... | @@ -190,7 +190,7 @@ list_insert (list_t list, void *item, void *new_item) | ... | @@ -190,7 +190,7 @@ list_insert (list_t list, void *item, void *new_item) |
190 | struct list_data *current; | 190 | struct list_data *current; |
191 | list_comparator_t comp; | 191 | list_comparator_t comp; |
192 | int status = MU_ERR_NOENT; | 192 | int status = MU_ERR_NOENT; |
193 | 193 | ||
194 | if (list == NULL) | 194 | if (list == NULL) |
195 | return EINVAL; | 195 | return EINVAL; |
196 | comp = list->comp ? list->comp : def_comp; | 196 | comp = list->comp ? list->comp : def_comp; |
... | @@ -205,7 +205,7 @@ list_insert (list_t list, void *item, void *new_item) | ... | @@ -205,7 +205,7 @@ list_insert (list_t list, void *item, void *new_item) |
205 | struct list_data *ldata = calloc (sizeof (*ldata), 1); | 205 | struct list_data *ldata = calloc (sizeof (*ldata), 1); |
206 | if (ldata == NULL) | 206 | if (ldata == NULL) |
207 | return ENOMEM; | 207 | return ENOMEM; |
208 | 208 | ||
209 | ldata->item = new_item; | 209 | ldata->item = new_item; |
210 | ldata->next = current->next; | 210 | ldata->next = current->next; |
211 | ldata->prev = current; | 211 | ldata->prev = current; |
... | @@ -218,7 +218,7 @@ list_insert (list_t list, void *item, void *new_item) | ... | @@ -218,7 +218,7 @@ list_insert (list_t list, void *item, void *new_item) |
218 | current = list->head.next = ldata; | 218 | current = list->head.next = ldata; |
219 | else | 219 | else |
220 | current->next = ldata; | 220 | current->next = ldata; |
221 | 221 | ||
222 | list->count++; | 222 | list->count++; |
223 | status = 0; | 223 | status = 0; |
224 | break; | 224 | break; |
... | @@ -234,7 +234,7 @@ list_remove (list_t list, void *item) | ... | @@ -234,7 +234,7 @@ list_remove (list_t list, void *item) |
234 | struct list_data *current, *previous; | 234 | struct list_data *current, *previous; |
235 | list_comparator_t comp; | 235 | list_comparator_t comp; |
236 | int status = MU_ERR_NOENT; | 236 | int status = MU_ERR_NOENT; |
237 | 237 | ||
238 | if (list == NULL) | 238 | if (list == NULL) |
239 | return EINVAL; | 239 | return EINVAL; |
240 | comp = list->comp ? list->comp : def_comp; | 240 | comp = list->comp ? list->comp : def_comp; |
... | @@ -263,7 +263,7 @@ list_replace (list_t list, void *old_item, void *new_item) | ... | @@ -263,7 +263,7 @@ list_replace (list_t list, void *old_item, void *new_item) |
263 | struct list_data *current, *previous; | 263 | struct list_data *current, *previous; |
264 | list_comparator_t comp; | 264 | list_comparator_t comp; |
265 | int status = MU_ERR_NOENT; | 265 | int status = MU_ERR_NOENT; |
266 | 266 | ||
267 | if (list == NULL) | 267 | if (list == NULL) |
268 | return EINVAL; | 268 | return EINVAL; |
269 | comp = list->comp ? list->comp : def_comp; | 269 | comp = list->comp ? list->comp : def_comp; |
... | @@ -288,7 +288,7 @@ list_get (list_t list, size_t indx, void **pitem) | ... | @@ -288,7 +288,7 @@ list_get (list_t list, size_t indx, void **pitem) |
288 | struct list_data *current; | 288 | struct list_data *current; |
289 | size_t count; | 289 | size_t count; |
290 | int status = MU_ERR_NOENT; | 290 | int status = MU_ERR_NOENT; |
291 | 291 | ||
292 | if (list == NULL) | 292 | if (list == NULL) |
293 | return EINVAL; | 293 | return EINVAL; |
294 | if (pitem == NULL) | 294 | if (pitem == NULL) |
... | @@ -313,7 +313,7 @@ list_do (list_t list, list_action_t * action, void *cbdata) | ... | @@ -313,7 +313,7 @@ list_do (list_t list, list_action_t * action, void *cbdata) |
313 | { | 313 | { |
314 | iterator_t itr; | 314 | iterator_t itr; |
315 | int status = 0; | 315 | int status = 0; |
316 | 316 | ||
317 | if (list == NULL || action == NULL) | 317 | if (list == NULL || action == NULL) |
318 | return EINVAL; | 318 | return EINVAL; |
319 | status = list_get_iterator(list, &itr); | 319 | status = list_get_iterator(list, &itr); |
... | @@ -346,20 +346,20 @@ list_to_array (list_t list, void **array, size_t count, size_t *pcount) | ... | @@ -346,20 +346,20 @@ list_to_array (list_t list, void **array, size_t count, size_t *pcount) |
346 | 346 | ||
347 | if (!list) | 347 | if (!list) |
348 | return EINVAL; | 348 | return EINVAL; |
349 | 349 | ||
350 | total = (count < list->count) ? count : list->count; | 350 | total = (count < list->count) ? count : list->count; |
351 | 351 | ||
352 | if (array) | 352 | if (array) |
353 | { | 353 | { |
354 | size_t i; | 354 | size_t i; |
355 | struct list_data *current; | 355 | struct list_data *current; |
356 | 356 | ||
357 | for (i = 0, current = list->head.next; | 357 | for (i = 0, current = list->head.next; |
358 | i < total && current != &(list->head); current = current->next) | 358 | i < total && current != &(list->head); current = current->next) |
359 | array[i] = current->item; | 359 | array[i] = current->item; |
360 | } | 360 | } |
361 | if (pcount) | 361 | if (pcount) |
362 | *pcount = total; | 362 | *pcount = total; |
363 | return 0; | 363 | return 0; |
364 | } | 364 | } |
365 | 365 | ||
... | @@ -420,10 +420,15 @@ curitem_p (void *owner, void *item) | ... | @@ -420,10 +420,15 @@ curitem_p (void *owner, void *item) |
420 | } | 420 | } |
421 | 421 | ||
422 | static int | 422 | static int |
423 | list_data_dup (void **ptr, void *data) | 423 | list_data_dup (void **ptr, void *owner) |
424 | { | 424 | { |
425 | *ptr = malloc (sizeof (struct list_iterator *)); | 425 | struct list_iterator *itr = owner; |
426 | memcpy (*ptr, data, sizeof (struct list_iterator *)); | 426 | struct list_iterator *clone; |
427 | clone = malloc (sizeof (sizeof *itr)); | ||
428 | if (clone == NULL) | ||
429 | return ENOMEM; | ||
430 | /* let the assignement operator copy the elements. */ | ||
431 | *clone = *itr; | ||
427 | return 0; | 432 | return 0; |
428 | } | 433 | } |
429 | 434 | ||
... | @@ -436,16 +441,19 @@ list_get_iterator (list_t list, iterator_t *piterator) | ... | @@ -436,16 +441,19 @@ list_get_iterator (list_t list, iterator_t *piterator) |
436 | 441 | ||
437 | if (!list) | 442 | if (!list) |
438 | return EINVAL; | 443 | return EINVAL; |
439 | 444 | ||
440 | itr = calloc (1, sizeof (struct list_iterator *)); | 445 | itr = calloc (1, sizeof *itr); |
441 | if (!itr) | 446 | if (!itr) |
442 | return ENOMEM; | 447 | return ENOMEM; |
443 | itr->list = list; | 448 | itr->list = list; |
444 | itr->cur = NULL; | 449 | itr->cur = NULL; |
445 | 450 | ||
446 | status = iterator_create (&iterator, itr); | 451 | status = iterator_create (&iterator, itr); |
447 | if (status) | 452 | if (status) |
448 | return status; | 453 | { |
454 | free (itr); | ||
455 | return status; | ||
456 | } | ||
449 | 457 | ||
450 | iterator_set_first (iterator, first); | 458 | iterator_set_first (iterator, first); |
451 | iterator_set_next (iterator, next); | 459 | iterator_set_next (iterator, next); |
... | @@ -454,10 +462,9 @@ list_get_iterator (list_t list, iterator_t *piterator) | ... | @@ -454,10 +462,9 @@ list_get_iterator (list_t list, iterator_t *piterator) |
454 | iterator_set_curitem_p (iterator, curitem_p); | 462 | iterator_set_curitem_p (iterator, curitem_p); |
455 | iterator_set_destroy (iterator, destroy); | 463 | iterator_set_destroy (iterator, destroy); |
456 | iterator_set_dup (iterator, list_data_dup); | 464 | iterator_set_dup (iterator, list_data_dup); |
457 | 465 | ||
458 | iterator_attach (&list->itr, iterator); | 466 | iterator_attach (&list->itr, iterator); |
459 | 467 | ||
460 | *piterator = iterator; | 468 | *piterator = iterator; |
461 | return 0; | 469 | return 0; |
462 | } | 470 | } |
463 | ... | ... |
-
Please register or sign in to post a comment