[v3,1/2] test: utils: Validate hex sign extension
diff mbox series

Message ID 20251031203854.19021-2-kieran.bingham@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: base: utils: Prevent hex signed extension
Related show

Commit Message

Kieran Bingham Oct. 31, 2025, 8:38 p.m. UTC
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 <barnabas.pocze@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 test/meson.build |  2 +-
 test/utils.cpp   | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

Patch
diff mbox series

diff --git a/test/meson.build b/test/meson.build
index 52f04364e4fc..96c4477f04b2 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 ca2e4f3b3e85..b5ce94e5e912 100644
--- a/test/utils.cpp
+++ b/test/utils.cpp
@@ -218,6 +218,23 @@  protected:
 		os << utils::hex(static_cast<uint64_t>(0x42), 1) << " ";
 		ref += "0x42 ";
 
+		os << utils::hex(static_cast<int8_t>(-1)) << " ";
+		ref += "0xff ";
+		os << utils::hex(static_cast<uint8_t>(-1)) << " ";
+		ref += "0xff ";
+		os << utils::hex(static_cast<int16_t>(-1)) << " ";
+		ref += "0xffff ";
+		os << utils::hex(static_cast<uint16_t>(-1)) << " ";
+		ref += "0xffff ";
+		os << utils::hex(static_cast<int32_t>(-1)) << " ";
+		ref += "0xffffffff ";
+		os << utils::hex(static_cast<uint32_t>(-1)) << " ";
+		ref += "0xffffffff ";
+		os << utils::hex(static_cast<int64_t>(-1)) << " ";
+		ref += "0xffffffffffffffff ";
+		os << utils::hex(static_cast<uint64_t>(-1)) << " ";
+		ref += "0xffffffffffffffff ";
+
 		std::string s = os.str();
 		if (s != ref) {
 			cerr << "utils::hex() test failed, expected '" << ref