cloudwu / sproto

Yet another protocol library like google protocol buffers , but simple and fast.
MIT License
942 stars 253 forks source link

pack/unpack 结尾00信息无法还原, 不影响使用. #100

Closed fanlix closed 3 years ago

fanlix commented 3 years ago

查bug过程中抓包发现数据pack后再upack回来, 偶尔与原始数据不符的情况. 原因是pack算法中,未对结尾00做处理, unpack时强制补足00

local function test_pack_tail_zero()
    local pto = sprotoloader.load(1)
    local function test(hs)
        local s = hexstr2str(hs)
        local p = pto.pack(s)
        local up = pto.unpack(p)
        log("test pack, s=%s p=%s up=%s", hexstr(s), hexstr(p), hexstr(up))
    end

    test("AB12")
    test("AB120000")
    test("AB12000000000000")
    test("00")
    test("000000000000000000")
end
[:0100000d] test pack, s=AB12 p=03AB12 up=AB12000000000000
[:0100000d] test pack, s=AB120000 p=03AB12 up=AB12000000000000
[:0100000d] test pack, s=AB12000000000000 p=03AB12 up=AB12000000000000
[:0100000d] test pack, s=00 p=00 up=0000000000000000
[:0100000d] test pack, s=000000000000000000 p=0000 up=00000000000000000000000000000000

结论:

Mark一下这个知识点,大家可能遇到. 可以直接close.

fanlix commented 3 years ago

看了Cap'n的算法, 对00有特殊处理, 可以还原00 sproto用不到, 省掉这个逻辑很合理.