aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-04 21:41:32 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-05 09:46:05 +0200
commit9a20f40bb5e7106f3fa59affbbb81f30337ada6b (patch)
treea6648c6477caa3ff162fa06c322c641a6258dd1e /lib
parent8e86f894afe775e39b8d02c95dd55f97a3465d27 (diff)
libio: istream_get_line: fix trailing CR sanitation
The istream_get_line function is supposed to remove the line break, inclduing CR+LF if used. The previous implementation simply checked the buffer when the LF was found, missing the case where the CR was alraedy added and the LF is at the beginning of the newly loaded data. This commit modifies the check to inspect the line itself when it was found, and remove the CR after the fact. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/io/src/get_line.c7
-rw-r--r--lib/io/test/get_line.c2
2 files changed, 4 insertions, 5 deletions
diff --git a/lib/io/src/get_line.c b/lib/io/src/get_line.c
index 19af208..1159037 100644
--- a/lib/io/src/get_line.c
+++ b/lib/io/src/get_line.c
@@ -71,10 +71,6 @@ int istream_get_line(istream_t *strm, char **out,
if (i < strm->buffer_used) {
count = i++;
-
- if (count > 0 && strm->buffer[count - 1] == '\r')
- --count;
-
have_line = true;
} else {
count = i;
@@ -93,6 +89,9 @@ int istream_get_line(istream_t *strm, char **out,
strm->buffer_used -= i;
if (have_line) {
+ if (line_len > 0 && line[line_len - 1] == '\r')
+ line[--line_len] = '\0';
+
line_len = trim(line, flags);
if (line_len == 0 &&
diff --git a/lib/io/test/get_line.c b/lib/io/test/get_line.c
index bb06b3e..0ce67ac 100644
--- a/lib/io/test/get_line.c
+++ b/lib/io/test/get_line.c
@@ -20,7 +20,7 @@ static void run_test_case(const char *raw, const line_t *lines, size_t count,
char *line;
int ret;
- fp = istream_memory_create("lines.txt", 512, raw, strlen(raw));
+ fp = istream_memory_create("lines.txt", 2, raw, strlen(raw));
TEST_NOT_NULL(fp);
line_num = 1;