Skip to contentSkip to conversation

lib/vsprintf: Remove redundant code

PATCH In review Jiapeng Chong 4 participants 5 messages Oct 18, 2023 – Oct 19, 2023

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

PATCH 1/1 lib/vsprintf: Remove redundant code

When variable needcolon is assigned a value of false, it must be assigned a value of true later on, which is redundant code.

lib/vsprintf.c:1411:4: warning: Value stored to 'needcolon' is never read.

---
 lib/vsprintf.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
lib/vsprintf.c +2 -3
index afb88b24fa74..9a9086885da8 100644
@@ -1406,10 +1406,9 @@ char *ip6_compressed_string(char *p, const char *addr)
 			i += longest - 1;
 			continue;
 		}
-		if (needcolon) {
+		if (needcolon)
 			*p++ = ':';
-			needcolon = false;
-		}
+
 		/* hex u16 without leading 0s */
 		word = ntohs(in6.s6_addr16[i]);
 		hi = word >> 8;
Signature · 1 line
2.20.1.7.g153144c
#1 lore raw
Quoted 4 lines · Jiapeng Chong

When variable needcolon is assigned a value of false, it must be assigned a value of true later on, which is redundant code.

lib/vsprintf.c:1411:4: warning: Value stored to 'needcolon' is never read.

...

lib/vsprintf.c char *ip6_compressed_string(char *p, const char *addr) · line 1414

Quoted 13 lines
lib/vsprintf.c +2 -3
@@ -1406,10 +1406,9 @@ char *ip6_compressed_string(char *p, const char *addr)
 			i += longest - 1;
 			continue;
 		}
-		if (needcolon) {
+		if (needcolon)
 			*p++ = ':';
-			needcolon = false;
-		}
+
 		/* hex u16 without leading 0s */
 		word = ntohs(in6.s6_addr16[i]);
 		hi = word >> 8;

Logically you may remove then the assignment to true as well.

But I would double check if it's not a continue missing somewhere or so.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
In reply to Jiapeng Chong
Quoted 2 lines · Jiapeng Chong

When variable needcolon is assigned a value of false, it must be assigned a value of true later on, which is redundant code.

NAK on any patch modifying these things without first filling the blank space in ip6() in lib/test_printf.c with tests for all the various ip6 printing modes.

Adding test cases there is much more useful than code-golfing, and would help verify that these changes are actually ok. Or may reveal that the current code is buggy.

Rasmus

#3 lore raw
Quoted 1 line · Rasmus Villemoes

On 18/10/2023 08.48, Jiapeng Chong wrote:

...

Quoted 1 line

Or may reveal that the current code is buggy.

...and hopefully didn't become a part of any ABIs...

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#4 lore raw
In reply to Jiapeng Chong
Quoted 19 lines · Jiapeng Chong

When variable needcolon is assigned a value of false, it must be assigned a value of true later on, which is redundant code.

lib/vsprintf.c:1411:4: warning: Value stored to 'needcolon' is never read.

---
 lib/vsprintf.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
lib/vsprintf.c +0 -0
index afb88b24fa74..9a9086885da8 100644
@@ -1406,10 +1406,9 @@ char *ip6_compressed_string(char *p, const char *addr)
 			i += longest - 1;
 			continue;

lib/vsprintf.c char *ip6_compressed_string(char *p, const char *addr) · line 1414

Quoted 10 lines
lib/vsprintf.c +2 -3
 		}
-		if (needcolon) {
+		if (needcolon)
 			*p++ = ':';
-			needcolon = false;
-		}
+
 		/* hex u16 without leading 0s */
 		word = ntohs(in6.s6_addr16[i]);
 		hi = word >> 8;

I am against this change. The assignment should be there from the logical POV. IMHO, it helps people to verify that the code works as expected. The removal might even cause regression when the logic gets modified in the future.

Anyway, this is a slow path. The code readability is more important then the speed. Let compiler do these micro optimizations.

Best Regards,
Petr