Skip to content

CBOR loses Map entries with specific long Map key values (32-bit boundary) #269

@Quantum64

Description

@Quantum64

The issue only appears to occur with large keys of opposite sign. See the below reproducer.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;

import java.util.HashMap;
import java.util.Map;

public class CBORLongMapReproducer {
    private static final ObjectMapper json = new ObjectMapper();
    private static final ObjectMapper cbor = new ObjectMapper(new CBORFactory());

    public static void main(String[] args) throws Exception {
        Map<Long, String> map = new HashMap<>();
        map.put(4294967296L, "hello");
        map.put(-4294967296L, "world");
        TestData data = new TestData();
        data.setMap(map);

        Map<Long, String> resultCbor = cbor.readValue(cbor.writeValueAsBytes(data), TestData.class).getMap();
        Map<Long, String> resultJson = json.readValue(json.writeValueAsBytes(data), TestData.class).getMap();

        System.out.println("JSON size: " + resultJson.size());
        System.out.println("CBOR size: " + resultCbor.size());
    }

    public static class TestData {
        private Map<Long, String> map;

        public void setMap(Map<Long, String> map) {
            this.map = map;
        }

        public Map<Long, String> getMap() {
            return map;
        }
    }
}

This program produces the following output:

JSON size: 2
CBOR size: 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions