[v3,0/2] libcamera: base: utils: Prevent hex signed extension
mbox series

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

Message

Kieran Bingham Oct. 31, 2025, 8:38 p.m. UTC
When converting signed values to hex strings using the utils::hex
helpers, signed negative values get sign extended to the larger 64 bit
type.

This causes values such as 0x80 to be printed as 0xffffffffffffff80
instead.

Fix this by first adding a test to catch and identify the issue, and
secondly by reconfiguring the hex adaptor to use a single templated type
which always casts to an unsigned version of the incoming type.

Kieran Bingham (2):
  test: utils: Validate hex sign extension
  libcamera: base: utils: Simplify hex adaptor

 include/libcamera/base/utils.h | 68 +++++++---------------------------
 src/libcamera/base/utils.cpp   |  3 +-
 test/utils.cpp                 | 17 +++++++++
 3 files changed, 33 insertions(+), 55 deletions(-)

Comments

Kieran Bingham Oct. 31, 2025, 8:40 p.m. UTC | #1
Except actually this is v4.

Quoting Kieran Bingham (2025-10-31 20:38:52)
> When converting signed values to hex strings using the utils::hex
> helpers, signed negative values get sign extended to the larger 64 bit
> type.
> 
> This causes values such as 0x80 to be printed as 0xffffffffffffff80
> instead.
> 
> Fix this by first adding a test to catch and identify the issue, and
> secondly by reconfiguring the hex adaptor to use a single templated type
> which always casts to an unsigned version of the incoming type.
> 
> Kieran Bingham (2):
>   test: utils: Validate hex sign extension
>   libcamera: base: utils: Simplify hex adaptor
> 
>  include/libcamera/base/utils.h | 68 +++++++---------------------------
>  src/libcamera/base/utils.cpp   |  3 +-
>  test/utils.cpp                 | 17 +++++++++
>  3 files changed, 33 insertions(+), 55 deletions(-)
> 
> -- 
> 2.51.0
>
Laurent Pinchart Nov. 2, 2025, 12:39 p.m. UTC | #2
On Fri, Oct 31, 2025 at 08:38:52PM +0000, Kieran Bingham wrote:
> When converting signed values to hex strings using the utils::hex
> helpers, signed negative values get sign extended to the larger 64 bit
> type.
> 
> This causes values such as 0x80 to be printed as 0xffffffffffffff80
> instead.
> 
> Fix this by first adding a test to catch and identify the issue, and
> secondly by reconfiguring the hex adaptor to use a single templated type
> which always casts to an unsigned version of the incoming type.
> 
> Kieran Bingham (2):
>   test: utils: Validate hex sign extension
>   libcamera: base: utils: Simplify hex adaptor

Pushed, thank you.

>  include/libcamera/base/utils.h | 68 +++++++---------------------------
>  src/libcamera/base/utils.cpp   |  3 +-
>  test/utils.cpp                 | 17 +++++++++
>  3 files changed, 33 insertions(+), 55 deletions(-)