You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/07/13 03:11:57 UTC
[GitHub] lovepoem closed pull request #2032: [Dubbo-2031] Fix hessian2
serialization infinit recursion(StackOverflowError) when object's
writeReplace method returns the object itself
lovepoem closed pull request #2032: [Dubbo-2031] Fix hessian2 serialization infinit recursion(StackOverflowError) when object's writeReplace method returns the object itself
URL: https://github.com/apache/incubator-dubbo/pull/2032
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/JavaSerializer.java b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/JavaSerializer.java
index a6c4a132b5..150a598e7e 100644
--- a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/JavaSerializer.java
+++ b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/JavaSerializer.java
@@ -218,13 +218,17 @@ public void writeObject(Object obj, AbstractHessianOutput out)
else
repl = _writeReplace.invoke(obj);
- out.removeRef(obj);
+ //Some class would return itself for wrapReplace, which would cause infinite recursion
+ //In this case, we could just write the object just like normal cases
+ if (repl != obj) {
+ out.removeRef(obj);
- out.writeObject(repl);
+ out.writeObject(repl);
- out.replaceRef(repl, obj);
+ out.replaceRef(repl, obj);
- return;
+ return;
+ }
}
} catch (RuntimeException e) {
throw e;
diff --git a/hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/writereplace/Hessian2WriteReplaceTest.java b/hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/writereplace/Hessian2WriteReplaceTest.java
new file mode 100644
index 0000000000..f2e827da51
--- /dev/null
+++ b/hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/writereplace/Hessian2WriteReplaceTest.java
@@ -0,0 +1,115 @@
+package com.alibaba.com.caucho.hessian.io.writereplace;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.Serializable;
+import org.junit.Test;
+
+import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
+
+public class Hessian2WriteReplaceTest extends SerializeTestBase {
+
+ @Test
+ public void testWriteReplaceReturningItself() throws Exception {
+ String someName = "some name";
+ WriteReplaceReturningItself object = new WriteReplaceReturningItself(someName);
+
+ WriteReplaceReturningItself result = baseHession2Serialize(object);
+
+ assertEquals(someName, result.getName());
+ }
+
+ @Test
+ public void testNormalWriteReplace() throws Exception {
+ String someFirstName = "first";
+ String someLastName = "last";
+ String someAddress = "some address";
+
+ NormalWriteReplace object = new NormalWriteReplace(someFirstName, someLastName, someAddress);
+
+ NormalWriteReplace result = baseHession2Serialize(object);
+
+ assertEquals(someFirstName, result.getFirstName());
+ assertEquals(someLastName, result.getLastName());
+ assertEquals(someAddress, result.getAddress());
+ }
+
+ static class WriteReplaceReturningItself implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private String name;
+
+ WriteReplaceReturningItself(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Some object may return itself for wrapReplace, e.g.
+ * https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java#L173
+ */
+ Object writeReplace() {
+ //do some extra things
+
+ return this;
+ }
+ }
+
+ static class NormalWriteReplace implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ private String firstName;
+ private String lastName;
+ private String address;
+
+ public NormalWriteReplace(String firstName, String lastName, String address) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ this.address = address;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public String getAddress() {
+ return address;
+ }
+
+ //return a proxy to save space for serialization
+ Object writeReplace() {
+ return new NormalWriteReplaceProxy(this);
+ }
+ }
+
+ static class NormalWriteReplaceProxy implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ private String data;
+
+ //empty constructor for deserialization
+ public NormalWriteReplaceProxy() {
+ }
+
+ public NormalWriteReplaceProxy(NormalWriteReplace normalWriteReplace) {
+ this.data = normalWriteReplace.getFirstName() + "," + normalWriteReplace.getLastName() + "," + normalWriteReplace
+ .getAddress();
+ }
+
+ //construct the actual object
+ Object readResolve() {
+ String[] parts = data.split(",");
+
+ return new NormalWriteReplace(parts[0], parts[1], parts[2]);
+ }
+ }
+}
+
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org