HIGUCHI Yuta
Committed by Gerrit Code Review

Add some serializer related sanity check

Change-Id: I5ebdde41f5528b5bad0c704b11c191afed45691e
......@@ -15,6 +15,8 @@
*/
package org.onosproject.store.serializers;
import static com.google.common.base.Preconditions.checkArgument;
import org.onlab.packet.Ip4Address;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
......@@ -45,6 +47,7 @@ public class Ip4AddressSerializer extends Serializer<Ip4Address> {
@Override
public Ip4Address read(Kryo kryo, Input input, Class<Ip4Address> type) {
final int octLen = input.readInt();
checkArgument(octLen == Ip4Address.BYTE_LENGTH);
byte[] octs = new byte[octLen];
input.readBytes(octs);
return Ip4Address.valueOf(octs);
......
......@@ -15,8 +15,10 @@
*/
package org.onosproject.store.serializers;
import org.onlab.packet.Ip4Prefix;
import static com.google.common.base.Preconditions.checkArgument;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.Ip4Prefix;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
......@@ -49,6 +51,7 @@ public final class Ip4PrefixSerializer extends Serializer<Ip4Prefix> {
public Ip4Prefix read(Kryo kryo, Input input,
Class<Ip4Prefix> type) {
int octLen = input.readInt();
checkArgument(octLen <= Ip4Address.BYTE_LENGTH);
byte[] octs = new byte[octLen];
input.readBytes(octs);
int prefLen = input.readInt();
......
......@@ -15,6 +15,8 @@
*/
package org.onosproject.store.serializers;
import static com.google.common.base.Preconditions.checkArgument;
import org.onlab.packet.Ip6Address;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
......@@ -45,6 +47,7 @@ public class Ip6AddressSerializer extends Serializer<Ip6Address> {
@Override
public Ip6Address read(Kryo kryo, Input input, Class<Ip6Address> type) {
final int octLen = input.readInt();
checkArgument(octLen == Ip6Address.BYTE_LENGTH);
byte[] octs = new byte[octLen];
input.readBytes(octs);
return Ip6Address.valueOf(octs);
......
......@@ -15,8 +15,10 @@
*/
package org.onosproject.store.serializers;
import org.onlab.packet.Ip6Prefix;
import static com.google.common.base.Preconditions.checkArgument;
import org.onlab.packet.Ip6Address;
import org.onlab.packet.Ip6Prefix;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
......@@ -49,6 +51,7 @@ public final class Ip6PrefixSerializer extends Serializer<Ip6Prefix> {
public Ip6Prefix read(Kryo kryo, Input input,
Class<Ip6Prefix> type) {
int octLen = input.readInt();
checkArgument(octLen <= Ip6Address.BYTE_LENGTH);
byte[] octs = new byte[octLen];
input.readBytes(octs);
int prefLen = input.readInt();
......
......@@ -15,6 +15,8 @@
*/
package org.onosproject.store.serializers;
import static com.google.common.base.Preconditions.checkArgument;
import org.onlab.packet.IpAddress;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
......@@ -44,6 +46,7 @@ public class IpAddressSerializer extends Serializer<IpAddress> {
@Override
public IpAddress read(Kryo kryo, Input input, Class<IpAddress> type) {
final int octLen = input.readInt();
checkArgument(octLen <= IpAddress.INET6_BYTE_LENGTH);
byte[] octs = new byte[octLen];
input.readBytes(octs);
// Use the address size to decide whether it is IPv4 or IPv6 address
......
......@@ -15,6 +15,8 @@
*/
package org.onosproject.store.serializers;
import static com.google.common.base.Preconditions.checkArgument;
import org.onlab.packet.IpAddress;
import org.onlab.packet.IpPrefix;
......@@ -49,6 +51,7 @@ public final class IpPrefixSerializer extends Serializer<IpPrefix> {
public IpPrefix read(Kryo kryo, Input input,
Class<IpPrefix> type) {
int octLen = input.readInt();
checkArgument(octLen <= IpAddress.INET6_BYTE_LENGTH);
byte[] octs = new byte[octLen];
input.readBytes(octs);
int prefLen = input.readInt();
......
......@@ -15,6 +15,8 @@
*/
package org.onlab.util;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
......@@ -22,6 +24,7 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
......@@ -58,6 +61,8 @@ public final class KryoNamespace implements KryoFactory, KryoPool {
*/
public static final int INITIAL_ID = 11;
private static final Logger log = getLogger(KryoNamespace.class);
private final KryoPool pool = new KryoPool.Builder(this)
.softReferences()
......@@ -101,6 +106,12 @@ public final class KryoNamespace implements KryoFactory, KryoPool {
*/
public Builder nextId(final int id) {
if (!types.isEmpty()) {
if (id != FLOATING_ID && id < blockHeadId + types.size()) {
log.warn("requested nextId {} could potentially overlap" +
"with existing registrations {}+{} ",
id, blockHeadId, types.size());
}
blocks.add(new RegistrationBlock(this.blockHeadId, types));
types = new ArrayList<>();
}
......