From ec38d9024189a98cac7b57ca8cc61972826baa1c Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Mon, 1 Jul 2024 14:17:07 +0800 Subject: [PATCH] bugfix: PACKET_GETALL_TUNNELS --- include/stellar/tunnel.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/include/stellar/tunnel.h b/include/stellar/tunnel.h index 81d0526..07d2339 100644 --- a/include/stellar/tunnel.h +++ b/include/stellar/tunnel.h @@ -47,13 +47,16 @@ int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *o #define PACKET_FOREACH_TUNNEL_REVERSE(pkt, tunnel) \ for (int i = packet_get_tunnel_count(pkt) - 1; i >= 0 && packet_get_tunnel_by_idx(pkt, i, &tunnel) == 0; i--) -#define PACKET_GETALL_TUNNELS(pkt, tunnels) \ - { \ - int num = MIN(packet_get_tunnel_count(pkt), (sizeof(tunnels) / sizeof(tunnels[0]))); \ - for (int i = 0; i < num && packet_get_tunnel_by_idx(pkt, i, &tunnels[i]) == 0; i++) \ - /* void */; \ - return num; \ - } +#define PACKET_GETALL_TUNNELS(pkt, tunnels) \ + ({ \ + memset(tunnels, 0, sizeof(tunnels)); \ + int size = sizeof(tunnels) / sizeof(tunnels[0]); \ + int count = packet_get_tunnel_count(pkt); \ + int num = count > size ? size : count; \ + for (int i = 0; i < num && packet_get_tunnel_by_idx(pkt, i, &tunnels[i]) == 0; i++) \ + /* void */; \ + num; \ + }) #ifdef __cplusplus }