From f3aae6b7c256b1d9faff96f957f32886643bbaa8 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 31 Jan 2023 08:22:10 +0000 Subject: libbb: fix CRLF handling Ensure a trailing CR is only removed if it precedes a LF. The two cases at issue are intended to read complete lines and remove the line terminator. In the normal case a trailing LF will be present so removing the CR unconditionally worked. However, if the last line of a file was missing its LF or if a NUL was detected (in the case of xmalloc_fgetline()) the CR might have been removed without justification. --- libbb/get_line_from_file.c | 9 +++++++-- libbb/parse_config.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index ce5c7810e..9f20f68cf 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c @@ -66,11 +66,16 @@ char* FAST_FUNC xmalloc_fgetline(FILE *file) size_t i; char *c = bb_get_chunk_from_file(file, &i); +#if !ENABLE_PLATFORM_MINGW32 if (i && c[--i] == '\n') c[i] = '\0'; -#if ENABLE_PLATFORM_MINGW32 - if (i && c[--i] == '\r') +#else + if (i && c[--i] == '\n') { c[i] = '\0'; + if (i && c[--i] == '\r') { + c[i] = '\0'; + } + } #endif return c; diff --git a/libbb/parse_config.c b/libbb/parse_config.c index f7a2b81b9..800a2935e 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c @@ -113,11 +113,16 @@ static int get_line_with_continuation(parser_t *parser) line = parser->line; for (;;) { parser->lineno++; +#if ENABLE_PLATFORM_MINGW32 if (line[len - 1] == '\n') len--; -#if ENABLE_PLATFORM_MINGW32 - if (line[len - 1] == '\r') +#else + if (line[len - 1] == '\n') { len--; + if (len != 0 && line[len - 1] == '\r') { + len--; + } + } #endif if (len == 0 || line[len - 1] != '\\') break; -- cgit v1.2.3-55-g6feb