Workaround for Unconditional String Comparison in Qt QML

Here is a crazy string for numerical comparison in Qt (LTS 5.6.2) QML JavaScript implementation:

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

And the result:

 true true false 

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

 240000000000 == 0x37E11D6000 3776798720 == 0xE11D6000 

This error also affects 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 in two different lines.

Question

Is there any option to get a workaround with this without fixing Qt? Or at least an approximate location, where a problem in Qt might occur to improve itself?

ps Also here is QTBUG-56830 reported by my colleague.

+5
source share
2 answers

I do not see a workaround, so I made a fix: apply the qtdeclarative patch that I posted here

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

And recompile it.

+4
source

Here's a 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 a string variable

 var str = "3776798720" console.log(str.localeCompare("240000000000") === 0) 
+1
source

Source: https://habr.com/ru/post/1259087/


All Articles