Skip to contentSkip to conversation

tty: xtensa/iss: Use umin() to fix Coccinelle warning

PATCH Superseded Thorsten Blum 3 participants 9 messages Apr 3, 2024 – Apr 4, 2024 Tensilica Xtensa port (xtensa)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty: xtensa/iss: Use umin() to fix Coccinelle warning

Use unsigned size_t to improve len data type and umin() to fix the following Coccinelle/coccicheck warning reported by minmax.cocci:

WARNING opportunity for min()

---
 arch/xtensa/platforms/iss/console.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/xtensa/platforms/iss/console.c +3 -2
index 8896e691c051..b493fc50933d 100644
@@ -167,8 +167,9 @@ late_initcall(rs_init);
 static void iss_console_write(struct console *co, const char *s, unsigned count)
 {
 	if (s && *s != 0) {
-		int len = strlen(s);
-		simc_write(1, s, count < len ? count : len);
+		size_t len = strlen(s);
+
+		simc_write(1, s, umin(count, len));
 	}
 }
 
Signature · 1 line
2.44.0
maintainer
#1 lore raw
Quoted 10 lines · Thorsten Blum

Use unsigned size_t to improve len data type and umin() to fix the following Coccinelle/coccicheck warning reported by minmax.cocci:

WARNING opportunity for min()

---
 arch/xtensa/platforms/iss/console.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Thanks. Applied to my xtensa tree.

-- Max

#2 lore raw
In reply to Thorsten Blum
Quoted 15 lines · Thorsten Blum

Use unsigned size_t to improve len data type and umin() to fix the following Coccinelle/coccicheck warning reported by minmax.cocci:

WARNING opportunity for min()

---
  arch/xtensa/platforms/iss/console.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
arch/xtensa/platforms/iss/console.c +0 -0
index 8896e691c051..b493fc50933d 100644

arch/xtensa/platforms/iss/console.c late_initcall(rs_init); · line 172

Quoted 8 lines
arch/xtensa/platforms/iss/console.c +3 -2
@@ -167,8 +167,9 @@ late_initcall(rs_init);
  static void iss_console_write(struct console *co, const char *s, unsigned count)
  {
  	if (s && *s != 0) {
-		int len = strlen(s);
-		simc_write(1, s, count < len ? count : len);
+		size_t len = strlen(s);
+
+		simc_write(1, s, umin(count, len));

The cast to unsigned (ie. umin()) is now not necessary. You should have used min() as was suggested, right?

arch/xtensa/platforms/iss/console.c late_initcall(rs_init); · line 175

Quoted 3 lines
arch/xtensa/platforms/iss/console.c +0 -0
  	}
  }
 

thanks,

Signature · 2 lines
js
suse labs
#3 lore raw
Quoted 2 lines · Jiri Slaby

The cast to unsigned (ie. umin()) is now not necessary. You should have used min() as was suggested, right?

Yes, umin() was only necessary before I casted len from int to size_t.

I'll submit a v2 shortly.

Thanks,
Thorsten

#4 lore raw

PATCH v2 1/1 tty: xtensa/iss: Use min() to fix Coccinelle warning

In reply to Jiri Slaby

Use unsigned size_t to improve len data type and min() to fix the following Coccinelle/coccicheck warning reported by minmax.cocci:

WARNING opportunity for min()

---
Changes in v2:
- Use min() instead of umin() as suggested by Jiri Slaby

---
 arch/xtensa/platforms/iss/console.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/xtensa/platforms/iss/console.c +3 -2
index 8896e691c051..d2380767f3a4 100644
@@ -167,8 +167,9 @@ late_initcall(rs_init);
 static void iss_console_write(struct console *co, const char *s, unsigned count)
 {
 	if (s && *s != 0) {
-		int len = strlen(s);
-		simc_write(1, s, count < len ? count : len);
+		size_t len = strlen(s);
+
+		simc_write(1, s, min(count, len));
 	}
 }
 
Signature · 1 line
2.44.0
#5 lore raw

Re: [PATCH v2] tty: xtensa/iss: Use min() to fix Coccinelle warning

Quoted 18 lines · Thorsten Blum

Use unsigned size_t to improve len data type and min() to fix the following Coccinelle/coccicheck warning reported by minmax.cocci:

WARNING opportunity for min()

---
Changes in v2:
- Use min() instead of umin() as suggested by Jiri Slaby

---
  arch/xtensa/platforms/iss/console.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
arch/xtensa/platforms/iss/console.c +0 -0
index 8896e691c051..d2380767f3a4 100644

arch/xtensa/platforms/iss/console.c late_initcall(rs_init); · line 172

Quoted 8 lines
arch/xtensa/platforms/iss/console.c +3 -2
@@ -167,8 +167,9 @@ late_initcall(rs_init);
  static void iss_console_write(struct console *co, const char *s, unsigned count)
  {
  	if (s && *s != 0) {
-		int len = strlen(s);
-		simc_write(1, s, count < len ? count : len);
+		size_t len = strlen(s);
+
+		simc_write(1, s, min(count, len));

So do you need 'len' after all :)?

if (s && *s != 0)
simc_write(1, s, min(count, strlen(s)));

sounds good.

Quoted 3 lines
arch/xtensa/platforms/iss/console.c +0 -0
  	}
  }
 
Signature · 2 lines
js
suse labs
#6 lore raw

PATCH v3 1/1 tty: xtensa/iss: Use min() to fix Coccinelle warning

Inline strlen() and use min() to fix the following Coccinelle/coccicheck warning reported by minmax.cocci:

WARNING opportunity for min()

---
Changes in v2:
- Use min() instead of umin() as suggested by Jiri Slaby

Changes in v3:
- Inline strlen() as suggested by Jiri Slaby

---
 arch/xtensa/platforms/iss/console.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/xtensa/platforms/iss/console.c +2 -4
index 8896e691c051..abec44b687df 100644
@@ -166,10 +166,8 @@ late_initcall(rs_init);
 
 static void iss_console_write(struct console *co, const char *s, unsigned count)
 {
-	if (s && *s != 0) {
-		int len = strlen(s);
-		simc_write(1, s, count < len ? count : len);
-	}
+	if (s && *s != 0)
+		simc_write(1, s, min(count, strlen(s)));
 }
 
 static struct tty_driver* iss_console_device(struct console *c, int *index)
Signature · 1 line
2.44.0
Reviewed-by on v3 1/1
#7 lore raw

Re: [PATCH v3] tty: xtensa/iss: Use min() to fix Coccinelle warning

Quoted 6 lines · Thorsten Blum

Inline strlen() and use min() to fix the following Coccinelle/coccicheck warning reported by minmax.cocci:

WARNING opportunity for min()

Thanks.

Quoted 27 lines

---
Changes in v2:
- Use min() instead of umin() as suggested by Jiri Slaby

Changes in v3:
- Inline strlen() as suggested by Jiri Slaby

---
  arch/xtensa/platforms/iss/console.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
arch/xtensa/platforms/iss/console.c +2 -4
index 8896e691c051..abec44b687df 100644
@@ -166,10 +166,8 @@ late_initcall(rs_init);
 
  static void iss_console_write(struct console *co, const char *s, unsigned count)
  {
-	if (s && *s != 0) {
-		int len = strlen(s);
-		simc_write(1, s, count < len ? count : len);
-	}
+	if (s && *s != 0)
+		simc_write(1, s, min(count, strlen(s)));
  }
 
  static struct tty_driver* iss_console_device(struct console *c, int *index)
Signature · 2 lines
js
suse labs
maintainer
#8 lore raw

Re: [PATCH v3] tty: xtensa/iss: Use min() to fix Coccinelle warning

In reply to Thorsten Blum
Quoted 11 lines · Thorsten Blum

Inline strlen() and use min() to fix the following Coccinelle/coccicheck warning reported by minmax.cocci:

WARNING opportunity for min()

---
Changes in v2:
- Use min() instead of umin() as suggested by Jiri Slaby

Quoted 5 lines

Changes in v3:
- Inline strlen() as suggested by Jiri Slaby

---
 arch/xtensa/platforms/iss/console.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Thanks. Applied to my xtensa tree.

-- Max