From c23b3ff44948416d67ff30f38116bed3b2efb6e5 Mon Sep 17 00:00:00 2001 From: Eli Lipsitz Date: Fri, 20 Jan 2023 19:51:11 +0000 Subject: [PATCH] init: Remove trailing newline when parsing inittab Commit 05e4f52c3c modified init to use the standard getline instead of get_line when parsing the inittab. However, these functions have different behavior that wasn't properly accounted for: get_line strips the trailing newline, and getline keeps it (if it exists, i.e. not EOF). This led to a bug where every command in the inittab is parsed as ending with "\n". This commit corrects init to remove the newline, fixing this issue. --- toys/pending/init.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/toys/pending/init.c b/toys/pending/init.c index 05afb718..9f4e7ba6 100644 --- a/toys/pending/init.c +++ b/toys/pending/init.c @@ -124,6 +124,7 @@ static void parse_inittab(void) { char *line = 0; size_t allocated_length = 0; + ssize_t line_length = 0; int line_number = 0; char *act_name = "sysinit\0wait\0once\0respawn\0askfirst\0ctrlaltdel\0" "shutdown\0restart\0"; @@ -136,10 +137,11 @@ static void parse_inittab(void) return; } - while (getline(&line, &allocated_length, fp) > 0) { + while ((line_length = getline(&line, &allocated_length, fp)) > 0) { char *p = line, *x, *tty_name = 0, *command = 0, *extracted_token, *tmp; int action = 0, token_count = 0, i; + if (p[line_length - 1] == '\n') p[line_length - 1] = '\0'; if ((x = strchr(p, '#'))) *x = '\0'; line_number++; action = 0; -- 2.39.2