Skip to content

Commit

Permalink
fix stackoverflow of protostuff and other errors
Browse files Browse the repository at this point in the history
  • Loading branch information
moriadry committed Aug 17, 2019
1 parent 897709b commit 808677c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.dubbo.common.serialize.protostuff;

import io.protostuff.GraphIOUtil;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
import java.io.DataInputStream;
Expand Down Expand Up @@ -61,12 +61,12 @@ public Object readObject() throws IOException, ClassNotFoundException {
if (WrapperUtils.needWrapper(clazz)) {
Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
Wrapper wrapper = schema.newMessage();
GraphIOUtil.mergeFrom(bytes, wrapper, schema);
ProtobufIOUtil.mergeFrom(bytes, wrapper, schema);
result = wrapper.getData();
} else {
Schema schema = RuntimeSchema.getSchema(clazz);
result = schema.newMessage();
GraphIOUtil.mergeFrom(bytes, result, schema);
ProtobufIOUtil.mergeFrom(bytes, result, schema);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.dubbo.common.serialize.protostuff;

import io.protostuff.GraphIOUtil;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.LinkedBuffer;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
Expand Down Expand Up @@ -50,11 +50,11 @@ public void writeObject(Object obj) throws IOException {
if (obj == null || WrapperUtils.needWrapper(obj)) {
Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
Wrapper wrapper = new Wrapper(obj);
bytes = GraphIOUtil.toByteArray(wrapper, schema, buffer);
bytes = ProtobufIOUtil.toByteArray(wrapper, schema, buffer);
classNameBytes = Wrapper.class.getName().getBytes();
} else {
Schema schema = RuntimeSchema.getSchema(obj.getClass());
bytes = GraphIOUtil.toByteArray(obj, schema, buffer);
bytes = ProtobufIOUtil.toByteArray(obj, schema, buffer);
classNameBytes = obj.getClass().getName().getBytes();
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.dubbo.common.serialize.model.SerializablePerson;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -90,9 +94,58 @@ public void testSerializeDate() throws IOException, ClassNotFoundException {
assertThat(serializedTime, is(originTime));
}

@Test
public void testListObject() throws IOException, ClassNotFoundException {
List<SerializablePerson> list = new ArrayList<SerializablePerson>();
list.add(new SerializablePerson());
list.add(new SerializablePerson());
list.add(new SerializablePerson());
SerializablePersonList personList = new SerializablePersonList(list);
this.protostuffObjectOutput.writeObject(personList);
this.flushToInput();

SerializablePersonList serializedTime = protostuffObjectInput.readObject(SerializablePersonList.class);
assertThat(serializedTime, is(personList));
}

private void flushToInput() throws IOException {
this.protostuffObjectOutput.flushBuffer();
this.byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
this.protostuffObjectInput = new ProtostuffObjectInput(byteArrayInputStream);
}

private class SerializablePersonList implements Serializable {
private static final long serialVersionUID = 1L;

public List<SerializablePerson> personList;

public SerializablePersonList() {}

public SerializablePersonList(List<SerializablePerson> list) {
this.personList = list;
}

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

SerializablePersonList list = (SerializablePersonList) obj;
if (list.personList == null && this.personList == null)
return true;
if (list.personList == null || this.personList == null)
return false;
if (list.personList.size() != this.personList.size())
return false;
for (int i =0; i < this.personList.size(); i++) {
if (!this.personList.get(i).equals(list.personList.get(i)))
return false;
}
return true;
}
}
}

0 comments on commit 808677c

Please sign in to comment.