Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.esotericsoftware.kryo.KryoException: Encountered unregistered class ID: <negative_number> #340

Closed
max-k7v opened this issue Aug 13, 2015 · 2 comments

Comments

@max-k7v
Copy link

max-k7v commented Aug 13, 2015

During read objects with big class ID, I've got exceptions about negative value in ID
Kryo version 3.0.3

package com.vaultmr.app.patientchart.repository.serializer;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.UnsafeInput;
import com.esotericsoftware.kryo.io.UnsafeOutput;

@RunWith(Parameterized.class)
public class KryoTest {

    public static class Base implements Serializable {
        private final int field;

        public Base(int field) {
            this.field = field;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null || !(this.getClass().equals(obj.getClass()))) {
                return false;
            }
            return this.field == ((Base) obj).field;
        }
    }

    public static class T1 extends Base {
        public static final int serialVersionUID = 1000;

        public T1() {
            super(10);
        }
    };

    public static class T2 extends Base {
        public static final int serialVersionUID = 1000 * 1000;

        public T2() {
            super(20);
        }
    };

    public static class T3 extends Base {
        public static final int serialVersionUID = 1000 * 1000 * 1000;

        public T3() {
            super(30);
        }
    };

    public static class T4 extends Base {
        public static final int serialVersionUID = Integer.MAX_VALUE - 1;

        public T4() {
            super(40);
        }
    };

    public static class T5 extends Base {
        public static final int serialVersionUID = Integer.MAX_VALUE;

        public T5() {
            super(50);
        }
    };

    @Parameters(name= "{index}: type[{0}]")
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] {
                {T1.class},
                {T2.class},
                {T3.class},
                {T4.class},
                {T5.class}
        });
    }

    private final Class<?> type;

    public KryoTest(Class<?> type) {
        this.type = type;
    }

    @Test
    public void test() throws Exception {
        Kryo kryo = new Kryo();

        Field classUIDField = type.getField("serialVersionUID");
        int typeId = classUIDField.getInt(null);
        kryo.register(type, typeId);

        ByteArrayOutputStream buff = new ByteArrayOutputStream();
        Object origObj = type.newInstance();

        UnsafeOutput out = new UnsafeOutput(buff);
        kryo.writeClassAndObject(out, origObj);
        out.flush();

        Object copyObj = kryo.readClassAndObject(new UnsafeInput(buff.toByteArray()));

        Assert.assertEquals(origObj, copyObj);
    }
}
@romix
Copy link
Collaborator

romix commented Sep 3, 2015

Thanks for your bug report. I could reproduce your problem. This seems to be a bug in variable length int encoding of UnsafeOutput/UnsafeMemoryOutput. I'm looking into it.

romix added a commit that referenced this issue Sep 3, 2015
…t was incorrect in certain cases.

Added more thorough unit tests to avoid such problems in the future.
@romix romix closed this as completed Sep 3, 2015
@max-k7v
Copy link
Author

max-k7v commented Sep 3, 2015

Thank you for live product 👍
I'm looking forward for the next release.

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

No branches or pull requests

2 participants