diff options
author | Alexander Færøy <ahf@torproject.org> | 2020-07-09 02:16:11 +0000 |
---|---|---|
committer | Alexander Færøy <ahf@torproject.org> | 2020-07-14 17:41:51 +0000 |
commit | abe7196c53f23186f06e1c56a406ec4c9a75a4f7 (patch) | |
tree | ea5837cc885721db538faf778a107398a829fc02 /src/lib | |
parent | c9751e26119e375fcbc74107e89958957c00ee5e (diff) | |
download | tor-abe7196c53f23186f06e1c56a406ec4c9a75a4f7.tar.gz tor-abe7196c53f23186f06e1c56a406ec4c9a75a4f7.zip |
Strip '\r' characters when reading text files on Unix.
This patch ensures that we strip "\r" characters on both Windows as well
as Unix when we read text files. This should prevent the issue where
some Tor state files have been moved from a Windows machine, and thus
contains CRLF line ending, to a Unix machine where only \n is needed.
We add a test-case to ensure that we handle this properly on all our
platforms.
See: https://bugs.torproject.org/tpo/core/tor/33781
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/fs/files.c | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/src/lib/fs/files.c b/src/lib/fs/files.c index b98a51a287..55d5f1bf8c 100644 --- a/src/lib/fs/files.c +++ b/src/lib/fs/files.c @@ -685,7 +685,6 @@ read_file_to_str, (const char *filename, int flags, struct stat *stat_out)) } string[r] = '\0'; /* NUL-terminate the result. */ -#if defined(_WIN32) || defined(__CYGWIN__) if (!bin && strchr(string, '\r')) { log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped " "when reading %s. Coping.", @@ -695,8 +694,7 @@ read_file_to_str, (const char *filename, int flags, struct stat *stat_out)) } if (!bin) { statbuf.st_size = (size_t) r; - } else -#endif /* defined(_WIN32) || defined(__CYGWIN__) */ + } else { if (r != statbuf.st_size) { /* Unless we're using text mode on win32, we'd better have an exact * match for size. */ @@ -708,6 +706,7 @@ read_file_to_str, (const char *filename, int flags, struct stat *stat_out)) errno = save_errno; return NULL; } + } close(fd); if (stat_out) { memcpy(stat_out, &statbuf, sizeof(struct stat)); |