8000 fix(lua): Use `lua_tonumber` to support 32-bit platforms by gucci-on-fleek · Pull Request #4369 · collectd/collectd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(lua): Use lua_tonumber to support 32-bit platforms #4369

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gucci-on-fleek
Copy link

Previously, luaC_tovalue used lua_tointeger for variables of type "derive", "counter", and "absolute". However, on 32-bit platforms, lua_Integer may be only 32 bits wide, so if you set values to a value larger than 2^31-1 from Lua, the value of lua_tointeger will be unspecified.

This commit changes the code to use lua_tonumber on 32-bit platforms, which returns a float, which will then be cast to the (typedef'ed to [u]int64_t) integer types derive_t, counter_t, and absolute_t`, ensuring that large values are not truncated.

When lua_Integer is 64 bits wide, the code will still use lua_tointeger to ensure that integers between 2^53 and 2^63-1 do not lose precision on newer versions of Lua with dedicated integer types.

I've only personally tested this patch on a 32-bit platform, but in theory this patch shouldn't affect 64-bit platforms at all.

ChangeLog: Lua plugin: Support "derive", "counter", and "absolute" values >2^31 on 32-bit platforms.

Previously, `luaC_tovalue` used `lua_tointeger` for variables of type
"derive", "counter", and "absolute". However, on 32-bit platforms,
`lua_Integer` may be only 32 bits wide [1], so if you set `values` to a
value larger than 2^31-1 from Lua, the value of `lua_tointeger` will be
unspecified [2].

This commit changes the code to use `lua_tonumber` on 32-bit platforms,
which returns a float, which will then be cast to the (typedef'ed to
`[u]int64_t`) integer types `derive_t`, `counter_t`, and `absolute_t`,
ensuring that large values are not truncated.

When `lua_Integer` is 64 bits wide, the code will still use
`lua_tointeger` to ensure that integers between 2^53 and 2^63-1 do not
lose precision on newer versions of Lua with dedicated integer
types [3].

[1]: https://www.lua.org/manual/5.1/manual.html#lua_Integer

[2]: https://www.lua.org/manual/5.1/manual.html#lua_tointeger

[3]: https://www.lua.org/manual/5.3/manual.html#8.1
@gucci-on-fleek gucci-on-fleek requested a review from a team as a code owner June 23, 2025 11:06
@collectd-bot collectd-bot added the Fix A pull request fixing a bug label Jun 23, 2025
gucci-on-fleek added a commit to gucci-on-fleek/openwrt-packages that referenced this pull request Jun 24, 2025
Currently, if you set a value larger than 2^31-1 from Lua on a 32-bit
platform, collectd will truncate the value to 0, as this is what
`lua_tointeger` returns on Lua 5.1 with a 32-bit `lua_Integer`. This
commit modifies the `collectd-mod-lua` plugin to use `lua_tonumber` to
first get the value as a float, and then cast it to the appropriate (>32
bit) integer type, ensuring that large values are not truncated.

See also:
- collectd/collectd#4369
- mstojek/nlbw2collectd#11

Signed-off-by: Max Chernoff <git@maxchernoff.ca>
gucci-on-fleek added a commit to gucci-on-fleek/openwrt-packages that referenced this pull request Jun 24, 2025
Currently, if you set a value larger than 2^31-1 from Lua on a 32-bit
platform, collectd will truncate the value to 0, as this is what
`lua_tointeger` returns on Lua 5.1 with a 32-bit `lua_Integer`. This
commit modifies the `collectd-mod-lua` plugin to use `lua_tonumber` to
first get the value as a float, and then cast it to the appropriate (>32
bit) integer type, ensuring that large values are not truncated.

See also:
- collectd/collectd#4369
- mstojek/nlbw2collectd#11

Signed-off-by: Max Chernoff <git@maxchernoff.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix A pull request fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0