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

ProtostuffIOUtil.toByteArray stack overflow when there is reference loop in message #258

Open
xnslong opened this issue Dec 26, 2018 · 1 comment

Comments

@xnslong
Copy link

xnslong commented Dec 26, 2018

When there is an reference loop in the object map, the ProtostuffIOUtil.toByteArray method will result in StackOverflowError. Following are the steps to reproduce this phenomenon.

// 1. define 2 classes, A and B, referencing each other
static class A {
    B b;
    String name;
}
static class B {
    A a;
    String name;
}

public static void main(String[] args) {
    // 2. create an object "a" and "b", with them referencing each other
    A a = new A();
    B b = new B();
    a.name = "a";
    b.name = "b";
    a.b = b;
    b.a = a;

    // 3. try to get byte array for object "a".
    Schema<A> schema = RuntimeSchema.getSchema(A.class);
    LinkedBuffer buffer = LinkedBuffer.allocate(8192);
    ProtostuffIOUtil.toByteArray(a, schema, buffer); // <-- this method call will result in StackOverflowError
}

Following is the top part of the error stack

Exception in thread "main" java.lang.StackOverflowError
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:358)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	at io.protostuff.runtime.RuntimeSchema.writeTo(RuntimeSchema.java:475)
	at io.protostuff.ProtostuffOutput.writeObject(ProtostuffOutput.java:363)
	at io.protostuff.runtime.RuntimeUnsafeFieldFactory$13$1.writeTo(RuntimeUnsafeFieldFactory.java:1048)
	...
@dyu
Copy link
Member

dyu commented Dec 26, 2018

Use GraphIOUtil instead of ProtostuffIOUtil for data with circular references

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

No branches or pull requests

2 participants