Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1749)

Unified Diff: mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java

Issue 514293002: Mojo: validate nullability in Java bindings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add example Foo.java. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java
diff --git a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java
index 8295c5eed3202d0a3efa2a81b6adbecdce7f280a..3ec158dffb55577c828d551842d66920a3e81093 100644
--- a/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java
+++ b/mojo/public/java/bindings/src/org/chromium/mojo/bindings/Decoder.java
@@ -83,6 +83,21 @@ public class Decoder {
}
/**
+ * "Array bit" of |arrayNullability| is set iff the array itself is nullable.
+ */
+ public static final int ARRAY_NULLABLE = (1 << 0);
+
+ /**
+ * "Element bit" of |arrayNullability| is set iff the array elements are nullable.
+ */
+ public static final int ELEMENT_NULLABLE = (1 << 1);
+
+ /**
+ * Passed as |arrayNullability| when neither the array or its elements are nullable.
+ */
+ public static final int NOTHING_NULLABLE = 0;
qsr 2014/08/29 14:17:57 What about putting this flag first?
ppi 2014/08/29 14:35:49 Done.
+
+ /**
* The message to deserialize from.
*/
private final Message mMessage;
@@ -180,10 +195,14 @@ public class Decoder {
* Deserializes a pointer at the given offset. Returns a Decoder suitable to decode the content
* of the pointer.
*/
- public Decoder readPointer(int offset) {
+ public Decoder readPointer(int offset, boolean nullable) {
int basePosition = mBaseOffset + offset;
long pointerOffset = readLong(offset);
if (pointerOffset == 0) {
+ if (!nullable) {
+ throw new DeserializationException(
+ "Trying to decode null pointer for a non-nullable type.");
+ }
return null;
}
int newPosition = (int) (basePosition + pointerOffset);
@@ -195,8 +214,9 @@ public class Decoder {
/**
* Deserializes an array of boolean at the given offset.
*/
- public boolean[] readBooleans(int offset) {
- Decoder d = readPointer(offset);
+ public boolean[] readBooleans(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
qsr 2014/08/29 14:17:57 What do you think about having a static helper met
ppi 2014/08/29 14:35:49 Done.
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -219,8 +239,9 @@ public class Decoder {
/**
* Deserializes an array of bytes at the given offset.
*/
- public byte[] readBytes(int offset) {
- Decoder d = readPointer(offset);
+ public byte[] readBytes(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -234,8 +255,9 @@ public class Decoder {
/**
* Deserializes an array of shorts at the given offset.
*/
- public short[] readShorts(int offset) {
- Decoder d = readPointer(offset);
+ public short[] readShorts(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -249,8 +271,9 @@ public class Decoder {
/**
* Deserializes an array of ints at the given offset.
*/
- public int[] readInts(int offset) {
- Decoder d = readPointer(offset);
+ public int[] readInts(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -264,8 +287,9 @@ public class Decoder {
/**
* Deserializes an array of floats at the given offset.
*/
- public float[] readFloats(int offset) {
- Decoder d = readPointer(offset);
+ public float[] readFloats(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -279,8 +303,9 @@ public class Decoder {
/**
* Deserializes an array of longs at the given offset.
*/
- public long[] readLongs(int offset) {
- Decoder d = readPointer(offset);
+ public long[] readLongs(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -294,8 +319,9 @@ public class Decoder {
/**
* Deserializes an array of doubles at the given offset.
*/
- public double[] readDoubles(int offset) {
- Decoder d = readPointer(offset);
+ public double[] readDoubles(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -309,9 +335,13 @@ public class Decoder {
/**
* Deserializes an |Handle| at the given offset.
*/
- public Handle readHandle(int offset) {
+ public Handle readHandle(int offset, boolean nullable) {
int index = readInt(offset);
if (index == -1) {
+ if (!nullable) {
+ throw new DeserializationException(
+ "Trying to decode an invalid handle for a non-nullable type.");
+ }
return InvalidHandle.INSTANCE;
}
mValidator.claimHandle(index);
@@ -321,36 +351,36 @@ public class Decoder {
/**
* Deserializes an |UntypedHandle| at the given offset.
*/
- public UntypedHandle readUntypedHandle(int offset) {
- return readHandle(offset).toUntypedHandle();
+ public UntypedHandle readUntypedHandle(int offset, boolean nullable) {
+ return readHandle(offset, nullable).toUntypedHandle();
}
/**
* Deserializes a |ConsumerHandle| at the given offset.
*/
- public DataPipe.ConsumerHandle readConsumerHandle(int offset) {
- return readUntypedHandle(offset).toDataPipeConsumerHandle();
+ public DataPipe.ConsumerHandle readConsumerHandle(int offset, boolean nullable) {
+ return readUntypedHandle(offset, nullable).toDataPipeConsumerHandle();
}
/**
* Deserializes a |ProducerHandle| at the given offset.
*/
- public DataPipe.ProducerHandle readProducerHandle(int offset) {
- return readUntypedHandle(offset).toDataPipeProducerHandle();
+ public DataPipe.ProducerHandle readProducerHandle(int offset, boolean nullable) {
+ return readUntypedHandle(offset, nullable).toDataPipeProducerHandle();
}
/**
* Deserializes a |MessagePipeHandle| at the given offset.
*/
- public MessagePipeHandle readMessagePipeHandle(int offset) {
- return readUntypedHandle(offset).toMessagePipeHandle();
+ public MessagePipeHandle readMessagePipeHandle(int offset, boolean nullable) {
+ return readUntypedHandle(offset, nullable).toMessagePipeHandle();
}
/**
* Deserializes a |SharedBufferHandle| at the given offset.
*/
- public SharedBufferHandle readSharedBufferHandle(int offset) {
- return readUntypedHandle(offset).toSharedBufferHandle();
+ public SharedBufferHandle readSharedBufferHandle(int offset, boolean nullable) {
+ return readUntypedHandle(offset, nullable).toSharedBufferHandle();
}
/**
@@ -358,9 +388,9 @@ public class Decoder {
*
* @return a proxy to the service.
*/
- public <P extends Proxy> P readServiceInterface(int offset,
+ public <P extends Proxy> P readServiceInterface(int offset, boolean nullable,
Interface.Manager<?, P> manager) {
- MessagePipeHandle handle = readMessagePipeHandle(offset);
+ MessagePipeHandle handle = readMessagePipeHandle(offset, nullable);
if (!handle.isValid()) {
return null;
}
@@ -370,8 +400,9 @@ public class Decoder {
/**
* Deserializes a |InterfaceRequest| at the given offset.
*/
- public <I extends Interface> InterfaceRequest<I> readInterfaceRequest(int offset) {
- MessagePipeHandle handle = readMessagePipeHandle(offset);
+ public <I extends Interface> InterfaceRequest<I> readInterfaceRequest(int offset,
+ boolean nullable) {
+ MessagePipeHandle handle = readMessagePipeHandle(offset, nullable);
if (handle == null) {
return null;
}
@@ -381,8 +412,9 @@ public class Decoder {
/**
* Deserializes a string at the given offset.
*/
- public String readString(int offset) {
- byte[] bytes = readBytes(offset);
+ public String readString(int offset, boolean nullable) {
+ final int arrayNullability = nullable ? ARRAY_NULLABLE : 0;
+ byte[] bytes = readBytes(offset, arrayNullability);
if (bytes == null) {
return null;
}
@@ -392,8 +424,10 @@ public class Decoder {
/**
* Deserializes an array of |Handle| at the given offset.
*/
- public Handle[] readHandles(int offset) {
- Decoder d = readPointer(offset);
+ public Handle[] readHandles(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ final boolean elementNullable = (arrayNullability & ELEMENT_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -401,7 +435,7 @@ public class Decoder {
Handle[] result = new Handle[si.numFields];
for (int i = 0; i < result.length; ++i) {
result[i] = d.readHandle(
- DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i);
+ DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i, true);
qsr 2014/08/29 14:17:57 Don't you want to use your flag here instead of tr
ppi 2014/08/29 14:35:49 Done.
}
return result;
}
@@ -409,8 +443,10 @@ public class Decoder {
/**
* Deserializes an array of |UntypedHandle| at the given offset.
*/
- public UntypedHandle[] readUntypedHandles(int offset) {
- Decoder d = readPointer(offset);
+ public UntypedHandle[] readUntypedHandles(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ final boolean elementNullable = (arrayNullability & ELEMENT_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -418,7 +454,7 @@ public class Decoder {
UntypedHandle[] result = new UntypedHandle[si.numFields];
for (int i = 0; i < result.length; ++i) {
result[i] = d.readUntypedHandle(
- DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i);
+ DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i, true);
}
return result;
}
@@ -426,8 +462,10 @@ public class Decoder {
/**
* Deserializes an array of |ConsumerHandle| at the given offset.
*/
- public DataPipe.ConsumerHandle[] readConsumerHandles(int offset) {
- Decoder d = readPointer(offset);
+ public DataPipe.ConsumerHandle[] readConsumerHandles(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ final boolean elementNullable = (arrayNullability & ELEMENT_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -435,7 +473,7 @@ public class Decoder {
DataPipe.ConsumerHandle[] result = new DataPipe.ConsumerHandle[si.numFields];
for (int i = 0; i < result.length; ++i) {
result[i] = d.readConsumerHandle(
- DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i);
+ DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i, true);
}
return result;
}
@@ -443,8 +481,10 @@ public class Decoder {
/**
* Deserializes an array of |ProducerHandle| at the given offset.
*/
- public DataPipe.ProducerHandle[] readProducerHandles(int offset) {
- Decoder d = readPointer(offset);
+ public DataPipe.ProducerHandle[] readProducerHandles(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ final boolean elementNullable = (arrayNullability & ELEMENT_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -452,7 +492,7 @@ public class Decoder {
DataPipe.ProducerHandle[] result = new DataPipe.ProducerHandle[si.numFields];
for (int i = 0; i < result.length; ++i) {
result[i] = d.readProducerHandle(
- DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i);
+ DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i, true);
}
return result;
@@ -461,8 +501,10 @@ public class Decoder {
/**
* Deserializes an array of |MessagePipeHandle| at the given offset.
*/
- public MessagePipeHandle[] readMessagePipeHandles(int offset) {
- Decoder d = readPointer(offset);
+ public MessagePipeHandle[] readMessagePipeHandles(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ final boolean elementNullable = (arrayNullability & ELEMENT_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -470,7 +512,7 @@ public class Decoder {
MessagePipeHandle[] result = new MessagePipeHandle[si.numFields];
for (int i = 0; i < result.length; ++i) {
result[i] = d.readMessagePipeHandle(
- DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i);
+ DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i, true);
}
return result;
@@ -479,8 +521,10 @@ public class Decoder {
/**
* Deserializes an array of |SharedBufferHandle| at the given offset.
*/
- public SharedBufferHandle[] readSharedBufferHandles(int offset) {
- Decoder d = readPointer(offset);
+ public SharedBufferHandle[] readSharedBufferHandles(int offset, int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ final boolean elementNullable = (arrayNullability & ELEMENT_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -488,7 +532,7 @@ public class Decoder {
SharedBufferHandle[] result = new SharedBufferHandle[si.numFields];
for (int i = 0; i < result.length; ++i) {
result[i] = d.readSharedBufferHandle(
- DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i);
+ DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i, true);
}
return result;
@@ -498,8 +542,10 @@ public class Decoder {
* Deserializes an array of |ServiceHandle| at the given offset.
*/
public <S extends Interface, P extends Proxy> S[] readServiceInterfaces(int offset,
- Interface.Manager<S, P> manager) {
- Decoder d = readPointer(offset);
+ int arrayNullability, Interface.Manager<S, P> manager) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ final boolean elementNullable = (arrayNullability & ELEMENT_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -510,7 +556,8 @@ public class Decoder {
// Manager<S, ? extends S>
@SuppressWarnings("unchecked")
S value = (S) d.readServiceInterface(
- DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i, manager);
+ DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i,
+ elementNullable, manager);
result[i] = value;
}
return result;
@@ -519,8 +566,11 @@ public class Decoder {
/**
* Deserializes an array of |InterfaceRequest| at the given offset.
*/
- public <I extends Interface> InterfaceRequest<I>[] readInterfaceRequests(int offset) {
- Decoder d = readPointer(offset);
+ public <I extends Interface> InterfaceRequest<I>[] readInterfaceRequests(int offset,
+ int arrayNullability) {
+ final boolean arrayNullable = (arrayNullability & ARRAY_NULLABLE) > 0;
+ final boolean elementNullable = (arrayNullability & ELEMENT_NULLABLE) > 0;
+ Decoder d = readPointer(offset, arrayNullable);
if (d == null) {
return null;
}
@@ -529,7 +579,8 @@ public class Decoder {
InterfaceRequest<I>[] result = new InterfaceRequest[si.numFields];
for (int i = 0; i < result.length; ++i) {
result[i] = d.readInterfaceRequest(
- DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i);
+ DataHeader.HEADER_SIZE + BindingsHelper.SERIALIZED_HANDLE_SIZE * i,
+ elementNullable);
}
return result;
}

Powered by Google App Engine
This is Rietveld 408576698