2012-11-02 20:36:11 +08:00
|
|
|
//===-- sanitizer_printf_test.cc ------------------------------------------===//
|
2012-05-10 22:18:22 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-11-02 20:36:11 +08:00
|
|
|
// Tests for sanitizer_printf.cc
|
2012-05-10 22:18:22 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-11-02 20:36:11 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2012-05-10 22:18:22 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2012-11-02 20:36:11 +08:00
|
|
|
namespace __sanitizer {
|
2012-05-10 22:18:22 +08:00
|
|
|
|
|
|
|
TEST(Printf, Basic) {
|
|
|
|
char buf[1024];
|
2012-06-19 17:21:57 +08:00
|
|
|
uptr len = internal_snprintf(buf, sizeof(buf),
|
2012-06-06 21:11:29 +08:00
|
|
|
"a%db%zdc%ue%zuf%xh%zxq%pe%sr",
|
|
|
|
(int)-1, (long)-2, // NOLINT
|
|
|
|
(unsigned)-4, (unsigned long)5, // NOLINT
|
|
|
|
(unsigned)10, (unsigned long)11, // NOLINT
|
2012-05-10 22:18:22 +08:00
|
|
|
(void*)0x123, "_string_");
|
|
|
|
EXPECT_EQ(len, strlen(buf));
|
2014-12-12 15:08:12 +08:00
|
|
|
|
|
|
|
std::string expectedString = "a-1b-2c4294967292e5fahbq0x";
|
|
|
|
expectedString += std::string(SANITIZER_POINTER_FORMAT_LENGTH - 3, '0');
|
|
|
|
expectedString += "123e_string_r";
|
|
|
|
EXPECT_STREQ(expectedString.c_str(), buf);
|
2012-05-10 22:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Printf, OverflowStr) {
|
|
|
|
char buf[] = "123456789";
|
2012-06-19 17:21:57 +08:00
|
|
|
uptr len = internal_snprintf(buf, 4, "%s", "abcdef"); // NOLINT
|
2012-05-10 22:18:22 +08:00
|
|
|
EXPECT_EQ(len, (uptr)6);
|
2012-11-02 20:36:11 +08:00
|
|
|
EXPECT_STREQ("abc", buf);
|
2012-05-10 22:18:22 +08:00
|
|
|
EXPECT_EQ(buf[3], 0);
|
|
|
|
EXPECT_EQ(buf[4], '5');
|
|
|
|
EXPECT_EQ(buf[5], '6');
|
|
|
|
EXPECT_EQ(buf[6], '7');
|
|
|
|
EXPECT_EQ(buf[7], '8');
|
|
|
|
EXPECT_EQ(buf[8], '9');
|
|
|
|
EXPECT_EQ(buf[9], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Printf, OverflowInt) {
|
|
|
|
char buf[] = "123456789";
|
2012-06-19 17:21:57 +08:00
|
|
|
internal_snprintf(buf, 4, "%d", -123456789); // NOLINT
|
2012-11-02 20:36:11 +08:00
|
|
|
EXPECT_STREQ("-12", buf);
|
2012-05-10 22:18:22 +08:00
|
|
|
EXPECT_EQ(buf[3], 0);
|
|
|
|
EXPECT_EQ(buf[4], '5');
|
|
|
|
EXPECT_EQ(buf[5], '6');
|
|
|
|
EXPECT_EQ(buf[6], '7');
|
|
|
|
EXPECT_EQ(buf[7], '8');
|
|
|
|
EXPECT_EQ(buf[8], '9');
|
|
|
|
EXPECT_EQ(buf[9], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Printf, OverflowUint) {
|
|
|
|
char buf[] = "123456789";
|
2012-11-02 22:28:17 +08:00
|
|
|
uptr val;
|
|
|
|
if (sizeof(val) == 4) {
|
|
|
|
val = (uptr)0x12345678;
|
|
|
|
} else {
|
|
|
|
val = (uptr)0x123456789ULL;
|
|
|
|
}
|
|
|
|
internal_snprintf(buf, 4, "a%zx", val); // NOLINT
|
2012-11-02 20:36:11 +08:00
|
|
|
EXPECT_STREQ("a12", buf);
|
2012-05-10 22:18:22 +08:00
|
|
|
EXPECT_EQ(buf[3], 0);
|
|
|
|
EXPECT_EQ(buf[4], '5');
|
|
|
|
EXPECT_EQ(buf[5], '6');
|
|
|
|
EXPECT_EQ(buf[6], '7');
|
|
|
|
EXPECT_EQ(buf[7], '8');
|
|
|
|
EXPECT_EQ(buf[8], '9');
|
|
|
|
EXPECT_EQ(buf[9], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Printf, OverflowPtr) {
|
|
|
|
char buf[] = "123456789";
|
2012-11-02 22:28:17 +08:00
|
|
|
void *p;
|
|
|
|
if (sizeof(p) == 4) {
|
|
|
|
p = (void*)0x1234567;
|
|
|
|
} else {
|
|
|
|
p = (void*)0x123456789ULL;
|
|
|
|
}
|
|
|
|
internal_snprintf(buf, 4, "%p", p); // NOLINT
|
2012-11-02 20:36:11 +08:00
|
|
|
EXPECT_STREQ("0x0", buf);
|
2012-05-10 22:18:22 +08:00
|
|
|
EXPECT_EQ(buf[3], 0);
|
|
|
|
EXPECT_EQ(buf[4], '5');
|
|
|
|
EXPECT_EQ(buf[5], '6');
|
|
|
|
EXPECT_EQ(buf[6], '7');
|
|
|
|
EXPECT_EQ(buf[7], '8');
|
|
|
|
EXPECT_EQ(buf[8], '9');
|
|
|
|
EXPECT_EQ(buf[9], 0);
|
|
|
|
}
|
|
|
|
|
2014-05-13 20:02:53 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// Oh well, MSVS headers don't define snprintf.
|
|
|
|
# define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2012-05-10 22:18:22 +08:00
|
|
|
template<typename T>
|
2013-06-27 23:30:44 +08:00
|
|
|
static void TestAgainstLibc(const char *fmt, T arg1, T arg2) {
|
2012-05-10 22:18:22 +08:00
|
|
|
char buf[1024];
|
2013-06-27 23:30:44 +08:00
|
|
|
uptr len = internal_snprintf(buf, sizeof(buf), fmt, arg1, arg2);
|
2012-05-10 22:18:22 +08:00
|
|
|
char buf2[1024];
|
2013-06-27 23:30:44 +08:00
|
|
|
snprintf(buf2, sizeof(buf2), fmt, arg1, arg2);
|
2012-05-10 22:18:22 +08:00
|
|
|
EXPECT_EQ(len, strlen(buf));
|
2012-11-02 20:36:11 +08:00
|
|
|
EXPECT_STREQ(buf2, buf);
|
2012-05-10 22:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Printf, MinMax) {
|
2013-06-27 23:30:44 +08:00
|
|
|
TestAgainstLibc<int>("%d-%d", INT_MIN, INT_MAX); // NOLINT
|
|
|
|
TestAgainstLibc<unsigned>("%u-%u", 0, UINT_MAX); // NOLINT
|
|
|
|
TestAgainstLibc<unsigned>("%x-%x", 0, UINT_MAX); // NOLINT
|
2014-05-13 20:02:53 +08:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
// %z* format doesn't seem to be supported by MSVS.
|
|
|
|
TestAgainstLibc<long>("%zd-%zd", LONG_MIN, LONG_MAX); // NOLINT
|
|
|
|
TestAgainstLibc<unsigned long>("%zu-%zu", 0, ULONG_MAX); // NOLINT
|
2013-06-27 23:30:44 +08:00
|
|
|
TestAgainstLibc<unsigned long>("%zx-%zx", 0, ULONG_MAX); // NOLINT
|
2014-05-13 20:02:53 +08:00
|
|
|
#endif
|
2013-06-27 23:30:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Printf, Padding) {
|
|
|
|
TestAgainstLibc<int>("%3d - %3d", 1, 0);
|
|
|
|
TestAgainstLibc<int>("%3d - %3d", -1, 123);
|
|
|
|
TestAgainstLibc<int>("%3d - %3d", -1, -123);
|
|
|
|
TestAgainstLibc<int>("%3d - %3d", 12, 1234);
|
|
|
|
TestAgainstLibc<int>("%3d - %3d", -12, -1234);
|
|
|
|
TestAgainstLibc<int>("%03d - %03d", 1, 0);
|
|
|
|
TestAgainstLibc<int>("%03d - %03d", -1, 123);
|
|
|
|
TestAgainstLibc<int>("%03d - %03d", -1, -123);
|
|
|
|
TestAgainstLibc<int>("%03d - %03d", 12, 1234);
|
|
|
|
TestAgainstLibc<int>("%03d - %03d", -12, -1234);
|
2012-05-10 22:18:22 +08:00
|
|
|
}
|
|
|
|
|
2014-01-21 18:59:44 +08:00
|
|
|
TEST(Printf, Precision) {
|
|
|
|
char buf[1024];
|
|
|
|
uptr len = internal_snprintf(buf, sizeof(buf), "%.*s", 3, "12345");
|
|
|
|
EXPECT_EQ(3U, len);
|
|
|
|
EXPECT_STREQ("123", buf);
|
|
|
|
len = internal_snprintf(buf, sizeof(buf), "%.*s", 6, "12345");
|
|
|
|
EXPECT_EQ(5U, len);
|
|
|
|
EXPECT_STREQ("12345", buf);
|
|
|
|
}
|
|
|
|
|
2012-11-02 20:36:11 +08:00
|
|
|
} // namespace __sanitizer
|