Skip to content

Encode signed integers correctly - #124

Merged
glbrntt merged 2 commits into
apple:mainfrom
aryan-25:encode-signed-integers-correctly
May 27, 2026
Merged

Encode signed integers correctly#124
glbrntt merged 2 commits into
apple:mainfrom
aryan-25:encode-signed-integers-correctly

Conversation

@aryan-25

Copy link
Copy Markdown
Contributor

Motivation:

Currently, positive signed integers whose minimal big-endian representation exactly fills N bytes (neededBits % 8 == 0) have their top bit misread as the DER sign bit. Int64(128) is therefore encoded as [0x80] and decoded back as -128. We should fix this.

Modifications:

  • Updated the FixedWidthInteger.neededBytes implementation to return bytes + 1 for positive signed integers in this case, causing IntegerBytesCollection to emit the required 0x00 sign byte.
  • Added associated test cases.

Result:

Positive signed integers whose minimal big-endian representation exactly fills N bytes (neededBits % 8 == 0) are now encoded correctly.

@aryan-25 aryan-25 added the 🔨 semver/patch No public API change. label May 21, 2026
@glbrntt
glbrntt merged commit a9a5efd into apple:main May 27, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants