Skip to content

nvme-print: fix unintended sign extension in json_eom_printable_eye() - #3822

Merged
igaw merged 1 commit into
linux-nvme:masterfrom
sahmed-ibm:fix-nvme-print-json-sign-extension
Aug 13, 2026
Merged

nvme-print: fix unintended sign extension in json_eom_printable_eye()#3822
igaw merged 1 commit into
linux-nvme:masterfrom
sahmed-ibm:fix-nvme-print-json-sign-extension

Conversation

@sahmed-ibm

Copy link
Copy Markdown
Contributor

The json_eom_printable_eye() function uses nrows and ncols, both of type uint16_t, to compute the allocation size for the printable eye string buffer.

Because uint16_t operands are promoted to int (32-bit signed) before multiplication, the expression nrows * ncols + nrows + 1 is evaluated as a signed 32-bit value. When passed to malloc(), the compiler widens it to size_t (64-bit unsigned) via sign extension. If the product exceeds 0x7FFFFFFF the sign bit is set, the result sign-extends to a very large 64-bit value, and malloc() will either fail or trigger an out-of-memory condition.

Cast nrows to size_t before the multiplication so the entire expression is evaluated in 64-bit unsigned arithmetic, preventing the sign extension.

The json_eom_printable_eye() function uses @nrows and @NCols, both of
type uint16_t, to compute the allocation size for the printable eye
string buffer.

Because uint16_t operands are promoted to int (32-bit signed) before
multiplication, the expression @nrows * @NCols + @nrows + 1 is
evaluated as a signed 32-bit value. When passed to malloc(), the
compiler widens it to size_t (64-bit unsigned) via sign extension. If
the product exceeds 0x7FFFFFFF the sign bit is set, the result
sign-extends to a very large 64-bit value, and malloc() will either
fail or trigger an out-of-memory condition.

Cast @nrows to size_t before the multiplication so the entire
expression is evaluated in 64-bit unsigned arithmetic, preventing
the sign extension.

Signed-off-by: Sarah Ahmed <sarah.ahmed@ibm.com>
@igaw
igaw merged commit fd81f65 into linux-nvme:master Aug 13, 2026
33 checks passed
@igaw

igaw commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants