Skip to contentSkip to conversation

kernel: debug: kdb: strncpy issue, using strlcpy instead of strncpy.

PATCH In review Chen Gang 2 participants 4 messages Apr 7, 2013 – Apr 24, 2013 KGDB / KDB /debug_core

lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 kernel: debug: kdb: strncpy issue, using strlcpy instead of strncpy.

cmd_cur and cmd_hist[] are all NUL terminated string. need using strlcpy instead of strncpy.

---
 kernel/debug/kdb/kdb_main.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
kernel/debug/kdb/kdb_main.c +4 -4
index 00eb8f7..a2b04d7 100644
@@ -1063,12 +1063,12 @@ static int handle_ctrl_cmd(char *cmd)
 	case CTRL_P:
 		if (cmdptr != cmd_tail)
 			cmdptr = (cmdptr-1) % KDB_CMD_HISTORY_COUNT;
-		strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
+		strlcpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
 		return 1;
 	case CTRL_N:
 		if (cmdptr != cmd_head)
 			cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT;
-		strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
+		strlcpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
 		return 1;
 	}
 	return 0;
@@ -1260,7 +1260,7 @@ do_full_getstr:
 		if (*cmdbuf != '\n') {
 			if (*cmdbuf < 32) {
 				if (cmdptr == cmd_head) {
-					strncpy(cmd_hist[cmd_head], cmd_cur,
+					strlcpy(cmd_hist[cmd_head], cmd_cur,
 						CMD_BUFLEN);
 					*(cmd_hist[cmd_head] +
 					  strlen(cmd_hist[cmd_head])-1) = '\0';
@@ -1270,7 +1270,7 @@ do_full_getstr:
 				cmdbuf = cmd_cur;
 				goto do_full_getstr;
 			} else {
-				strncpy(cmd_hist[cmd_head], cmd_cur,
+				strlcpy(cmd_hist[cmd_head], cmd_cur,
 					CMD_BUFLEN);
 			}
 
Signature · 1 line
1.7.7.6
author
#1 lore raw

Hello maintainers:

please help check this patch, when you have time, thanks.

Quoted 47 lines · Chen Gang

cmd_cur and cmd_hist[] are all NUL terminated string. need using strlcpy instead of strncpy.

---
 kernel/debug/kdb/kdb_main.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
kernel/debug/kdb/kdb_main.c +4 -4
index 00eb8f7..a2b04d7 100644
@@ -1063,12 +1063,12 @@ static int handle_ctrl_cmd(char *cmd)
 	case CTRL_P:
 		if (cmdptr != cmd_tail)
 			cmdptr = (cmdptr-1) % KDB_CMD_HISTORY_COUNT;
-		strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
+		strlcpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
 		return 1;
 	case CTRL_N:
 		if (cmdptr != cmd_head)
 			cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT;
-		strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
+		strlcpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
 		return 1;
 	}
 	return 0;
@@ -1260,7 +1260,7 @@ do_full_getstr:
 		if (*cmdbuf != '\n') {
 			if (*cmdbuf < 32) {
 				if (cmdptr == cmd_head) {
-					strncpy(cmd_hist[cmd_head], cmd_cur,
+					strlcpy(cmd_hist[cmd_head], cmd_cur,
 						CMD_BUFLEN);
 					*(cmd_hist[cmd_head] +
 					  strlen(cmd_hist[cmd_head])-1) = '\0';
@@ -1270,7 +1270,7 @@ do_full_getstr:
 				cmdbuf = cmd_cur;
 				goto do_full_getstr;
 			} else {
-				strncpy(cmd_hist[cmd_head], cmd_cur,
+				strlcpy(cmd_hist[cmd_head], cmd_cur,
 					CMD_BUFLEN);
 			}
 
Signature · 3 lines
Chen Gang

Asianux Corporation
#2 lore raw
Quoted 4 lines · Chen Gang

Hello maintainers:

please help check this patch, when you have time, thanks.

The patch looks good. I would add to the description: "..., otherwise the destination string might end up not being terminted with the NUL, causing all sorts of misbehaviour."

Thanks!

Quoted 54 lines · Chen Gang
Quoted 47 lines

cmd_cur and cmd_hist[] are all NUL terminated string. need using strlcpy instead of strncpy.

---
 kernel/debug/kdb/kdb_main.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
kernel/debug/kdb/kdb_main.c +4 -4
index 00eb8f7..a2b04d7 100644
@@ -1063,12 +1063,12 @@ static int handle_ctrl_cmd(char *cmd)
 	case CTRL_P:
 		if (cmdptr != cmd_tail)
 			cmdptr = (cmdptr-1) % KDB_CMD_HISTORY_COUNT;
-		strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
+		strlcpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
 		return 1;
 	case CTRL_N:
 		if (cmdptr != cmd_head)
 			cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT;
-		strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
+		strlcpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
 		return 1;
 	}
 	return 0;
@@ -1260,7 +1260,7 @@ do_full_getstr:
 		if (*cmdbuf != '\n') {
 			if (*cmdbuf < 32) {
 				if (cmdptr == cmd_head) {
-					strncpy(cmd_hist[cmd_head], cmd_cur,
+					strlcpy(cmd_hist[cmd_head], cmd_cur,
 						CMD_BUFLEN);
 					*(cmd_hist[cmd_head] +
 					  strlen(cmd_hist[cmd_head])-1) = '\0';
@@ -1270,7 +1270,7 @@ do_full_getstr:
 				cmdbuf = cmd_cur;
 				goto do_full_getstr;
 			} else {
-				strncpy(cmd_hist[cmd_head], cmd_cur,
+				strlcpy(cmd_hist[cmd_head], cmd_cur,
 					CMD_BUFLEN);
 			}
 
Signature · 3 lines
Chen Gang

Asianux Corporation
author
#3 lore raw
Quoted 5 lines · Anton Vorontsov
Quoted 4 lines · Chen Gang
Quoted 4 lines

Hello maintainers:

please help check this patch, when you have time, thanks.

Quoted 8 lines

The patch looks good. I would add to the description: "..., otherwise the destination string might end up not being terminted with the NUL, causing all sorts of misbehaviour."

Thanks!

Thanks, also thank you for your additional description.

Signature · 3 lines
Chen Gang

Asianux Corporation