From ad0397e4200ee1a765a53a9fb2079560a6bd46c6 Mon Sep 17 00:00:00 2001 From: Orivej Desh Date: Mon, 7 Oct 2019 00:29:10 +0000 Subject: [PATCH] Fix undefined in core-test and printf-test 1. The change in include/fmt/core.h fixes "shift exponent -4 is negative" in PrintfTest.InvalidArgIndex `do_get` is called with index -1 when `basic_printf_context.arg` is called with id 4294967295 when basic_printf_context::get_arg subtracts 1 from arg_index 0 in the format string "%0$d". 2. The change in test/core-test.cc fixes "reference binding to null pointer" in BufferTest.Ctor buffer.operator[] attempts to return a reference to `buffer.ptr_[0]` when `ptr_` in `mock_buffer buffer` is null. 2. The change in test/printf-test.cc fixes "signed integer overflow" in PrintfTest.Length This occurs in `TestLength("ll")`, since its minimum value minus one does not fit in long long. --- include/fmt/core.h | 1 + test/core-test.cc | 2 +- test/printf-test.cc | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 8cdc9cdcb81df..c6f771902d0ec 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1208,6 +1208,7 @@ template class basic_format_args { format_arg do_get(int index) const { format_arg arg; + if (index < 0) return arg; if (!is_packed()) { auto num_args = max_size(); if (index < num_args) arg = args_[index]; diff --git a/test/core-test.cc b/test/core-test.cc index 84d2b9eb0172e..76612b9914db8 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -103,7 +103,7 @@ template struct mock_buffer : buffer { TEST(BufferTest, Ctor) { { mock_buffer buffer; - EXPECT_EQ(nullptr, &buffer[0]); + EXPECT_EQ(nullptr, buffer.data()); EXPECT_EQ(static_cast(0), buffer.size()); EXPECT_EQ(static_cast(0), buffer.capacity()); } diff --git a/test/printf-test.cc b/test/printf-test.cc index cd9950d170b0b..d8fbc9649aec8 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -337,7 +337,9 @@ template void TestLength(const char* length_spec) { TestLength(length_spec, -42); TestLength(length_spec, min); TestLength(length_spec, max); - TestLength(length_spec, static_cast(min) - 1); + long long long_long_min = std::numeric_limits::min(); + if (static_cast(min) > long_long_min) + TestLength(length_spec, static_cast(min) - 1); unsigned long long long_long_max = max_value(); if (static_cast(max) < long_long_max) TestLength(length_spec, static_cast(max) + 1);