libubus-lua: Wrong conversion for negative numbers on 64bit systems

Florian Eckert fe at dev.tdt.de
Tue Jan 11 04:02:14 PST 2022


Hello,

The commit [1] added a regression for negative numbers in the ubus lua 
binding on 64bit systems.
This is was also backported to openwrt-19.07 branch with this commit 
[2].

I have noticed this on my Apu3 x86_64 system and I do not see this error 
on my 32 bit system.
I assume that this error occurs with every 64bit system.

This issue can be reproduced in the following way:

LUA Test-Script (lua /root/test.lua):
```
local util = require "luci.util"
local status = util.ubus("luci.test", "dump", {})
luci.util.dumptable(status)
```

LUA Test-Service (lua /root/luci.test):

```
#!/usr/bin/env lua

local ubus = require "ubus"
local uloop = require "uloop"

uloop.init()

local conn = ubus.connect()
if not conn then
         error("Failed to connect to ubus")
end

local name = "luci.test"
local ubus_objects = { }
ubus_objects[name] = {
         dump = {
                 function(req, msg)
                         local result = {}
                         local string = "This is a test"
                         local on = true
                         local off = false
                         local positiv = 50000
                         local negativ = -55

                         result["string"] = string
                         result["on"] = on
                         result["off"] = off
                         result["positiv"] = positiv
                         result["negativ"] = negativ

                         conn:reply(req, result);
                 end, {}
         }
}

conn:add( ubus_objects )
print("Objects added, starting loop")

uloop.run()
```

Incorrect output (lua /root/test.lua):
```
negativ 1.844674407371e+19
string  This is a test
positiv 50000
on      true
off     false

```

Correct output if I revert the commit (lua /root/test.lua):
```
negativ -55
string  This is a test
positiv 50000
on      true
off     false
```

I think the conversion is wrong here.
I think it must be checked beforehand whether it is a negative number, 
otherwise the typecast is wrong here if this is not taken into account.

Can someone please confirm this!


Best Regards

Florian


[1] 
https://git.openwrt.org/?p=project/ubus.git;a=commit;h=171469e3138cce191892e20b6fd35b52c9368064
[2] 
https://git.openwrt.org/?p=project/ubus.git;a=commit;h=9ec9cfc6574a197ea934489de056594f44088352



More information about the openwrt-devel mailing list