Skip to contentSkip to conversation

kdb: Fix bound check compiler warning

PATCH Superseded Wenlin Kang 2 participants 6 messages May 8, 2019 – May 13, 2019 KGDB / KDB /debug_core

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kdb: Fix bound check compiler warning

The strncpy() function may leave the destination string buffer unterminated, better use strlcpy() instead.

This fixes the following warning with gcc 8.2:

kernel/debug/kdb/kdb_io.c: In function 'kdb_getstr':
kernel/debug/kdb/kdb_io.c:449:3: warning: 'strncpy' specified bound 256 equals destination size [-Wstringop-truncation]
strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
 kernel/debug/kdb/kdb_io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/debug/kdb/kdb_io.c +1 -1
index 6a4b414..7fd4513 100644
@@ -446,7 +446,7 @@ static char *kdb_read(char *buffer, size_t bufsize)
 char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
 {
 	if (prompt && kdb_prompt_str != prompt)
-		strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
+		strlcpy(kdb_prompt_str, prompt, CMD_BUFLEN);
 	kdb_printf(kdb_prompt_str);
 	kdb_nextline = 1;	/* Prompt and input resets line number */
 	return kdb_read(buffer, bufsize);
Signature · 1 line
1.9.1
#1 lore raw
Quoted 20 lines · Wenlin Kang

The strncpy() function may leave the destination string buffer unterminated, better use strlcpy() instead.

This fixes the following warning with gcc 8.2:

kernel/debug/kdb/kdb_io.c: In function 'kdb_getstr':
kernel/debug/kdb/kdb_io.c:449:3: warning: 'strncpy' specified bound 256 equals destination size [-Wstringop-truncation]

   strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 kernel/debug/kdb/kdb_io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/debug/kdb/kdb_io.c +0 -0
index 6a4b414..7fd4513 100644

kernel/debug/kdb/kdb_io.c static char *kdb_read(char *buffer, size_t bufsize) · line 449

Quoted 5 lines
kernel/debug/kdb/kdb_io.c +1 -1
@@ -446,7 +446,7 @@ static char *kdb_read(char *buffer, size_t bufsize)
 char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
 {
 	if (prompt && kdb_prompt_str != prompt)
-		strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
+		strlcpy(kdb_prompt_str, prompt, CMD_BUFLEN);

Shouldn't that be strscpy?

Daniel.

Quoted 6 lines
kernel/debug/kdb/kdb_io.c +0 -0
 	kdb_printf(kdb_prompt_str);
 	kdb_nextline = 1;	/* Prompt and input resets line number */
 	return kdb_read(buffer, bufsize);
Signature · 1 line
1.9.1
#2 lore raw
Quoted 16 lines · Daniel Thompson
Quoted 15 lines · Wenlin Kang

The strncpy() function may leave the destination string buffer unterminated, better use strlcpy() instead.

This fixes the following warning with gcc 8.2:

kernel/debug/kdb/kdb_io.c: In function 'kdb_getstr':
kernel/debug/kdb/kdb_io.c:449:3: warning: 'strncpy' specified bound 256 equals destination size [-Wstringop-truncation]

    strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
  kernel/debug/kdb/kdb_io.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines

kernel/debug/kdb/kdb_io.c static char *kdb_read(char *buffer, size_t bufsize) · line 449

Quoted 10 lines
kernel/debug/kdb/kdb_io.c +1 -1
index 6a4b414..7fd4513 100644
@@ -446,7 +446,7 @@ static char *kdb_read(char *buffer, size_t bufsize)
  char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
  {
  	if (prompt && kdb_prompt_str != prompt)
-		strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
+		strlcpy(kdb_prompt_str, prompt, CMD_BUFLEN);

Shouldn't that be strscpy?

Hi Daniel

I thought about strscpy, but I think strlcpy is better, because it only copy the real number of characters if src string less than that size.

Quoted 10 lines

Daniel.

Quoted 6 lines
kernel/debug/kdb/kdb_io.c +0 -0
  	kdb_printf(kdb_prompt_str);
  	kdb_nextline = 1;	/* Prompt and input resets line number */
  	return kdb_read(buffer, bufsize);
Signature · 1 line
1.9.1
Signature · 2 lines
Thanks,
Wenlin Kang
#3 lore raw
Quoted 30 lines · Wenlin Kang
Quoted 16 lines · Daniel Thompson
Quoted 15 lines · Wenlin Kang

The strncpy() function may leave the destination string buffer unterminated, better use strlcpy() instead.

This fixes the following warning with gcc 8.2:

kernel/debug/kdb/kdb_io.c: In function 'kdb_getstr':
kernel/debug/kdb/kdb_io.c:449:3: warning: 'strncpy' specified bound 256 equals destination size [-Wstringop-truncation]

    strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
  kernel/debug/kdb/kdb_io.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines

kernel/debug/kdb/kdb_io.c static char *kdb_read(char *buffer, size_t bufsize) · line 449

Quoted 10 lines
kernel/debug/kdb/kdb_io.c +1 -1
index 6a4b414..7fd4513 100644
@@ -446,7 +446,7 @@ static char *kdb_read(char *buffer, size_t bufsize)
  char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
  {
  	if (prompt && kdb_prompt_str != prompt)
-		strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
+		strlcpy(kdb_prompt_str, prompt, CMD_BUFLEN);

Shouldn't that be strscpy?

Quoted 4 lines

Hi Daniel

I thought about strscpy, but I think strlcpy is better, because it only copy the real number of characters if src string less than that size.

Sorry, I'm confused by this. What behavior does strscpy() have that you consider undesirable in this case?

Daniel.

#4 lore raw
Quoted 30 lines · Daniel Thompson
Quoted 29 lines · Wenlin Kang
Quoted 27 lines · Daniel Thompson
Quoted 20 lines · Wenlin Kang

The strncpy() function may leave the destination string buffer unterminated, better use strlcpy() instead.

This fixes the following warning with gcc 8.2:

kernel/debug/kdb/kdb_io.c: In function 'kdb_getstr':
kernel/debug/kdb/kdb_io.c:449:3: warning: 'strncpy' specified bound 256 equals destination size [-Wstringop-truncation]

     strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
   kernel/debug/kdb/kdb_io.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
kernel/debug/kdb/kdb_io.c +0 -0
index 6a4b414..7fd4513 100644

kernel/debug/kdb/kdb_io.c static char *kdb_read(char *buffer, size_t bufsize) · line 449

Quoted 5 lines
kernel/debug/kdb/kdb_io.c +1 -1
@@ -446,7 +446,7 @@ static char *kdb_read(char *buffer, size_t bufsize)
   char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
   {
   	if (prompt && kdb_prompt_str != prompt)
-		strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
+		strlcpy(kdb_prompt_str, prompt, CMD_BUFLEN);

Shouldn't that be strscpy?

Quoted 6 lines
Quoted 4 lines

Hi Daniel

I thought about strscpy, but I think strlcpy is better, because it only copy the real number of characters if src string less than that size.

Sorry, I'm confused by this. What behavior does strscpy() have that you consider undesirable in this case?

Hi Daniel

I checked strscpy() again, and think either is fine to me, if you think strscpy() is better, I can change it to this, and send v2, thanks for your review.

Quoted 3 lines

Daniel.

Signature · 2 lines
Thanks,
Wenlin Kang
#5 lore raw
Quoted 39 lines · Wenlin Kang
Quoted 30 lines · Daniel Thompson
Quoted 29 lines · Wenlin Kang
Quoted 27 lines · Daniel Thompson
Quoted 20 lines · Wenlin Kang

The strncpy() function may leave the destination string buffer unterminated, better use strlcpy() instead.

This fixes the following warning with gcc 8.2:

kernel/debug/kdb/kdb_io.c: In function 'kdb_getstr':
kernel/debug/kdb/kdb_io.c:449:3: warning: 'strncpy' specified bound 256 equals destination size [-Wstringop-truncation]

     strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
   kernel/debug/kdb/kdb_io.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
kernel/debug/kdb/kdb_io.c +0 -0
index 6a4b414..7fd4513 100644

kernel/debug/kdb/kdb_io.c static char *kdb_read(char *buffer, size_t bufsize) · line 449

Quoted 5 lines
kernel/debug/kdb/kdb_io.c +1 -1
@@ -446,7 +446,7 @@ static char *kdb_read(char *buffer, size_t bufsize)
   char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
   {
   	if (prompt && kdb_prompt_str != prompt)
-		strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
+		strlcpy(kdb_prompt_str, prompt, CMD_BUFLEN);

Shouldn't that be strscpy?

Quoted 6 lines
Quoted 4 lines

Hi Daniel

I thought about strscpy, but I think strlcpy is better, because it only copy the real number of characters if src string less than that size.

Sorry, I'm confused by this. What behavior does strscpy() have that you consider undesirable in this case?

Quoted 5 lines

Hi Daniel

I checked strscpy() again, and think either is fine to me, if you think strscpy() is better, I can change it to this, and send v2, thanks for your review.

I think strscpy() is better.

Daniel.

Quoted 10 lines
Quoted 5 lines

Daniel.

Signature · 2 lines
Thanks,
Wenlin Kang