Skip to content

Lexer: Non-decimal integer literals (hex, bin, oct) are saturated at 64-bit limits #4453

@nsvke

Description

@nsvke

Summary

Currently, the Rust frontend's lexer handles non-decimal integer literals (hexadecimal, binary, and octal) by converting them into decimal strings using std::strtol. Since std::strtol is limited by the size of a 64-bit long (on 64-bit systems), any literal exceeding this range (such as large u128 values) is silently saturated to LONG_MAX (2^63−1) or ULONG_MAX. This leads to incorrect constant values being passed to the backend.

Reproducer

I tried this code:

#![feature(no_core)]
#![no_core]
pub fn main() {
        let hex_val: u128 = 0x10000000000000000_u128;
        let bin_val: u128 =
            0b10000000000000000000000000000000000000000000000000000000000000000_u128;
        let oct_val: u128 = 0o2000000000000000000000_u128;
}

Does the code make use of any (1.49) nightly feature ?

  • Nightly

Godbolt link

Reproduced on local build of gccrs

Actual behavior

For the reproducible code above

[DEBUG-OLD] RADIXSTRINGHEX: 0x10000000000000000
[DEBUG-OLD] DECIMALSTRINGHEX: 9223372036854775807
[DEBUG-OLD] RADIXSTRINGBIN: 010000000000000000000000000000000000000000000000000000000000000000
[DEBUG-OLD] DECIMALSTRINGBIN: 9223372036854775807
[DEBUG-OLD] RADIXSTRINGOCT: 02000000000000000000000
[DEBUG-OLD] DECIMALSTRINGOCT: 9223372036854775807

Expected behavior

For the reproducible code above

[DEBUG-NEW] RADIXSTRINGHEX: 010000000000000000
[DEBUG-NEW] DECIMALSTRINGHEX: 18446744073709551616
[DEBUG-NEW] RADIXSTRINGBIN: 010000000000000000000000000000000000000000000000000000000000000000
[DEBUG-NEW] DECIMALSTRINGBIN: 18446744073709551616
[DEBUG-NEW] RADIXSTRINGOCT: 02000000000000000000000
[DEBUG-NEW] DECIMALSTRINGOCT: 18446744073709551616

GCC Version

commit hash: 6b891ee

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions