Skip to content

Commit 684cac8

Browse files
author
Jan Bobolz
authored
Merge pull request #64 from cryptimeleon/fix-reprutil
Fixed handling of null representables in ReprUtil
2 parents 2f7d702 + fbd2d9c commit 684cac8

5 files changed

Lines changed: 30 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public static boolean canHandle(Type mapType) { //handles Map<anything>.
112112
@SuppressWarnings({"rawtypes", "unchecked"})
113113
@Override
114114
public Object deserializeFromRepresentation(Representation repr, Function<String, RepresentationRestorer> getRegisteredRestorer) {
115+
if (repr == null)
116+
return null;
117+
115118
Map result = null;
116119

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

142145
@Override
143146
public Representation serializeToRepresentation(Object obj) {
147+
if (obj == null)
148+
return null;
144149
if (!(obj instanceof Map))
145150
throw new IllegalArgumentException("Cannot handle representation of "+obj.getClass().getName());
151+
146152
MapRepresentation repr = new MapRepresentation();
147153
((Map<?,?>) obj).forEach(
148154
(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)