diff --git a/net/src/main/java/com/zfoo/net/handler/codec/jprotobuf/JProtobufTcpCodecHandler.java b/net/src/main/java/com/zfoo/net/handler/codec/jprotobuf/JProtobufTcpCodecHandler.java index 7eaa4119..7ed464d2 100644 --- a/net/src/main/java/com/zfoo/net/handler/codec/jprotobuf/JProtobufTcpCodecHandler.java +++ b/net/src/main/java/com/zfoo/net/handler/codec/jprotobuf/JProtobufTcpCodecHandler.java @@ -20,7 +20,7 @@ import com.zfoo.net.packet.EncodedPacketInfo; import com.zfoo.net.packet.PacketService; import com.zfoo.protocol.ProtocolManager; import com.zfoo.protocol.buffer.ByteBufUtils; -import com.zfoo.protocol.util.MathSafeUtils; +import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.StringUtils; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -47,7 +47,7 @@ public class JProtobufTcpCodecHandler extends ByteToMessageCodec MathSafeUtils.MAX_LENGTH) { + if (length < 0 || length > IOUtils.BYTES_PER_MB) { throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length)); } diff --git a/net/src/main/java/com/zfoo/net/handler/codec/tcp/TcpCodecHandler.java b/net/src/main/java/com/zfoo/net/handler/codec/tcp/TcpCodecHandler.java index 7c463f2e..80db63d4 100644 --- a/net/src/main/java/com/zfoo/net/handler/codec/tcp/TcpCodecHandler.java +++ b/net/src/main/java/com/zfoo/net/handler/codec/tcp/TcpCodecHandler.java @@ -16,7 +16,7 @@ package com.zfoo.net.handler.codec.tcp; import com.zfoo.net.NetContext; import com.zfoo.net.packet.EncodedPacketInfo; import com.zfoo.net.packet.PacketService; -import com.zfoo.protocol.util.MathSafeUtils; +import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.StringUtils; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -42,7 +42,7 @@ public class TcpCodecHandler extends ByteToMessageCodec { var length = in.readInt(); // 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽 - if (length < 0 || length > MathSafeUtils.MAX_LENGTH) { + if (length < 0 || length > IOUtils.BYTES_PER_MB) { throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length)); } diff --git a/net/src/main/java/com/zfoo/net/handler/codec/udp/UdpCodecHandler.java b/net/src/main/java/com/zfoo/net/handler/codec/udp/UdpCodecHandler.java index 8c66af2b..5ebbfa0b 100644 --- a/net/src/main/java/com/zfoo/net/handler/codec/udp/UdpCodecHandler.java +++ b/net/src/main/java/com/zfoo/net/handler/codec/udp/UdpCodecHandler.java @@ -16,7 +16,7 @@ import com.zfoo.net.NetContext; import com.zfoo.net.packet.EncodedPacketInfo; import com.zfoo.net.packet.PacketService; import com.zfoo.net.router.attachment.UdpAttachment; -import com.zfoo.protocol.util.MathSafeUtils; +import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.StringUtils; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -44,7 +44,7 @@ public class UdpCodecHandler extends MessageToMessageCodec MathSafeUtils.MAX_LENGTH) { + if (length < 0 || length > IOUtils.BYTES_PER_MB) { throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length)); } diff --git a/net/src/main/java/com/zfoo/net/handler/codec/websocket/WebSocketCodecHandler.java b/net/src/main/java/com/zfoo/net/handler/codec/websocket/WebSocketCodecHandler.java index c8cc48ac..59a05f82 100644 --- a/net/src/main/java/com/zfoo/net/handler/codec/websocket/WebSocketCodecHandler.java +++ b/net/src/main/java/com/zfoo/net/handler/codec/websocket/WebSocketCodecHandler.java @@ -15,7 +15,7 @@ package com.zfoo.net.handler.codec.websocket; import com.zfoo.net.NetContext; import com.zfoo.net.packet.EncodedPacketInfo; -import com.zfoo.protocol.util.MathSafeUtils; +import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.StringUtils; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -38,7 +38,7 @@ public class WebSocketCodecHandler extends MessageToMessageCodec MathSafeUtils.MAX_LENGTH) { + if (length < 0 || length > IOUtils.BYTES_PER_MB) { throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length)); } var sliceByteBuf = in.readSlice(length); diff --git a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java index 6267130b..cfea9334 100644 --- a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java @@ -867,7 +867,7 @@ public abstract class ByteBufUtils { public static short[] readShortArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var shorts = new short[CollectionUtils.comfortableLength(length)]; + var shorts = new short[CollectionUtils.comfortableShortLength(length)]; var readIndex = byteBuf.readerIndex(); for (var i = 0; i < length; i++) { shorts[i] = byteBuf.getShort(readIndex); @@ -890,7 +890,7 @@ public abstract class ByteBufUtils { public static Short[] readShortBoxArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var shorts = new Short[CollectionUtils.comfortableLength(length)]; + var shorts = new Short[CollectionUtils.comfortableShortLength(length)]; for (var i = 0; i < length; i++) { shorts[i] = readShortBox(byteBuf); } @@ -944,7 +944,7 @@ public abstract class ByteBufUtils { public static int[] readIntArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var ints = new int[CollectionUtils.comfortableLength(length)]; + var ints = new int[CollectionUtils.comfortableIntLength(length)]; for (var i = 0; i < length; i++) { ints[i] = readInt(byteBuf); } @@ -964,7 +964,7 @@ public abstract class ByteBufUtils { public static Integer[] readIntBoxArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var ints = new Integer[CollectionUtils.comfortableLength(length)]; + var ints = new Integer[CollectionUtils.comfortableIntLength(length)]; for (var i = 0; i < length; i++) { ints[i] = readIntBox(byteBuf); } @@ -1018,7 +1018,7 @@ public abstract class ByteBufUtils { public static long[] readLongArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var longs = new long[CollectionUtils.comfortableLength(length)]; + var longs = new long[CollectionUtils.comfortableLongLength(length)]; for (var i = 0; i < length; i++) { longs[i] = readLong(byteBuf); } @@ -1038,7 +1038,7 @@ public abstract class ByteBufUtils { public static Long[] readLongBoxArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var longs = new Long[CollectionUtils.comfortableLength(length)]; + var longs = new Long[CollectionUtils.comfortableLongLength(length)]; for (var i = 0; i < length; i++) { longs[i] = readLongBox(byteBuf); } @@ -1096,7 +1096,7 @@ public abstract class ByteBufUtils { public static float[] readFloatArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var floats = new float[CollectionUtils.comfortableLength(length)]; + var floats = new float[CollectionUtils.comfortableIntLength(length)]; var readIndex = byteBuf.readerIndex(); for (var i = 0; i < length; i++) { floats[i] = byteBuf.getFloat(readIndex); @@ -1119,7 +1119,7 @@ public abstract class ByteBufUtils { public static Float[] readFloatBoxArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var floats = new Float[CollectionUtils.comfortableLength(length)]; + var floats = new Float[CollectionUtils.comfortableIntLength(length)]; for (var i = 0; i < length; i++) { floats[i] = readFloatBox(byteBuf); } @@ -1177,7 +1177,7 @@ public abstract class ByteBufUtils { public static double[] readDoubleArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var doubles = new double[CollectionUtils.comfortableLength(length)]; + var doubles = new double[CollectionUtils.comfortableLongLength(length)]; var readIndex = byteBuf.readerIndex(); for (var i = 0; i < length; i++) { doubles[i] = byteBuf.getDouble(readIndex); @@ -1200,7 +1200,7 @@ public abstract class ByteBufUtils { public static Double[] readDoubleBoxArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var doubles = new Double[CollectionUtils.comfortableLength(length)]; + var doubles = new Double[CollectionUtils.comfortableLongLength(length)]; for (var i = 0; i < length; i++) { doubles[i] = readDoubleBox(byteBuf); } @@ -1253,7 +1253,7 @@ public abstract class ByteBufUtils { public static String[] readStringArray(ByteBuf byteBuf) { var length = readInt(byteBuf); - var strings = new String[CollectionUtils.comfortableLength(length)]; + var strings = new String[CollectionUtils.comfortableLongLength(length)]; for (var i = 0; i < length; i++) { strings[i] = readString(byteBuf); } diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/CollectionUtils.java b/protocol/src/main/java/com/zfoo/protocol/collection/CollectionUtils.java index ad37f0f2..34221ab0 100644 --- a/protocol/src/main/java/com/zfoo/protocol/collection/CollectionUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/collection/CollectionUtils.java @@ -100,9 +100,38 @@ public abstract class CollectionUtils { * CN: 数组初始化长度的安全上限限制,防止反序列化异常导致内存突然升高 */ public static int comfortableLength(int length) { - if (length >= IOUtils.BYTES_PER_MB) { + if (length >= MathSafeUtils.MAX_LENGTH) { throw new ArrayStoreException(StringUtils.format("The length of the newly created array [{}] exceeds the set safety range [{}]" - , length, IOUtils.BYTES_PER_MB)); + , length, MathSafeUtils.MAX_LENGTH)); + } + return length; + } + public static int comfortableShortLength(int length) { + if (length >= MathSafeUtils.MAX_LENGTH_SHORT_ARRAY) { + throw new ArrayStoreException(StringUtils.format("The length of the newly created array [{}] exceeds the set safety range [{}]" + , length, MathSafeUtils.MAX_LENGTH_SHORT_ARRAY)); + } + return length; + } + public static int comfortableIntLength(int length) { + if (length >= MathSafeUtils.MAX_LENGTH_INT_ARRAY) { + throw new ArrayStoreException(StringUtils.format("The length of the newly created array [{}] exceeds the set safety range [{}]" + , length, MathSafeUtils.MAX_LENGTH_INT_ARRAY)); + } + return length; + } + public static int comfortableLongLength(int length) { + if (length >= MathSafeUtils.MAX_LENGTH_LONG_ARRAY) { + throw new ArrayStoreException(StringUtils.format("The length of the newly created array [{}] exceeds the set safety range [{}]" + , length, MathSafeUtils.MAX_LENGTH_LONG_ARRAY)); + } + return length; + } + + public static int comfortableObjectLength(int length) { + if (length >= MathSafeUtils.MAX_LENGTH_OBJECT_ARRAY) { + throw new ArrayStoreException(StringUtils.format("The length of the newly created array [{}] exceeds the set safety range [{}]" + , length, MathSafeUtils.MAX_LENGTH_OBJECT_ARRAY)); } return length; } diff --git a/protocol/src/main/java/com/zfoo/protocol/util/MathSafeUtils.java b/protocol/src/main/java/com/zfoo/protocol/util/MathSafeUtils.java index 3a1efa7d..50ea3d6b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/util/MathSafeUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/util/MathSafeUtils.java @@ -5,10 +5,11 @@ package com.zfoo.protocol.util; */ public abstract class MathSafeUtils { - public static long MAX_LENGTH = IOUtils.BYTES_PER_MB; - public static long MAX_LENGTH_SHORT_ARRAY = MAX_LENGTH / 2; - public static long MAX_LENGTH_INT_ARRAY = MAX_LENGTH / 4; - public static long MAX_LENGTH_LONG_ARRAY = MAX_LENGTH / 8; + public static final long MAX_LENGTH = IOUtils.BYTES_PER_MB; + public static final long MAX_LENGTH_SHORT_ARRAY = MAX_LENGTH / 2; + public static final long MAX_LENGTH_INT_ARRAY = MAX_LENGTH / 4; + public static final long MAX_LENGTH_LONG_ARRAY = MAX_LENGTH / 8; + public static final long MAX_LENGTH_OBJECT_ARRAY = MAX_LENGTH / 16; public static int findNextPositivePowerOfTwo(int value) { assert value > Integer.MIN_VALUE && value < IOUtils.BYTES_PER_MB;