Skip to content

Commit bf12191

Browse files
committed
replace strcpy() function to prevent buffer overflow
1 parent 407ef92 commit bf12191

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

kirc.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,17 @@ kbhit(void) {
5858
}
5959

6060
/* handle keyboard strokes for command input */
61-
static char *
62-
input_handler() {
61+
static void
62+
input_handler(char *usrin, int len) {
6363

64-
char *usrin = malloc(sizeof(char) * (IRC_MSG_MAX + 1));
6564
struct termios tp, save;
6665

6766
tcgetattr(STDIN_FILENO, &tp);
6867
save = tp;
6968
tp.c_cc[VERASE] = 127;
7069
tcsetattr(STDIN_FILENO, TCSANOW, &tp);
71-
fgets(usrin, IRC_MSG_MAX, stdin);
70+
fgets(usrin, len, stdin);
7271
tcsetattr(STDIN_FILENO, TCSANOW, &save);
73-
74-
return usrin;
7572
}
7673

7774
/* send command to irc server */
@@ -235,7 +232,7 @@ main(int argc, char **argv) {
235232
while (waitpid(pid, NULL, WNOHANG) == 0) {
236233
if (!kbhit()) dprintf(fd[1], ":\n");
237234
else {
238-
strcpy(usrin, input_handler());
235+
input_handler(usrin, IRC_MSG_MAX);
239236

240237
if (sscanf(usrin, ":%[M] %s %[^\n]\n", &c1, v2, v1) == 3 ||
241238
sscanf(usrin, ":%[Qnjpm] %[^\n]\n", &c1, v1) == 2 ||

0 commit comments

Comments
 (0)