[nft PATCH 1/3] proto: short-circuit loops over upper protocols

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Each `struct proto_desc` contains a fixed-size array of higher layer
protocols.  Only the first few are not NULL.  Therefore, we can stop
iterating over the array once we reach a NULL member.

Signed-off-by: Jeremy Sowden <jeremy@xxxxxxxxxx>
---
 src/proto.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/proto.c b/src/proto.c
index fe58c83a9295..31a2f38065ad 100644
--- a/src/proto.c
+++ b/src/proto.c
@@ -59,7 +59,8 @@ proto_find_upper(const struct proto_desc *base, unsigned int num)
 {
 	unsigned int i;
 
-	for (i = 0; i < array_size(base->protocols); i++) {
+	for (i = 0; i < array_size(base->protocols) && base->protocols[i].desc;
+	     i++) {
 		if (base->protocols[i].num == num)
 			return base->protocols[i].desc;
 	}
@@ -77,7 +78,8 @@ int proto_find_num(const struct proto_desc *base,
 {
 	unsigned int i;
 
-	for (i = 0; i < array_size(base->protocols); i++) {
+	for (i = 0; i < array_size(base->protocols) && base->protocols[i].desc;
+	     i++) {
 		if (base->protocols[i].desc == desc)
 			return base->protocols[i].num;
 	}
@@ -105,7 +107,9 @@ int proto_dev_type(const struct proto_desc *desc, uint16_t *res)
 			*res = dev_proto_desc[i].type;
 			return 0;
 		}
-		for (j = 0; j < array_size(base->protocols); j++) {
+		for (j = 0; j < array_size(base->protocols) &&
+			     base->protocols[j].desc;
+		     j++) {
 			if (base->protocols[j].desc == desc) {
 				*res = dev_proto_desc[i].type;
 				return 0;
-- 
2.33.0





[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux