Skip to content

(release/25.1) os: fix undefined behavior in FormatInt64() for INT64_MIN - #3616

Open
metux wants to merge 1 commit into
release/25.1from
tmp-pr/release/25.1-os-fix-undefined-behavior-in-formatint64-for-int64-min
Open

(release/25.1) os: fix undefined behavior in FormatInt64() for INT64_MIN#3616
metux wants to merge 1 commit into
release/25.1from
tmp-pr/release/25.1-os-fix-undefined-behavior-in-formatint64-for-int64-min

Conversation

@metux

@metux metux commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Backport of #3614 (master)

'num *= -1' is undefined behavior when num is INT64_MIN, since the
positive value (9223372036854775808) exceeds INT64_MAX.

Fix by introducing a uint64_t intermediate and casting to uint64_t
before negation, which is well-defined unsigned modular arithmetic
and produces the correct absolute value for all int64_t values
including INT64_MIN.

Fixes: #3610
Reported-by: Alex <alexzk1@users.noreply.github.com>
Signed-off-by: Defiant <defiant@x11libre.dev>
@metux metux self-assigned this Aug 24, 2026
@metux
metux requested a review from a team August 24, 2026 10:24
@metux metux added the bot-review-passed Automated bot review found no blocking issues label Aug 24, 2026
@metux

metux commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Verdict: APPROVE

🤖 Automated review — generated by Starfleet ship Defiant on behalf of @metux. Not a human review.

This is a minimal, well-targeted fix for undefined behavior in FormatInt64().

What changed: num * -1-(uint64_t)num (master/25.2) or added uint64_t unum intermediate (25.1/25.0).

Why it is correct:

  • Signed integer overflow (INT64_MIN * -1) is undefined behavior in C
  • Casting to uint64_t first makes the negation well-defined (unsigned modular arithmetic)
  • -(uint64_t)INT64_MIN = 9223372036854775808 = correct absolute value
  • Works for all int64_t values, not just INT64_MIN

Scope: One line change (master/25.2) or 4-line adaptation (25.1/25.0 where uint64_t unum did not exist yet). No behavioral change for any value except INT64_MIN.

Fixes: #3610

Approved-by: Defiant

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

Labels

bot-review-passed Automated bot review found no blocking issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant