From 8ce59d204de0644e5804547dce83fab9e95ec247 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 4 Sep 2023 17:17:39 +0200 Subject: [PATCH 1/2] Add a test case demonstrating the issue when trying to print a zero value for uint64_t. --- test/src/Print/test_print.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test/src/Print/test_print.cpp b/test/src/Print/test_print.cpp index 92e9c083..ac731a89 100644 --- a/test/src/Print/test_print.cpp +++ b/test/src/Print/test_print.cpp @@ -110,12 +110,24 @@ TEST_CASE ("Print::print(unsigned long long, int = DEC|HEX|OCT|BIN)", "[Print-pr { PrintMock mock; - unsigned long long const val = 17; + GIVEN("a value of zero ...") + { + unsigned long long const val = 0; - WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); } - WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); } - WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); } - WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); } + WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "0"); } + WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "0"); } + WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "0"); } + WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "0"); } + } + GIVEN("a non-zero value ...") + { + unsigned long long const val = 17; + + WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); } + WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); } + WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); } + WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); } + } } TEST_CASE ("Print::print(double, int = 2)", "[Print-print-10]") From 754aa9b1a65f15c190d49b2478b95ccbe55b7f7a Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 3 Sep 2023 13:18:47 -0700 Subject: [PATCH 2/2] Fix Print::print(0ULL) Fixes #1691 To be removed once upstream bug is fixed and core-api imported: https://github.com/arduino/ArduinoCore-API/issues/178 --- api/Print.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/api/Print.cpp b/api/Print.cpp index 8c3e1930..f1e82469 100644 --- a/api/Print.cpp +++ b/api/Print.cpp @@ -287,6 +287,12 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base) uint8_t i = 0; uint8_t innerLoops = 0; + // Special case workaround https://github.com/arduino/ArduinoCore-API/issues/178 + if (n64 == 0) { + write('0'); + return 1; + } + // prevent crash if called with base == 1 if (base < 2) base = 10;