From 1171adaa21048ba8dc82169b9d79952e8754b201 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Tue, 10 Dec 2024 20:35:04 -0800 Subject: [PATCH] Fix UB in test_unistd_write_broken_link (#23125) The test was failing under ASan because no null byte was being read from the file, so the strlen inside printf was running off the buffer. It just happened to work on non-ASan because wasm memory is initialized to zero. --- test/unistd/test_unistd_write_broken_link.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/unistd/test_unistd_write_broken_link.c b/test/unistd/test_unistd_write_broken_link.c index 1c119a6e45a7e..014af7860b78f 100644 --- a/test/unistd/test_unistd_write_broken_link.c +++ b/test/unistd/test_unistd_write_broken_link.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -16,7 +17,9 @@ int main() { int target_fd = open("link_target", O_RDONLY); printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno)); char buf[10]; - read(target_fd, buf, 10); + memset(buf, 0, 10); + size_t r = read(target_fd, buf, 10); + assert(r == 3); printf("buf: '%s'\n", buf); close(target_fd); } @@ -24,7 +27,9 @@ int main() { int target_fd = open("link_source", O_RDONLY); printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno)); char buf[10]; - read(target_fd, buf, 10); + memset(buf, 0, 10); + size_t r = read(target_fd, buf, 10); + assert(r == 3); printf("buf: '%s'\n", buf); close(target_fd); }