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 /libbb/get_line_from_file.c | |
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.
Diffstat (limited to 'libbb/get_line_from_file.c')
-rw-r--r-- | libbb/get_line_from_file.c | 9 |
1 files changed, 7 insertions, 2 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; |