Skip to content

Commit 32a7eb2

Browse files
author
Jan Bobolz
committed
Fixed handling of null representables in ReprUtil
1 parent 2f7d702 commit 32a7eb2

5 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/main/java/org/cryptimeleon/math/serialization/annotations/internal/ArrayRepresentationHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public static boolean canHandle(Type type) {
5151

5252
@Override
5353
public Object deserializeFromRepresentation(Representation repr, Function<String, RepresentationRestorer> getRegisteredRestorer) {
54+
if (repr == null)
55+
return null;
56+
5457
//Create array
5558
Object result = Array.newInstance(elementType, repr.list().size());
5659

@@ -64,6 +67,9 @@ public Object deserializeFromRepresentation(Representation repr, Function<String
6467

6568
@Override
6669
public Representation serializeToRepresentation(Object obj) {
70+
if (obj == null)
71+
return null;
72+
6773
ListRepresentation repr = new ListRepresentation();
6874

6975
for (int i=0; i<Array.getLength(obj); i++)

src/main/java/org/cryptimeleon/math/serialization/annotations/internal/DependentRepresentationHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ public static boolean canHandle(Type type) {
3838

3939
@Override
4040
public Object deserializeFromRepresentation(Representation repr, Function<String, RepresentationRestorer> getRegisteredRestorer) {
41+
if (repr == null)
42+
return null;
4143
return getRegisteredRestorer.apply(restorerString).restoreFromRepresentation(typeToRestore, repr);
4244
}
4345

4446
@Override
4547
public Representation serializeToRepresentation(Object object) {
48+
if (object == null)
49+
return null;
4650
return ((Representable) object).getRepresentation();
4751
}
4852
}

src/main/java/org/cryptimeleon/math/serialization/annotations/internal/ListAndSetRepresentationHandler.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ public static boolean canHandle(Type collectionType) { //handles List|Set<anythi
8888

8989
@SuppressWarnings({"rawtypes", "unchecked"})
9090
@Override
91-
public Object deserializeFromRepresentation(Representation repr,
92-
Function<String, RepresentationRestorer> getRegisteredRestorer) {
91+
public Object deserializeFromRepresentation(Representation repr, Function<String, RepresentationRestorer> getRegisteredRestorer) {
92+
if (repr == null)
93+
return null;
94+
9395
Collection result = null;
9496

9597
//Try to call default constructor to create collection.
@@ -130,12 +132,19 @@ public Object deserializeFromRepresentation(Representation repr,
130132

131133
@Override
132134
public Representation serializeToRepresentation(Object obj) {
135+
if (obj == null)
136+
return null;
137+
133138
if (!(obj instanceof List || obj instanceof Set))
134139
throw new IllegalArgumentException("Cannot handle representation of "+obj.getClass().getName());
140+
141+
if (!(obj instanceof List)) { //shuffle elements to avoid order leak
142+
obj = Arrays.asList(((Set<?>) obj).toArray());
143+
Collections.shuffle((List<?>) obj);
144+
}
145+
135146
ListRepresentation repr = new ListRepresentation();
136147
for (Object inner : (Iterable<?>) obj) {
137-
//TODO sort elements in case of a Set type to avoid leaking something.
138-
// Or is that done by the Converter? No, cannot.
139148
repr.put(elementHandler.serializeToRepresentation(inner));
140149
}
141150
return repr;

src/main/java/org/cryptimeleon/math/serialization/annotations/internal/MapRepresentationHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import java.lang.reflect.InvocationTargetException;
88
import java.lang.reflect.ParameterizedType;
99
import java.lang.reflect.Type;
10+
import java.util.Arrays;
1011
import java.util.LinkedHashMap;
1112
import java.util.Map;
13+
import java.util.Set;
1214
import java.util.function.Function;
1315

1416
/**
@@ -112,6 +114,9 @@ public static boolean canHandle(Type mapType) { //handles Map<anything>.
112114
@SuppressWarnings({"rawtypes", "unchecked"})
113115
@Override
114116
public Object deserializeFromRepresentation(Representation repr, Function<String, RepresentationRestorer> getRegisteredRestorer) {
117+
if (repr == null)
118+
return null;
119+
115120
Map result = null;
116121

117122
// Try to call default constructor to create collection.
@@ -141,8 +146,11 @@ public Object deserializeFromRepresentation(Representation repr, Function<String
141146

142147
@Override
143148
public Representation serializeToRepresentation(Object obj) {
149+
if (obj == null)
150+
return null;
144151
if (!(obj instanceof Map))
145152
throw new IllegalArgumentException("Cannot handle representation of "+obj.getClass().getName());
153+
146154
MapRepresentation repr = new MapRepresentation();
147155
((Map<?,?>) obj).forEach(
148156
(k, v) -> repr.put(

src/main/java/org/cryptimeleon/math/serialization/annotations/internal/StandaloneRepresentationHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public static boolean canHandle(Type type) {
4444
}
4545

4646
@Override
47-
public Object deserializeFromRepresentation(Representation repr,
48-
Function<String, RepresentationRestorer> getRegisteredRestorer) {
47+
public Object deserializeFromRepresentation(Representation repr, Function<String, RepresentationRestorer> getRegisteredRestorer) {
4948
if (repr == null) {
5049
return null;
5150
}

0 commit comments

Comments
 (0)