Skip to contentSkip to conversation

common: cli_simple: use strncpy instead of strcpy

PATCH New Peng Fan 2 participants 3 messages Jan 9, 2016 – Jan 10, 2016

lore
Share & tools
lore

Peng Fan

author
#0 lore raw

PATCH 1/1 common: cli_simple: use strncpy instead of strcpy

Report Coverity log:
Destination buffer too small (STRING_OVERFLOW)
string_overflow: You might overrun the 1024 byte destination string lastcommand by writing 1025 bytes from console_buffer

Cc Heiko Schocher <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
 common/cli_simple.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
common/cli_simple.c +2 -1
index 9c3d073..c51f963 100644
@@ -276,7 +276,8 @@ void cli_simple_loop(void)
 
 		flag = 0;	/* assume no special flags for now */
 		if (len > 0)
-			strcpy(lastcommand, console_buffer);
+			strncpy(lastcommand, console_buffer,
+				CONFIG_SYS_CBSIZE + 1);
 		else if (len == 0)
 			flag |= CMD_FLAG_REPEAT;
 #ifdef CONFIG_BOOT_RETRY_TIME
Signature · 1 line
2.6.2
#1 lore raw

PATCH 1/1 common: cli_simple: use strncpy instead of strcpy

Quoted 18 lines · Peng Fan

Report Coverity log:
Destination buffer too small (STRING_OVERFLOW)
string_overflow: You might overrun the 1024 byte destination string lastcommand by writing 1025 bytes from console_buffer

Cc Heiko Schocher <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
 common/cli_simple.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
common/cli_simple.c +0 -0
index 9c3d073..c51f963 100644

common/cli_simple.c void cli_simple_loop(void) · line 283

Quoted 9 lines
common/cli_simple.c +2 -1
@@ -276,7 +276,8 @@ void cli_simple_loop(void)
 
 		flag = 0;	/* assume no special flags for now */
 		if (len > 0)
-			strcpy(lastcommand, console_buffer);
+			strncpy(lastcommand, console_buffer,
+				CONFIG_SYS_CBSIZE + 1);
 		else if (len == 0)
 			flag |= CMD_FLAG_REPEAT;
 #ifdef CONFIG_BOOT_RETRY_TIME

So, long term I would like to see use move to using strlcpy for the normal case (it might not make sense when working with various defined protocols, etc). Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160109/2830526c/attachment.sig>
author
#2 lore raw

PATCH 1/1 common: cli_simple: use strncpy instead of strcpy

Quoted 21 lines · Tom Rini
Quoted 19 lines · Peng Fan

Report Coverity log:
Destination buffer too small (STRING_OVERFLOW)
string_overflow: You might overrun the 1024 byte destination string lastcommand by writing 1025 bytes from console_buffer

Cc Heiko Schocher <[email protected]>
Cc Simon Glass <[email protected]>
Cc Tom Rini <[email protected]>
---
 common/cli_simple.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
common/cli_simple.c +0 -0
index 9c3d073..c51f963 100644
@@ -276,7 +276,8 @@ void cli_simple_loop(void)
 
Quoted 12 lines

common/cli_simple.c void cli_simple_loop(void) · line 283

Quoted 8 lines
common/cli_simple.c +2 -1
 		flag = 0;	/* assume no special flags for now */
 		if (len > 0)
-			strcpy(lastcommand, console_buffer);
+			strncpy(lastcommand, console_buffer,
+				CONFIG_SYS_CBSIZE + 1);
 		else if (len == 0)
 			flag |= CMD_FLAG_REPEAT;
 #ifdef CONFIG_BOOT_RETRY_TIME

So, long term I would like to see use move to using strlcpy for the normal case (it might not make sense when working with various defined protocols, etc). Thanks!

Thanks. Just read this, https://www.sudo.ws/todd/papers/strlcpy.html.
strlcpy is a better choice.

Thanks,
Peng.

Quoted 3 lines
Signature · 2 lines
-- 
Tom