diff options
author | Ron Yorston <rmy@pobox.com> | 2023-01-31 08:22:10 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-01-31 08:22:10 +0000 |
commit | f3aae6b7c256b1d9faff96f957f32886643bbaa8 (patch) | |
tree | 18d08897a507cb7866f309ec996268cb4cf0b2dd | |
parent | b9a1b02dee2b82e4e4a580c446a1e022aa96166d (diff) | |
download | busybox-w32-f3aae6b7c256b1d9faff96f957f32886643bbaa8.tar.gz busybox-w32-f3aae6b7c256b1d9faff96f957f32886643bbaa8.tar.bz2 busybox-w32-f3aae6b7c256b1d9faff96f957f32886643bbaa8.zip |
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.
-rw-r--r-- | libbb/get_line_from_file.c | 9 | ||||
-rw-r--r-- | 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) | |||
66 | size_t i; | 66 | size_t i; |
67 | char *c = bb_get_chunk_from_file(file, &i); | 67 | char *c = bb_get_chunk_from_file(file, &i); |
68 | 68 | ||
69 | #if !ENABLE_PLATFORM_MINGW32 | ||
69 | if (i && c[--i] == '\n') | 70 | if (i && c[--i] == '\n') |
70 | c[i] = '\0'; | 71 | c[i] = '\0'; |
71 | #if ENABLE_PLATFORM_MINGW32 | 72 | #else |
72 | if (i && c[--i] == '\r') | 73 | if (i && c[--i] == '\n') { |
73 | c[i] = '\0'; | 74 | c[i] = '\0'; |
75 | if (i && c[--i] == '\r') { | ||
76 | c[i] = '\0'; | ||
77 | } | ||
78 | } | ||
74 | #endif | 79 | #endif |
75 | 80 | ||
76 | return c; | 81 | 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) | |||
113 | line = parser->line; | 113 | line = parser->line; |
114 | for (;;) { | 114 | for (;;) { |
115 | parser->lineno++; | 115 | parser->lineno++; |
116 | #if ENABLE_PLATFORM_MINGW32 | ||
116 | if (line[len - 1] == '\n') | 117 | if (line[len - 1] == '\n') |
117 | len--; | 118 | len--; |
118 | #if ENABLE_PLATFORM_MINGW32 | 119 | #else |
119 | if (line[len - 1] == '\r') | 120 | if (line[len - 1] == '\n') { |
120 | len--; | 121 | len--; |
122 | if (len != 0 && line[len - 1] == '\r') { | ||
123 | len--; | ||
124 | } | ||
125 | } | ||
121 | #endif | 126 | #endif |
122 | if (len == 0 || line[len - 1] != '\\') | 127 | if (len == 0 || line[len - 1] != '\\') |
123 | break; | 128 | break; |