HeapsIO / hxbit

Haxe Binary serialization and network synchronization library
155 stars 30 forks source link

Hash is not computed correctly in some environments #48

Closed kyubuns closed 4 years ago

kyubuns commented 4 years ago

Sample.hx

class Sample {
    static function main() {
        var serializer = new hxbit.Serializer();
        var serializedA = serializer.serialize(new AElement());
        trace(serializedA.toHex());
    }
}

class Element implements hxbit.Serializable {
}

class AElement extends Element {
    public function new() {}
}

lua.hxml

-main Sample
-lib hxbit
-lua sample.lua
-D lua-vanilla
-cmd lua sample.lua

result

lua: Conflicting CLID between Element and AElement
stack traceback:
    [C]: in function 'error'
    sample.lua:640: in field 'initClassIDS'
    sample.lua:535: in field 'super'
    sample.lua:529: in field 'new'
    sample.lua:515: in field 'main'
    sample.lua:6708: in main chunk
    [C]: in ?
Error: Command failed with error 1

This is a bug that occurs because the result of Std.int is unspecified, when argument is outside of the signed int 32 range. https://github.com/HeapsIO/hxbit/blob/c8af5cdbd7c867f9a0bb144aa54a0d2f7f27643e/hxbit/Serializer.hx#L52-L60


I looked into it further.

class Sample {
    static public function main(): Void {
        var v = 14337447;
        var v1 = Std.int(v * 223 + 101);
        var v2 = Std.int(v1 * 223 + 101);
        trace(v1);
        trace(v2);
    }
}
+ haxe -main Sample --interp
Sample.hx:6: -1097716514
Sample.hx:7: 22353351

+ haxe -main Sample -js sample.js
+ node sample.js
Sample.hx:6: -1097716514
Sample.hx:7: 22353351

+ haxe -main Sample -cs sample_cs
+ mono sample_cs/bin/Sample.exe
Sample.hx:6: -1097716514
Sample.hx:7: 22353351

+ haxe -main Sample -lua sample.lua
+ lua sample.lua
Sample.hx:6: -1097716514
Sample.hx:7: 22353351

+ haxe -main Sample -lua v_sample.lua -D lua-vanilla
+ lua v_sample.lua
Sample.hx:6: 2147483647
Sample.hx:7: 2147483647

lua-vanilla returns same value, when over 2147483647. Do you have a plan?

kyubuns commented 4 years ago

Sorry, this problem is fixed in haxe 4.1.1.

(I thought it was undefined because it said undefined in the documentation, but apparently it's unified? I don't know...)