Skip to contentSkip to conversation

datatype: replace strncmp() by strcmp() in unit parser

PATCH In review Pablo Neira Ayuso 1 participant 3 messages Aug 14, 2024 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 datatype: replace strncmp() by strcmp() in unit parser

Bail out if unit cannot be parsed:

ruleset.nft:5:77-106: Error: Wrong rate format, expecting bytes or kbytes or mbytes
add rule netdev firewall PROTECTED_IPS update @quota_temp_before { ip daddr quota over 45000 mbytes/second } add @quota_trigger { ip daddr }

                                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

improve error reporting while at this.

Fixes 6615676d825e ("src: add per-bytes limit")
---
 src/datatype.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
src/datatype.c +4 -4
index d398a9c8c618..8879ff0523e8 100644
@@ -1485,14 +1485,14 @@ static struct error_record *time_unit_parse(const struct location *loc,
 struct error_record *data_unit_parse(const struct location *loc,
 				     const char *str, uint64_t *rate)
 {
-	if (strncmp(str, "bytes", strlen("bytes")) == 0)
+	if (strcmp(str, "bytes") == 0)
 		*rate = 1ULL;
-	else if (strncmp(str, "kbytes", strlen("kbytes")) == 0)
+	else if (strcmp(str, "kbytes") == 0)
 		*rate = 1024;
-	else if (strncmp(str, "mbytes", strlen("mbytes")) == 0)
+	else if (strcmp(str, "mbytes") == 0)
 		*rate = 1024 * 1024;
 	else
-		return error(loc, "Wrong rate format");
+		return error(loc, "Wrong unit format, expecting bytes or kbytes or mbytes");
 
 	return NULL;
 }
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 datatype: improve error reporting when time unit is not correct

Display:

Wrong unit format, expecting bytes or kbytes or mbytes

instead of:

Wrong rate format

Fixes 6615676d825e ("src: add per-bytes limit")
---
 src/datatype.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/datatype.c +1 -1
index 8879ff0523e8..24c3512d2a06 100644
@@ -1477,7 +1477,7 @@ static struct error_record *time_unit_parse(const struct location *loc,
 	else if (strcmp(str, "week") == 0)
 		*unit = 1ULL * 60 * 60 * 24 * 7;
 	else
-		return error(loc, "Wrong rate format");
+		return error(loc, "Wrong time format, expecting second or minute or hour or day or week");
 
 	return NULL;
 }
Signature · 1 line
2.30.2
author maintainer
#2 lore raw
In reply to Pablo Neira Ayuso
Quoted 6 lines · Pablo Neira Ayuso

Bail out if unit cannot be parsed:

ruleset.nft:5:77-106: Error: Wrong rate format, expecting bytes or kbytes or mbytes
add rule netdev firewall PROTECTED_IPS update @quota_temp_before { ip daddr quota over 45000 mbytes/second } add @quota_trigger { ip daddr }

                                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Scratch this patch. It breaks limit rate.

Quoted 35 lines

improve error reporting while at this.

Fixes 6615676d825e ("src: add per-bytes limit")
---
 src/datatype.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
src/datatype.c +4 -4
index d398a9c8c618..8879ff0523e8 100644
@@ -1485,14 +1485,14 @@ static struct error_record *time_unit_parse(const struct location *loc,
 struct error_record *data_unit_parse(const struct location *loc,
 				     const char *str, uint64_t *rate)
 {
-	if (strncmp(str, "bytes", strlen("bytes")) == 0)
+	if (strcmp(str, "bytes") == 0)
 		*rate = 1ULL;
-	else if (strncmp(str, "kbytes", strlen("kbytes")) == 0)
+	else if (strcmp(str, "kbytes") == 0)
 		*rate = 1024;
-	else if (strncmp(str, "mbytes", strlen("mbytes")) == 0)
+	else if (strcmp(str, "mbytes") == 0)
 		*rate = 1024 * 1024;
 	else
-		return error(loc, "Wrong rate format");
+		return error(loc, "Wrong unit format, expecting bytes or kbytes or mbytes");
 
 	return NULL;
 }
Signature · 1 line
2.30.2