X Tutup
Skip to content

Commit 80a226b

Browse files
committed
list: make LIST_FOREACH() and LIST_FOREACH_BACKWARDS() safer
1 parent 0367788 commit 80a226b

File tree

16 files changed

+33
-30
lines changed

16 files changed

+33
-30
lines changed

src/basic/list.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,17 @@
139139
/* The type of the iterator 'i' is automatically determined by the type of 'head', and declared in the
140140
* loop. Hence, do not declare the same variable in the outer scope. Sometimes, we set 'head' through
141141
* hashmap_get(). In that case, you need to explicitly cast the result. */
142+
#define LIST_FOREACH_WITH_NEXT(name,i,n,head) \
143+
for (typeof(*(head)) *n, *i = (head); i && (n = i->name##_next, true); i = n)
144+
142145
#define LIST_FOREACH(name,i,head) \
143-
for (typeof(*(head)) *i = (head); i; i = i->name##_next)
146+
LIST_FOREACH_WITH_NEXT(name, i, UNIQ_T(n, UNIQ), head)
144147

145-
#define LIST_FOREACH_SAFE(name,i,n,head) \
146-
for (typeof(*(head)) *n, *i = (head); i && ((n = i->name##_next), 1); i = n)
148+
#define _LIST_FOREACH_WITH_PREV(name,i,p,start) \
149+
for (typeof(*(start)) *p, *i = (start); i && (p = i->name##_prev, true); i = p)
147150

148-
#define LIST_FOREACH_BACKWARDS(name,i,p) \
149-
for (typeof(*(p)) *i = (p); i; i = i->name##_prev)
151+
#define LIST_FOREACH_BACKWARDS(name,i,start) \
152+
_LIST_FOREACH_WITH_PREV(name, i, UNIQ_T(p, UNIQ), start)
150153

151154
/* Iterate through all the members of the list p is included in, but skip over p */
152155
#define LIST_FOREACH_OTHERS(name,i,p) \

src/core/device.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ static void device_update_found_by_sysfs(Manager *m, const char *sysfs, DeviceFo
719719
return;
720720

721721
l = hashmap_get(m->devices_by_sysfs, sysfs);
722-
LIST_FOREACH_SAFE(same_sysfs, d, n, l)
722+
LIST_FOREACH(same_sysfs, d, l)
723723
device_update_found_one(d, found, mask);
724724
}
725725

@@ -905,7 +905,7 @@ static void device_propagate_reload_by_sysfs(Manager *m, const char *sysfs) {
905905
assert(sysfs);
906906

907907
l = hashmap_get(m->devices_by_sysfs, sysfs);
908-
LIST_FOREACH_SAFE(same_sysfs, d, n, l) {
908+
LIST_FOREACH(same_sysfs, d, l) {
909909
if (d->state == DEVICE_DEAD)
910910
continue;
911911

src/core/service.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ static void service_remove_fd_store(Service *s, const char *name) {
507507
assert(s);
508508
assert(name);
509509

510-
LIST_FOREACH_SAFE(fd_store, fs, n, s->fd_store) {
510+
LIST_FOREACH(fd_store, fs, s->fd_store) {
511511
if (!streq(fs->fdname, name))
512512
continue;
513513

src/libsystemd-network/sd-dhcp6-lease.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int sd_dhcp6_lease_get_server_address(sd_dhcp6_lease *lease, struct in6_addr *re
108108
void dhcp6_ia_clear_addresses(DHCP6IA *ia) {
109109
assert(ia);
110110

111-
LIST_FOREACH_SAFE(addresses, a, n, ia->addresses)
111+
LIST_FOREACH(addresses, a, ia->addresses)
112112
free(a);
113113

114114
ia->addresses = NULL;

src/libudev/libudev-list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void udev_list_cleanup(struct udev_list *list) {
117117
list->uptodate = false;
118118
hashmap_clear_with_destructor(list->unique_entries, udev_list_entry_free);
119119
} else
120-
LIST_FOREACH_SAFE(entries, i, n, list->entries)
120+
LIST_FOREACH(entries, i, list->entries)
121121
udev_list_entry_free(i);
122122
}
123123

src/network/netdev/wireguard.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ static int wireguard_verify(NetDev *netdev, const char *filename) {
11461146
"%s: Missing PrivateKey= or PrivateKeyFile=, "
11471147
"Ignoring network device.", filename);
11481148

1149-
LIST_FOREACH_SAFE(peers, peer, peer_next, w->peers) {
1149+
LIST_FOREACH(peers, peer, w->peers) {
11501150
if (wireguard_peer_verify(peer) < 0) {
11511151
wireguard_peer_free(peer);
11521152
continue;

src/partition/repart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,7 @@ static int context_load_partition_table(
19021902
static void context_unload_partition_table(Context *context) {
19031903
assert(context);
19041904

1905-
LIST_FOREACH_SAFE(partitions, p, next, context->partitions) {
1905+
LIST_FOREACH(partitions, p, context->partitions) {
19061906

19071907
/* Entirely remove partitions that have no configuration */
19081908
if (PARTITION_IS_FOREIGN(p)) {

src/resolve/resolved-dns-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static bool dns_cache_remove_by_key(DnsCache *c, DnsResourceKey *key) {
144144
if (!first)
145145
return false;
146146

147-
LIST_FOREACH_SAFE(by_key, i, n, first) {
147+
LIST_FOREACH(by_key, i, first) {
148148
prioq_remove(c->by_expiry, i, &i->prioq_idx);
149149
dns_cache_item_free(i);
150150
}

src/resolve/resolved-dns-transaction.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ static int on_stream_complete(DnsStream *s, int error) {
635635
}
636636

637637
if (error != 0)
638-
LIST_FOREACH_SAFE(transactions_by_stream, t, n, s->transactions)
638+
LIST_FOREACH(transactions_by_stream, t, s->transactions)
639639
on_transaction_stream_error(t, error);
640640

641641
return 0;

src/rfkill/rfkill.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static int load_state(Context *c, const struct rfkill_event *event) {
190190
static void save_state_queue_remove(Context *c, int idx, const char *state_file) {
191191
assert(c);
192192

193-
LIST_FOREACH_SAFE(queue, item, tmp, c->write_queue)
193+
LIST_FOREACH(queue, item, c->write_queue)
194194
if ((state_file && streq(item->file, state_file)) || idx == item->rfkill_idx) {
195195
log_debug("Canceled previous save state of '%s' to %s.", one_zero(item->state), item->file);
196196
LIST_REMOVE(queue, c->write_queue, item);

0 commit comments

Comments
 (0)
X Tutup