From 06044ca70d06afb18d30b7634ee6bfbad53a3378 Mon Sep 17 00:00:00 2001 From: Kieran Bingham Date: Fri, 31 Oct 2025 20:38:53 +0000 Subject: [PATCH] test: utils: Validate hex sign extension MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Converting strings to hex stores data in a uint64_t. This can incorrectly sign extend if the type being converted is signed. Provide tests to be sure that the signed conversion is correct. This is known to fail, so report as expected failure. Reviewed-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham Signed-off-by: Laurent Pinchart --- test/meson.build | 2 +- test/utils.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/test/meson.build b/test/meson.build index 52f04364..96c4477f 100644 --- a/test/meson.build +++ b/test/meson.build @@ -73,7 +73,7 @@ internal_tests = [ {'name': 'timer-fail', 'sources': ['timer-fail.cpp'], 'should_fail': true}, {'name': 'timer-thread', 'sources': ['timer-thread.cpp']}, {'name': 'unique-fd', 'sources': ['unique-fd.cpp']}, - {'name': 'utils', 'sources': ['utils.cpp']}, + {'name': 'utils', 'sources': ['utils.cpp'], 'should_fail': true}, {'name': 'vector', 'sources': ['vector.cpp']}, {'name': 'yaml-parser', 'sources': ['yaml-parser.cpp']}, ] diff --git a/test/utils.cpp b/test/utils.cpp index ca2e4f3b..b5ce94e5 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -218,6 +218,23 @@ protected: os << utils::hex(static_cast(0x42), 1) << " "; ref += "0x42 "; + os << utils::hex(static_cast(-1)) << " "; + ref += "0xff "; + os << utils::hex(static_cast(-1)) << " "; + ref += "0xff "; + os << utils::hex(static_cast(-1)) << " "; + ref += "0xffff "; + os << utils::hex(static_cast(-1)) << " "; + ref += "0xffff "; + os << utils::hex(static_cast(-1)) << " "; + ref += "0xffffffff "; + os << utils::hex(static_cast(-1)) << " "; + ref += "0xffffffff "; + os << utils::hex(static_cast(-1)) << " "; + ref += "0xffffffffffffffff "; + os << utils::hex(static_cast(-1)) << " "; + ref += "0xffffffffffffffff "; + std::string s = os.str(); if (s != ref) { cerr << "utils::hex() test failed, expected '" << ref