Skip to content

fix(asn1parser): format the signed minimum exactly - #614

Merged
mouse07410 merged 1 commit into
mouse07410:vlm_masterfrom
mr-j0nes:pr-ASN1-0087-itoa-minimum
Nov 18, 2026
Merged

mouse07410 merged 1 commit into
mouse07410:vlm_masterfrom
mr-j0nes:pr-ASN1-0087-itoa-minimum

Conversation

@mr-j0nes

Copy link
Copy Markdown

asn1p_itoa_s() handled values that do not fit long by negating them to obtain the magnitude, which overflows for ASN_INTEGER_MIN. It worked around that with two hard-coded strings, and both were wrong: the 128-bit literal ended in ...729 instead of ...728 and the 64-bit one in ...809 instead of ...808. The branch also returned 41 and 21, counting the terminating NUL, although the header promises the length of the numeric value (40 and 20).

Compute the magnitude in asn1c_uinteger_t instead: converting to unsigned and subtracting from zero is defined for every value and yields |v| even for the minimum. Split that magnitude with unsigned arithmetic and keep the existing nine-digit padding and the long fast path. The width-specific literals, the negation and the switch are gone, so the result no longer depends on long or on the configured width.

Extend the libasn1parser unit test with exact strings, return lengths and the buffer-size contract, including both limits and their neighbors. The boundary script now also checks that -E -F prints the exact minimum, since the old value could not be re-parsed.

Fixes #613

asn1p_itoa_s() handled values that do not fit long by negating them to
obtain the magnitude, which overflows for ASN_INTEGER_MIN. It worked
around that with two hard-coded strings, and both were wrong: the
128-bit literal ended in ...729 instead of ...728 and the 64-bit one in
...809 instead of ...808. The branch also returned 41 and 21, counting
the terminating NUL, although the header promises the length of the
numeric value (40 and 20).

Compute the magnitude in asn1c_uinteger_t instead: converting to
unsigned and subtracting from zero is defined for every value and yields
|v| even for the minimum. Split that magnitude with unsigned arithmetic
and keep the existing nine-digit padding and the long fast path. The
width-specific literals, the negation and the switch are gone, so the
result no longer depends on long or on the configured width.

Extend the libasn1parser unit test with exact strings, return lengths and
the buffer-size contract, including both limits and their neighbours.
The boundary script now also checks that
-E -F prints the exact minimum, since the old value could not be
re-parsed.
@mouse07410
mouse07410 merged commit b47f2b8 into mouse07410:vlm_master Nov 18, 2026
4 checks passed
@mr-j0nes
mr-j0nes deleted the pr-ASN1-0087-itoa-minimum branch November 19, 2026 10:32
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.

The printed minimum INTEGER value is off by one

2 participants