[libc-commits] [libc] 0f1da49 - [libc] Fix readlink tests on 32-bit systems (#97850)
via libc-commits
libc-commits at lists.llvm.org
Fri Jul 5 11:20:55 PDT 2024
Author: Mikhail R. Gadelha
Date: 2024-07-05T15:20:51-03:00
New Revision: 0f1da49b4d854ce7c6572000da3fb6cb0a1245d2
URL: https://github.com/llvm/llvm-project/commit/0f1da49b4d854ce7c6572000da3fb6cb0a1245d2
DIFF: https://github.com/llvm/llvm-project/commit/0f1da49b4d854ce7c6572000da3fb6cb0a1245d2.diff
LOG: [libc] Fix readlink tests on 32-bit systems (#97850)
Use sizeof in a string literal instead of a CString so we get the right size when creating the buf array.
We also now use strlen(FILENAME) to get the string lenght when calling readlink and readlinkat.
Added:
Modified:
libc/test/src/unistd/CMakeLists.txt
libc/test/src/unistd/readlink_test.cpp
libc/test/src/unistd/readlinkat_test.cpp
Removed:
################################################################################
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index de3e8d9ccbb62..f4f78b800987d 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -262,6 +262,7 @@ add_libc_unittest(
libc.include.unistd
libc.src.errno.errno
libc.src.unistd.readlink
+ libc.src.string.string_utils
libc.src.unistd.symlink
libc.src.unistd.unlink
libc.src.__support.CPP.string_view
@@ -278,6 +279,7 @@ add_libc_unittest(
libc.include.fcntl
libc.include.unistd
libc.src.errno.errno
+ libc.src.string.string_utils
libc.src.unistd.readlinkat
libc.src.unistd.symlink
libc.src.unistd.unlink
diff --git a/libc/test/src/unistd/readlink_test.cpp b/libc/test/src/unistd/readlink_test.cpp
index 20f3951349118..0760850d9bae1 100644
--- a/libc/test/src/unistd/readlink_test.cpp
+++ b/libc/test/src/unistd/readlink_test.cpp
@@ -9,6 +9,7 @@
#include "src/__support/CPP/string_view.h"
#include "src/errno/libc_errno.h"
#include "src/unistd/readlink.h"
+#include "src/string/string_utils.h"
#include "src/unistd/symlink.h"
#include "src/unistd/unlink.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
@@ -30,8 +31,9 @@ TEST(LlvmLibcReadlinkTest, CreateAndUnlink) {
// 3. Cleanup the symlink created in step #1.
ASSERT_THAT(LIBC_NAMESPACE::symlink(LINK_VAL, LINK), Succeeds(0));
- char buf[sizeof(LINK_VAL)];
- ssize_t len = LIBC_NAMESPACE::readlink(LINK, buf, sizeof(buf));
+ char buf[sizeof(FILENAME)];
+ ssize_t len = LIBC_NAMESPACE::readlink(
+ LINK, buf, LIBC_NAMESPACE::internal::string_length(FILENAME));
ASSERT_ERRNO_SUCCESS();
ASSERT_EQ(cpp::string_view(buf, len), cpp::string_view(LINK_VAL));
@@ -40,7 +42,8 @@ TEST(LlvmLibcReadlinkTest, CreateAndUnlink) {
TEST(LlvmLibcReadlinkTest, ReadlinkInNonExistentPath) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
- char buf[8];
- ASSERT_THAT(LIBC_NAMESPACE::readlink("non-existent-link", buf, sizeof(buf)),
+ constexpr auto len = 8;
+ char buf[len];
+ ASSERT_THAT(LIBC_NAMESPACE::readlink("non-existent-link", buf, len),
Fails(ENOENT));
}
diff --git a/libc/test/src/unistd/readlinkat_test.cpp b/libc/test/src/unistd/readlinkat_test.cpp
index 39d81d9ba544a..61e87731c9b9d 100644
--- a/libc/test/src/unistd/readlinkat_test.cpp
+++ b/libc/test/src/unistd/readlinkat_test.cpp
@@ -9,6 +9,7 @@
#include "src/__support/CPP/string_view.h"
#include "src/errno/libc_errno.h"
#include "src/unistd/readlinkat.h"
+#include "src/string/string_utils.h"
#include "src/unistd/symlink.h"
#include "src/unistd/unlink.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
@@ -32,8 +33,9 @@ TEST(LlvmLibcReadlinkatTest, CreateAndUnlink) {
// 3. Cleanup the symlink created in step #1.
ASSERT_THAT(LIBC_NAMESPACE::symlink(LINK_VAL, LINK), Succeeds(0));
- char buf[sizeof(LINK_VAL)];
- ssize_t len = LIBC_NAMESPACE::readlinkat(AT_FDCWD, LINK, buf, sizeof(buf));
+ char buf[sizeof(FILENAME)];
+ ssize_t len = LIBC_NAMESPACE::readlinkat(
+ AT_FDCWD, LINK, buf, LIBC_NAMESPACE::internal::string_length(FILENAME));
ASSERT_ERRNO_SUCCESS();
ASSERT_EQ(cpp::string_view(buf, len), cpp::string_view(LINK_VAL));
@@ -42,8 +44,9 @@ TEST(LlvmLibcReadlinkatTest, CreateAndUnlink) {
TEST(LlvmLibcReadlinkatTest, ReadlinkInNonExistentPath) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
- char buf[8];
- ASSERT_THAT(LIBC_NAMESPACE::readlinkat(AT_FDCWD, "non-existent-link", buf,
- sizeof(buf)),
- Fails(ENOENT));
+ constexpr auto len = 8;
+ char buf[len];
+ ASSERT_THAT(
+ LIBC_NAMESPACE::readlinkat(AT_FDCWD, "non-existent-link", buf, len),
+ Fails(ENOENT));
}
More information about the libc-commits
mailing list