Skip to contentSkip to conversation

f_flower: Remove always zero checks

PATCH Applied Maks Mishin 5 participants 5 messages Jul 7, 2024 – Jul 18, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f_flower: Remove always zero checks

Expression 'ttl & ~(255 >> 0)' is always zero, because right operand has 8 trailing zero bits, which is greater or equal than the size of the left operand == 8 bits.

Found by RASU JSC.

---
 tc/f_flower.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tc/f_flower.c +2 -2
index 08c1001a..244f0f7e 100644
@@ -1523,7 +1523,7 @@ static int flower_parse_mpls_lse(int *argc_p, char ***argv_p,
 
 			NEXT_ARG();
 			ret = get_u8(&ttl, *argv, 10);
-			if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) {
+			if (ret < 0) {
 				fprintf(stderr, "Illegal \"ttl\"\n");
 				return -1;
 			}
@@ -1936,7 +1936,7 @@ static int flower_parse_opt(const struct filter_util *qu, char *handle,
 			}
 			mpls_format_old = true;
 			ret = get_u8(&ttl, *argv, 10);
-			if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) {
+			if (ret < 0) {
 				fprintf(stderr, "Illegal \"mpls_ttl\"\n");
 				return -1;
 			}
Signature · 1 line
2.30.2
#1 lore raw
Quoted 25 lines · On Sun, 7 Jul 2024 20:27:41 +0300 Maks Mishin <[email protected]> wrote:

Expression 'ttl & ~(255 >> 0)' is always zero, because right operand has 8 trailing zero bits, which is greater or equal than the size of the left operand == 8 bits.

Found by RASU JSC.

---
 tc/f_flower.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tc/f_flower.c +1 -1
index 08c1001a..244f0f7e 100644
@@ -1523,7 +1523,7 @@ static int flower_parse_mpls_lse(int *argc_p, char ***argv_p,
 
 			NEXT_ARG();
 			ret = get_u8(&ttl, *argv, 10);
-			if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) {
+			if (ret < 0) {
 				fprintf(stderr, "Illegal \"ttl\"\n");
 				return -1;
 			}

tc/f_flower.c static int flower_parse_opt(const struct filter_util *qu, char *handle, · line 1942

Quoted 8 lines
tc/f_flower.c +1 -1
@@ -1936,7 +1936,7 @@ static int flower_parse_opt(const struct filter_util *qu, char *handle,
 			}
 			mpls_format_old = true;
 			ret = get_u8(&ttl, *argv, 10);
-			if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) {
+			if (ret < 0) {
 				fprintf(stderr, "Illegal \"mpls_ttl\"\n");
 				return -1;
 			}

That is correct mathematically, but perhaps the original author had different idea. Could we have review from someone familiar with MPLS support please.

Reviewed-by
#2 lore raw
Quoted 31 lines · Stephen Hemminger
Quoted 28 lines · On Sun, 7 Jul 2024 20:27:41 +0300 Maks Mishin <[email protected]> wrote:

Expression 'ttl & ~(255 >> 0)' is always zero, because right operand has 8 trailing zero bits, which is greater or equal than the size of the left operand == 8 bits.

Found by RASU JSC.

---
 tc/f_flower.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tc/f_flower.c +1 -1
index 08c1001a..244f0f7e 100644
@@ -1523,7 +1523,7 @@ static int flower_parse_mpls_lse(int *argc_p, char ***argv_p,
 
 			NEXT_ARG();
 			ret = get_u8(&ttl, *argv, 10);
-			if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) {
+			if (ret < 0) {
 				fprintf(stderr, "Illegal \"ttl\"\n");
 				return -1;
 			}
@@ -1936,7 +1936,7 @@ static int flower_parse_opt(const struct filter_util *qu, char *handle,
 			}
 			mpls_format_old = true;
 			ret = get_u8(&ttl, *argv, 10);
Quoted 8 lines

tc/f_flower.c static int flower_parse_opt(const struct filter_util *qu, char *handle, · line 1942

Quoted 5 lines
tc/f_flower.c +1 -1
-			if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) {
+			if (ret < 0) {
 				fprintf(stderr, "Illegal \"mpls_ttl\"\n");
 				return -1;
 			}

That is correct mathematically, but perhaps the original author had different idea. Could we have review from someone familiar with MPLS support please.

The MPLS TTL field is an 8 bits value. Therefore any successful return of get_u8() should be valid and this test is indeed not needed.

I guess the original intent was to keep consistency with how the other MPLS parameters are validated. But TTL is indeed a special case as it's the only parameter with a "common" length.

reviewer Reviewed-by
#3 lore raw
Quoted 41 lines · Guillaume Nault
Quoted 31 lines · Stephen Hemminger
Quoted 28 lines · On Sun, 7 Jul 2024 20:27:41 +0300 Maks Mishin <[email protected]> wrote:

Expression 'ttl & ~(255 >> 0)' is always zero, because right operand has 8 trailing zero bits, which is greater or equal than the size of the left operand == 8 bits.

Found by RASU JSC.

---
 tc/f_flower.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tc/f_flower.c +1 -1
index 08c1001a..244f0f7e 100644
@@ -1523,7 +1523,7 @@ static int flower_parse_mpls_lse(int *argc_p, char ***argv_p,
 
 			NEXT_ARG();
 			ret = get_u8(&ttl, *argv, 10);
-			if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) {
+			if (ret < 0) {
 				fprintf(stderr, "Illegal \"ttl\"\n");
 				return -1;
 			}
@@ -1936,7 +1936,7 @@ static int flower_parse_opt(const struct filter_util *qu, char *handle,
 			}
 			mpls_format_old = true;
 			ret = get_u8(&ttl, *argv, 10);
Quoted 8 lines

tc/f_flower.c static int flower_parse_opt(const struct filter_util *qu, char *handle, · line 1942

Quoted 5 lines
tc/f_flower.c +1 -1
-			if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) {
+			if (ret < 0) {
 				fprintf(stderr, "Illegal \"mpls_ttl\"\n");
 				return -1;
 			}

That is correct mathematically, but perhaps the original author had different idea. Could we have review from someone familiar with MPLS support please.

Quoted 8 lines

The MPLS TTL field is an 8 bits value. Therefore any successful return of get_u8() should be valid and this test is indeed not needed.

I guess the original intent was to keep consistency with how the other MPLS parameters are validated. But TTL is indeed a special case as it's the only parameter with a "common" length.

Thanks Guillaume,

Yes, I agree that was very likely the intent.

#4 lore raw
In reply to Maks Mishin
This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Hello:

This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Quoted 9 lines · On Sun, 7 Jul 2024 20:27:41 +0300 you wrote:

Expression 'ttl & ~(255 >> 0)' is always zero, because right operand has 8 trailing zero bits, which is greater or equal than the size of the left operand == 8 bits.

Found by RASU JSC.

[...]

Here is the summary with links:
- f_flower: Remove always zero checks

    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=b06341a252a8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html