Use atoi() in memcpy_chk fortify test.
When calling __memcpy_chk() with a literal -1 argument, the compiler may
(and does, in practice) optimize away the check and can call memcpy()
directly instead. This defeats the purpose of the test, and a defective
__memcpy_chk() implementation (e.g. in assembly) can pass the test.
This can be validated by changing the x86 __memcpy_chk comparison
opcode from `ja` (unsigned compare) to `jg` (signed compare). Without
the atoi(), the test still passes even with the wrong implementation.
To fix this, use atoi() to parse the size from a string, similar to
other tests in this file. This ensures the actual __memcpy_chk()
implementation gets executed.
Bug: 389669171
Test: ./tests/run-on-host.sh 32 --gtest_filter='*memcpy*'
Test: ./tests/run-on-host.sh 64 --gtest_filter='*memcpy*'
Change-Id: I69e9df72d5071925cc3dee44863dc0ca66e243d4
diff --git a/tests/fortify_test.cpp b/tests/fortify_test.cpp
index 7b64fbf..64194c8 100644
--- a/tests/fortify_test.cpp
+++ b/tests/fortify_test.cpp
@@ -958,7 +958,8 @@
TEST(TEST_NAME, memcpy_chk_max_int_size) {
char buf[10];
- void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
+ size_t buf_size = atoi("-1");
+ void* res = __memcpy_chk(buf, "012345678", sizeof(buf), buf_size);
ASSERT_EQ((void*)buf, res);
ASSERT_EQ('0', buf[0]);
ASSERT_EQ('1', buf[1]);