Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int64 encoder _VarintSize error! #23

Open
zhenmu opened this issue Jan 20, 2016 · 8 comments
Open

int64 encoder _VarintSize error! #23

zhenmu opened this issue Jan 20, 2016 · 8 comments

Comments

@zhenmu
Copy link

zhenmu commented Jan 20, 2016

有64位字段的message,里面数值很大时 在计算 bytesize时出错, 应该是之前代码没兼容64位

function _VarintSize(value)
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
return 5
end

may need to change:

function _VarintSize(value)
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
if value <= 0x7ffffffff then return 5 end
if value <= 0x3ffffffffff then return 6 end
if value <= 0x1ffffffffffff then return 7 end
if value <= 0xffffffffffffff then return 8 end
if value <= 0x7fffffffffffffff then return 9 end
return 10
end

不知道这样改可行不可行?

@cp790621656
Copy link

@zhenmu

我今天也遇到你这个问题,测试发现这样改没有效果。

@zhenmu
Copy link
Author

zhenmu commented Feb 26, 2016

@cp790621656

我看了一下我们项目里完整的修改

1.encoder.lua

function _VarintSize(value)
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
return 5
end

function _SignedVarintSize(value)
if value < 0 then return 10 end
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
return 5
end

改成

function _VarintSize(value)
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
if value <= 0x7ffffffff then return 5 end
if value <= 0x3ffffffffff then return 6 end
if value <= 0x1ffffffffffff then return 7 end
if value <= 0xffffffffffffff then return 8 end
if value <= 0x7fffffffffffffff then return 9 end
return 10
end

function _SignedVarintSize(value)
if value < 0 then return 10 end
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
if value <= 0x7ffffffff then return 5 end
if value <= 0x3ffffffffff then return 6 end
if value <= 0x1ffffffffffff then return 7 end
if value <= 0xffffffffffffff then return 8 end
if value <= 0x7fffffffffffffff then return 9 end
return 10
end

2.ware_format里 (非必要?)

local function _VarUInt64ByteSizeNoTag(uint64)
if uint64 <= 0x7f then return 1 end
if uint64 <= 0x3fff then return 2 end
if uint64 <= 0x1fffff then return 3 end
if uint64 <= 0xfffffff then return 4 end
return 5
end

改成

local function _VarUInt64ByteSizeNoTag(uint64)
if uint64 <= 0x7f then return 1 end
if uint64 <= 0x3fff then return 2 end
if uint64 <= 0x1fffff then return 3 end
if uint64 <= 0xfffffff then return 4 end
if uint64 <= 0x7ffffffff then return 5 end
if uint64 <= 0x3ffffffffff then return 6 end
if uint64 <= 0x1ffffffffffff then return 7 end
if uint64 <= 0xffffffffffffff then return 8 end
if uint64 <= 0x7fffffffffffffff then return 9 end
return 10
end

  1. type_checkers.lua里 (这个范围是我们自己根据一些情况 设定了一个范围)
    增加

function Int64ValueChecker()
local _MIN = -562949953421312
local _MAX = 562949953421312
return function(proposed_value)
if type(proposed_value) ~= 'number' then
error(string.format('%s has type %s, but expected one of: number',
proposed_value, type(proposed_value)))
end
if _MIN > proposed_value or proposed_value > _MAX then
error('Value out of range: ' .. proposed_value)
end
end
end

function Uint64ValueChecker(IntValueChecker)
local _MIN = 0
local _MAX = 1125899906842624

return function(proposed_value)
    if type(proposed_value) ~= 'number' then
        error(string.format('%s has type %s, but expected one of: number',
            proposed_value, type(proposed_value)))
    end
    if _MIN > proposed_value or proposed_value > _MAX then
        error('Value out of range: ' .. proposed_value)
    end
end

end

4.protobuf.lua

[FieldDescriptor.CPPTYPE_INT64] = type_checkers.Int32ValueChecker(),

[FieldDescriptor.CPPTYPE_UINT64] = type_checkers.Uint32ValueChecker(),

改成

[FieldDescriptor.CPPTYPE_INT64] = type_checkers.Int64ValueChecker(),

[FieldDescriptor.CPPTYPE_UINT64] = type_checkers.Uint64ValueChecker(),

仅供参考

@cp790621656
Copy link

测试OK啦,安卓上也测试OK,非常感谢您!! @zhenmu

@mirchd
Copy link

mirchd commented Jun 21, 2018

测试可用,感谢楼主

@NiYun
Copy link

NiYun commented Nov 3, 2018

如上修改 发现,java后端传来的long型数值,在客户端解析的数据溢出

@mirchd
Copy link

mirchd commented Nov 5, 2018

最大支持50位。

@yening520
Copy link

最大支持50位。

为什么最大是50位呀,哪里有限制的呢,不是真的int64呢

@zhenmu
Copy link
Author

zhenmu commented Nov 17, 2020

最大支持50位。

为什么最大是50位呀,哪里有限制的呢,不是真的int64呢

lua存放数值这类的都是用double的

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

No branches or pull requests

5 participants