Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

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

Closed
Anthony-Dong opened this issue Jan 13, 2021 · 3 comments
Milestone

Comments

@Anthony-Dong
Copy link

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)由于业务中经常遇到编码问题,这种隐式问题还希望及时抛出问题,即时定位问题

@Certseeds
Copy link
Contributor

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

@Anthony-Dong
Copy link
Author

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
    }

@wenshao wenshao added this to the 1.2.76 milestone Apr 5, 2021
@wenshao wenshao closed this as completed in e2f9cc6 Apr 5, 2021
@JianhuangShu
Copy link

1.2.83也是有这个问题的

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants