Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround for crazy string numeric comparison in Qt QML

Tags:

javascript

qt

qml

Here is crazy string to numeric comparison in Qt (LTS 5.6.2) QML JavaScript implementaion:

console.log("240000000000" == "3776798720");
console.log("240000000000" === "3776798720");
console.log("240000000000" === "3776798721");

And the output is:

true
true
false

It looks like string interpreted as (u)int32 and high byte lost:

240000000000 == 0x37E11D6000
3776798720   ==   0xE11D6000

This bug also effects on objects:

var g = {};
var h = "240000000000";
g[h] = h + "BUG";
console.log(JSON.stringify(g, null, 2));
console.log(g["3776798720"], g["240000000000"]);

Output:

qml: {
    "3776798720": "240000000000BUG"
}
qml: 240000000000BUG 240000000000BUG

As you can see the key is damaged. The value can be obtained by two different strings.

Question

Is there any option to get some workaround with this without patching Qt? Or at least the approximate location where can be the problem in Qt to improve yourself?

p.s. Also here is a QTBUG-56830 reported by my coworker.

like image 514
Nick Bondarenko Avatar asked Nov 01 '16 10:11

Nick Bondarenko


2 Answers

I can't see a workaround, so I've made a fix: apply to qtdeclarative the patch that I've posted here

https://codereview.qt-project.org/#/c/175782

And recompile it.

like image 165
peppe Avatar answered Nov 14 '22 11:11

peppe


Here's workaround that seems to work

console.log(String("3776798720").localeCompare("240000000000") === 0)
console.log(String("3776798721").localeCompare("240000000000") === 0)
console.log(String("240000000000").localeCompare("240000000000") === 0)

Output:

qml: false
qml: false
qml: true

Or if you have string variable

var str = "3776798720"
console.log(str.localeCompare("240000000000") === 0)
like image 2
mip Avatar answered Nov 14 '22 11:11

mip