From 362973902ca8501cad5e32db3820d7c66486b7a7 Mon Sep 17 00:00:00 2001 From: Evegeny Vereshchagin Date: Mon, 5 Mar 2018 21:23:33 +0000 Subject: [PATCH 1/2] tests: stop using `freopen` in `test-fileio` This helps get around a bug confusing `glibc` and making the test bail out with the following error under `asan` on `x86`: Fatal error: glibc detected an invalid stdio handle Aborted (core dumped) The bug has been reported in https://github.com/google/sanitizers/issues/778, but it is unlikely to be fixed anytime soon. --- src/test/test-fileio.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index 286867d2dc..3b4ee4b92a 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -406,7 +406,7 @@ static void test_capeff(void) { static void test_write_string_stream(void) { char fn[] = "/tmp/test-write_string_stream-XXXXXX"; - _cleanup_fclose_ FILE *f = NULL; + FILE *f = NULL; int fd; char buf[64]; @@ -416,8 +416,9 @@ static void test_write_string_stream(void) { f = fdopen(fd, "r"); assert_se(f); assert_se(write_string_stream(f, "boohoo", 0) < 0); + f = safe_fclose(f); - f = freopen(fn, "r+", f); + f = fopen(fn, "r+"); assert_se(f); assert_se(write_string_stream(f, "boohoo", 0) == 0); @@ -425,8 +426,9 @@ static void test_write_string_stream(void) { assert_se(fgets(buf, sizeof(buf), f)); assert_se(streq(buf, "boohoo\n")); + f = safe_fclose(f); - f = freopen(fn, "w+", f); + f = fopen(fn, "w+"); assert_se(f); assert_se(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE) == 0); @@ -435,6 +437,7 @@ static void test_write_string_stream(void) { assert_se(fgets(buf, sizeof(buf), f)); printf(">%s<", buf); assert_se(streq(buf, "boohoo")); + f = safe_fclose(f); unlink(fn); } From 9a92e25545210a5f4eddacbf52a3eeeadb062313 Mon Sep 17 00:00:00 2001 From: Evegeny Vereshchagin Date: Mon, 5 Mar 2018 22:05:46 +0000 Subject: [PATCH 2/2] tests: close a leftover file descriptor in `test-fileio` This should make it a bit easier to search for real file descriptor leaks. ``` $ valgrind --leak-check=full --track-fds=yes ./build/test-fileio ... ==29457== ==29457== FILE DESCRIPTORS: 4 open at exit. ==29457== Open file descriptor 3: /tmp/test-systemd_writing_tmpfile.lyV5Rc ==29457== at 0x4B9AD9E: open (open.c:43) ==29457== by 0x4B19B24: __gen_tempname (tempname.c:261) ==29457== by 0x4BA5CC3: mkostemp64 (mkostemp64.c:32) ==29457== by 0x48F739B: mkostemp_safe (fileio.c:1206) ==29457== by 0x10D968: test_writing_tmpfile (test-fileio.c:620) ==29457== by 0x10E930: main (test-fileio.c:767) ==29457== ``` --- src/test/test-fileio.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index 3b4ee4b92a..c6128c9091 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -610,7 +610,8 @@ static void test_writing_tmpfile(void) { char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX"; _cleanup_free_ char *contents = NULL; size_t size; - int fd, r; + int r; + _cleanup_close_ int fd = -1; struct iovec iov[3]; iov[0] = IOVEC_MAKE_STRING("abc\n");