[alibaba/fastjson]Fast-Json解析gzip压缩的数据,不抛出异常,反而是直接返回null,对于定位问题很难定位,希望解决此BUG

2024-09-20 91 views
8

version:1.2.72 问题代码: com.alibaba.fastjson.JSON#parseObject, line 435

public static <T> T parseObject(byte[] bytes, int offset, int len,
                                    Charset charset,
                                    Type clazz,
                                    ParserConfig config,
                                    ParseProcess processor,
                                    int featureValues,
                                    Feature... features) {
        if (charset == null) {
            charset = IOUtils.UTF8;
        }

        String strVal;
        if (charset == IOUtils.UTF8) {
            char[] chars = allocateChars(bytes.length);
           // 如果使用gzip编码,那么读取出来的len=-1,所以直接返回null
            int chars_len = IOUtils.decodeUTF8(bytes, offset, len, chars);
            if (chars_len < 0) {
                return null;
            }
            strVal = new String(chars, 0, chars_len);
        } else {
            if (len < 0) {
                return null;
            }
            strVal = new String(bytes, offset, len, charset);
        }
        return (T) parseObject(strVal, clazz, config, processor, featureValues, features);
    }

解决方式,希望直接抛出异常: 理由:1)fastjson对于非json的符合编码字符串,直接抛出异常,为什么不对不符合字符编码的抛出异常 2)由于业务中经常遇到编码问题,这种隐式问题还希望及时抛出问题,即时定位问题

回答

3

1.2.75这个地方也没变,问题应该还是存在,请问能够提供一个最小demo吗

8

1.2.75这个地方也没变,问题应该还是存在,请问能够提供一个最小demo吗

    /**
     * GZIP 压缩
     */
    private static byte[] gzip(byte[] source) throws IOException {
        if (source == null) return null;
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
            gzip.write(source);
            return bos.toByteArray();
        }
    }

    @Test
    public void fastJsonBugTest() throws IOException {
        byte[] gzipBytes = gzip(JSON.toJSONString(Collections.singletonMap("key", "value")).getBytes());
        System.out.println(JSON.parseObject(gzipBytes, JSONObject.class) == null); // 输出 true
    }
6

1.2.83也是有这个问题的