starlight icon indicating copy to clipboard operation
starlight copied to clipboard

Fail-fast in unpack will be better?

Open iSenninha opened this issue 4 years ago • 1 comments

BaiduRpcProtocol now is below

            int bodySize = fixHeaderBuf.readInt();
            if (in.readableBytes() < FIXED_LEN + bodySize) {
                throw notEnoughDataException;
            }
            // 512M
            if (bodySize > 512 * 1024 * 1024) {
                throw new TooBigDataException("to big body size:" + bodySize);
            }

change sequence like this will be better?

            int bodySize = fixHeaderBuf.readInt();
            // 512M
            if (bodySize > 512 * 1024 * 1024) {
                throw new TooBigDataException("to big body size:" + bodySize);
            }
            if (in.readableBytes() < FIXED_LEN + bodySize) {
                throw notEnoughDataException;
            }

iSenninha avatar Sep 15 '19 07:09 iSenninha

why? Since notEnoughDataException is more likely to happen than TooBigDataException.

wenweihu86 avatar Sep 16 '19 02:09 wenweihu86