You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Negative BigInteger values not encoded/decoded correctlyΒ #431
Hi!
I was doing some work with the CBOR part of the library, and I found that it didn't decode or encode negative BigIntegers as I understand the spec. For example:
Prints 1, instead of -340282366920938463463374607431768211456 (-2^128). CBOR playground
I think the issue is that the library is constructing the BigIntegers from the byte string, and then negating it, instead of taking the complement of the byte string (or "-1 - n" as stated by the RFC) and constructing the BigInteger from that.
That sounds plausible to me. Thank you for reporting this @bgruber.
But aside from confirming the case and proposing a fix, the tricky part is compatibility.
It is unfortunate that generation produces wrong value (essentially corrupt encoding); if this was only for decoding there'd be bit less problem.
I suspect it will be necessary to add CBORGenerator.Feature and CBORParser.Features to control handling, and possibly even default these to existing incorrect behavior, given that format backend is used in production and so just changes can be pretty nasty.
I think I could use help here if anyone has time: I can help in configuration part, but would need a simple(st) reproduction for a small negative number (easiest to verify, handling not dependent on magnitude).
changed the title [-]CBOR: negative BigInteger values not handled correctly[/-][+]Negative `BigInteger` values not encoded/decoded correctly[/+]on Apr 30, 2025
Activity
cowtowncoder commentedon Dec 30, 2023
That sounds plausible to me. Thank you for reporting this @bgruber.
But aside from confirming the case and proposing a fix, the tricky part is compatibility.
It is unfortunate that generation produces wrong value (essentially corrupt encoding); if this was only for decoding there'd be bit less problem.
I suspect it will be necessary to add
CBORGenerator.FeatureandCBORParser.Features to control handling, and possibly even default these to existing incorrect behavior, given that format backend is used in production and so just changes can be pretty nasty.cowtowncoder commentedon Jun 7, 2024
I think I could use help here if anyone has time: I can help in configuration part, but would need a simple(st) reproduction for a small negative number (easiest to verify, handling not dependent on magnitude).
cowtowncoder commentedon Aug 23, 2024
@bgruber Is this something you could help with? I'd really like to address this for 2.18.0, but would need help.
[-]CBOR: negative BigInteger values not handled correctly[/-][+]Negative `BigInteger` values not encoded/decoded correctly[/+]cowtowncoder commentedon Apr 30, 2025
Fixed via #578 by @iifawzi -- thank you!
cowtowncoder commentedon Aug 30, 2025
NOTE! In Jackson 2.x (2.20.0+), it is necessary to enable 2 features:
CBORGenerator.Feature.ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODINGCBORParser.Feature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODINGIn Jackson 3.0 these will be enabled by default