Nick Bondarenko
Nick Bondarenko

Reputation: 6371

Workaround for crazy string numeric comparison in 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.

Upvotes: 5

Views: 927

Answers (2)

peppe
peppe

Reputation: 22796

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.

Upvotes: 4

mip
mip

Reputation: 8733

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)

Upvotes: 2

Related Questions