diff options
author | Elliott Hughes <enh@google.com> | 2021-02-08 20:07:12 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2021-02-08 20:07:12 +0000 |
commit | 6d5662d22a38ca6200d6b6dd919204bc93dbaba8 (patch) | |
tree | 3acd79e25463207e38f2f3e9964ae2ba0e982aac /tests/stdio_test.cpp | |
parent | 3990193d7fcefce1837fac47b56094ee3e626d06 (diff) | |
parent | f9cfecf3d4d848e247dc8bdc486ed227bff7315f (diff) |
Merge "Fix freopen() where the path is null."
Diffstat (limited to 'tests/stdio_test.cpp')
-rw-r--r-- | tests/stdio_test.cpp | 83 |
1 files changed, 67 insertions, 16 deletions
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp index 5680f9556..afbbf2686 100644 --- a/tests/stdio_test.cpp +++ b/tests/stdio_test.cpp @@ -1956,34 +1956,64 @@ TEST(STDIO_TEST, open_memstream_EINVAL) { #endif } -TEST(STDIO_TEST, fdopen_CLOEXEC) { - int fd = open("/proc/version", O_RDONLY); - ASSERT_TRUE(fd != -1); - +TEST(STDIO_TEST, fdopen_add_CLOEXEC) { // This fd doesn't have O_CLOEXEC... - AssertCloseOnExec(fd, false); - - FILE* fp = fdopen(fd, "re"); - ASSERT_TRUE(fp != nullptr); - + int fd = open("/proc/version", O_RDONLY); + ASSERT_FALSE(CloseOnExec(fd)); // ...but the new one does. - AssertCloseOnExec(fileno(fp), true); + FILE* fp = fdopen(fd, "re"); + ASSERT_TRUE(CloseOnExec(fileno(fp))); + fclose(fp); +} +TEST(STDIO_TEST, fdopen_remove_CLOEXEC) { + // This fd has O_CLOEXEC... + int fd = open("/proc/version", O_RDONLY | O_CLOEXEC); + ASSERT_TRUE(CloseOnExec(fd)); + // ...but the new one doesn't. + FILE* fp = fdopen(fd, "r"); + ASSERT_TRUE(CloseOnExec(fileno(fp))); fclose(fp); } -TEST(STDIO_TEST, freopen_CLOEXEC) { +TEST(STDIO_TEST, freopen_add_CLOEXEC) { + // This FILE* doesn't have O_CLOEXEC... FILE* fp = fopen("/proc/version", "r"); - ASSERT_TRUE(fp != nullptr); + ASSERT_FALSE(CloseOnExec(fileno(fp))); + // ...but the new one does. + fp = freopen("/proc/version", "re", fp); + ASSERT_TRUE(CloseOnExec(fileno(fp))); - // This FILE* doesn't have O_CLOEXEC... - AssertCloseOnExec(fileno(fp), false); + fclose(fp); +} - fp = freopen("/proc/version", "re", fp); +TEST(STDIO_TEST, freopen_remove_CLOEXEC) { + // This FILE* has O_CLOEXEC... + FILE* fp = fopen("/proc/version", "re"); + ASSERT_TRUE(CloseOnExec(fileno(fp))); + // ...but the new one doesn't. + fp = freopen("/proc/version", "r", fp); + ASSERT_FALSE(CloseOnExec(fileno(fp))); + fclose(fp); +} +TEST(STDIO_TEST, freopen_null_filename_add_CLOEXEC) { + // This FILE* doesn't have O_CLOEXEC... + FILE* fp = fopen("/proc/version", "r"); + ASSERT_FALSE(CloseOnExec(fileno(fp))); // ...but the new one does. - AssertCloseOnExec(fileno(fp), true); + fp = freopen(nullptr, "re", fp); + ASSERT_TRUE(CloseOnExec(fileno(fp))); + fclose(fp); +} +TEST(STDIO_TEST, freopen_null_filename_remove_CLOEXEC) { + // This FILE* has O_CLOEXEC... + FILE* fp = fopen("/proc/version", "re"); + ASSERT_TRUE(CloseOnExec(fileno(fp))); + // ...but the new one doesn't. + fp = freopen(nullptr, "r", fp); + ASSERT_FALSE(CloseOnExec(fileno(fp))); fclose(fp); } @@ -2883,3 +2913,24 @@ TEST(STDIO_TEST, tmpnam_buf) { char buf[L_tmpnam]; tmpnam_test(buf); } + +TEST(STDIO_TEST, freopen_null_filename_mode) { + TemporaryFile tf; + FILE* fp = fopen(tf.path, "r"); + ASSERT_TRUE(fp != nullptr); + + // "r" = O_RDONLY + char buf[1]; + ASSERT_EQ(0, read(fileno(fp), buf, 1)); + ASSERT_EQ(-1, write(fileno(fp), "hello", 1)); + // "r+" = O_RDWR + fp = freopen(nullptr, "r+", fp); + ASSERT_EQ(0, read(fileno(fp), buf, 1)); + ASSERT_EQ(1, write(fileno(fp), "hello", 1)); + // "w" = O_WRONLY + fp = freopen(nullptr, "w", fp); + ASSERT_EQ(-1, read(fileno(fp), buf, 1)); + ASSERT_EQ(1, write(fileno(fp), "hello", 1)); + + fclose(fp); +} |