File: | obj-scan-build/libports/../../libports/bucket-iterate.c |
Location: | line 71, column 20 |
Description: | Call to 'realloc' has an allocation size of 0 bytes |
1 | /* Iterate a function over the ports in a bucket. | |||
2 | Copyright (C) 1995, 1999 Free Software Foundation, Inc. | |||
3 | Written by Michael I. Bushnell. | |||
4 | ||||
5 | This file is part of the GNU Hurd. | |||
6 | ||||
7 | The GNU Hurd is free software; you can redistribute it and/or | |||
8 | modify it under the terms of the GNU General Public License as | |||
9 | published by the Free Software Foundation; either version 2, or (at | |||
10 | your option) any later version. | |||
11 | ||||
12 | The GNU Hurd is distributed in the hope that it will be useful, but | |||
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
15 | General Public License for more details. | |||
16 | ||||
17 | You should have received a copy of the GNU General Public License | |||
18 | along with this program; if not, write to the Free Software | |||
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ | |||
20 | ||||
21 | #include "ports.h" | |||
22 | #include <hurd/ihash.h> | |||
23 | ||||
24 | ||||
25 | /* Internal entrypoint for both ports_bucket_iterate and ports_class_iterate. | |||
26 | If CLASS is non-null, call FUN only for ports in that class. */ | |||
27 | error_t | |||
28 | _ports_bucket_class_iterate (struct port_bucket *bucket, | |||
29 | struct port_class *class, | |||
30 | error_t (*fun)(void *)) | |||
31 | { | |||
32 | /* This is obscenely ineffecient. ihash and ports need to cooperate | |||
33 | more closely to do it efficiently. */ | |||
34 | void **p; | |||
35 | size_t i, n, nr_items; | |||
36 | error_t err; | |||
37 | ||||
38 | pthread_mutex_lock (&_ports_lock); | |||
39 | ||||
40 | if (bucket->htable.nr_items == 0) | |||
41 | { | |||
42 | pthread_mutex_unlock (&_ports_lock); | |||
43 | return 0; | |||
44 | } | |||
45 | ||||
46 | nr_items = bucket->htable.nr_items; | |||
47 | p = malloc (nr_items * sizeof *p); | |||
48 | if (p == NULL((void*)0)) | |||
49 | { | |||
50 | pthread_mutex_unlock (&_ports_lock); | |||
51 | return ENOMEM((0x10 << 26) | ((12) & 0x3fff)); | |||
52 | } | |||
53 | ||||
54 | n = 0; | |||
55 | HURD_IHASH_ITERATE (&bucket->htable, arg)for (hurd_ihash_value_t arg, *_hurd_ihash_valuep = (&bucket ->htable)->size ? &(&bucket->htable)->items [0].value : 0; (&bucket->htable)->size && ( (_hurd_ihash_item_t) _hurd_ihash_valuep) - &(&bucket-> htable)->items[0] < (&bucket->htable)->size && (arg = *_hurd_ihash_valuep, 1); _hurd_ihash_valuep = (hurd_ihash_value_t *) (((_hurd_ihash_item_t) _hurd_ihash_valuep) + 1)) if (arg != ((hurd_ihash_value_t) 0) && arg != ((hurd_ihash_value_t ) -1)) | |||
56 | { | |||
57 | struct port_info *const pi = arg; | |||
58 | ||||
59 | if (class == 0 || pi->class == class) | |||
60 | { | |||
61 | pi->refcnt++; | |||
62 | p[n] = pi; | |||
63 | n++; | |||
64 | } | |||
65 | } | |||
66 | pthread_mutex_unlock (&_ports_lock); | |||
67 | ||||
68 | if (n != nr_items) | |||
69 | { | |||
70 | /* We allocated too much. Release unused memory. */ | |||
71 | void **new = realloc (p, n * sizeof *p); | |||
| ||||
72 | if (new) | |||
73 | p = new; | |||
74 | } | |||
75 | ||||
76 | err = 0; | |||
77 | for (i = 0; i < n; i++) | |||
78 | { | |||
79 | if (!err) | |||
80 | err = (*fun)(p[i]); | |||
81 | ports_port_deref (p[i]); | |||
82 | } | |||
83 | ||||
84 | free (p); | |||
85 | return err; | |||
86 | } | |||
87 | ||||
88 | error_t | |||
89 | ports_bucket_iterate (struct port_bucket *bucket, | |||
90 | error_t (*fun)(void *)) | |||
91 | { | |||
92 | return _ports_bucket_class_iterate (bucket, 0, fun); | |||
| ||||
93 | } |