Skip to content

Negative BigInteger values not encoded/decoded correctlyΒ #431

Closed

Description

@bgruber

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:

var negOne = BigInteger.ONE.negate();
var cborMapper = new ObjectMapper(new CBORFactory());
var bytes = cborMapper.writeValueAsBytes(negOne);
System.out.println(org.apache.commons.codec.binary.Hex.encode(bytes));

This prints out c34101, but per this CBOR playground, that's CBOR for a bigint of -2, not -1.

Similarly on the read side:

    var cborMapper = new ObjectMapper(new CBORFactory());
    var bytes =
        new byte[] {
          (byte) 0xC3,
          (byte) 0x50,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF,
          (byte) 0xFF
        };
    var read = cborMapper.readValue(bytes, BigInteger.class);
    System.out.println(read);

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.

Thanks for all your work on jackson!

Activity

  1. cowtowncoder commented on Dec 30, 2023

    @cowtowncoder
    Member

    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.

  2. added
    pr-neededFeature request for which PR likely needed (no active development but idea is workable)
    on Jun 7, 2024
  3. cowtowncoder commented on Jun 7, 2024

    @cowtowncoder
    Member

    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).

  4. added
    need-test-caseTo work on issue, a reproduction (ideally unit test) needed
    and removed on Aug 23, 2024
  5. cowtowncoder commented on Aug 23, 2024

    @cowtowncoder
    Member

    @bgruber Is this something you could help with? I'd really like to address this for 2.18.0, but would need help.

  6. added and removed
    need-test-caseTo work on issue, a reproduction (ideally unit test) needed
    on Apr 30, 2025
  7. changed the title [-]CBOR: negative BigInteger values not handled correctly[/-] [+]Negative `BigInteger` values not encoded/decoded correctly[/+] on Apr 30, 2025
  8. added this to the 2.20.0 milestone on Apr 30, 2025
  9. cowtowncoder commented on Apr 30, 2025

    @cowtowncoder
    Member

    Fixed via #578 by @iifawzi -- thank you!

  10. cowtowncoder commented on Aug 30, 2025

    @cowtowncoder
    Member

    NOTE! In Jackson 2.x (2.20.0+), it is necessary to enable 2 features:

    • CBORGenerator.Feature.ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING
    • CBORParser.Feature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING

    In Jackson 3.0 these will be enabled by default

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    cborpr-neededFeature request for which PR likely needed (no active development but idea is workable)

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions