Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] blit may throw "source too large" exception if length is changed #5474

Closed
vroad opened this issue Jul 20, 2016 · 9 comments
Closed

[js] blit may throw "source too large" exception if length is changed #5474

vroad opened this issue Jul 20, 2016 · 9 comments

Comments

@vroad
Copy link
Contributor

vroad commented Jul 20, 2016

On js, specifying length in constructor is not allowed, but subclasses of haxe.io.Bytes could set this to a different value. OpenFL ByteArray sometimes stops working with this exception on js. Probably blit() should use src.b.byteLength (or src.b.length?) to get consistent behavior in this case.

@vroad vroad changed the title You may get "source too large" error when blitting Bytes on js if you set shorter length than underlying Uint8Array in a subclass [js] You may get "source too large" error when blitting Bytes on js if you set shorter length than underlying Uint8Array in a subclass Jul 21, 2016
@ncannasse
Copy link
Member

I tried reading several times but still couldn't get it: could you post some actual code?

Also, your issue title is way too long :-)

@vroad
Copy link
Contributor Author

vroad commented Jul 22, 2016

I'll try creating some test code.

@vroad vroad changed the title [js] You may get "source too large" error when blitting Bytes on js if you set shorter length than underlying Uint8Array in a subclass [js] blit may throw "source too large" exception if length is changed Jul 22, 2016
@vroad
Copy link
Contributor Author

vroad commented Jul 22, 2016

I tried creating test code from OpenFL ByteArray impl. This throws "Source too large" error only on js, but not on other targets such as cpp because b.byteLength and length no longer match in Bytes.

import haxe.io.Bytes;
import haxe.io.BytesData;

class ExtBytes extends Bytes {

    private var __length:Int;

    public function new(length:Int)
    {
        var bytes:Bytes = Bytes.alloc (length);
        #if js
        super(bytes.b.buffer);
        #else
        super(length, bytes.b);
        #end
        __length = length;
    }

    public function clear ():Void {

        length = 0;

    }

    public function __resize (size:Int) {

        if (size > __length) {

            var bytes = Bytes.alloc ((((size + 1) * 3) >> 1));
            var cacheLength = length;
            length = __length;
            bytes.blit (0, this, 0, __length);
            length = cacheLength;
            __setData (bytes);

        }

        if (length < size) {

            length = size;

        }

    }

    private inline function __setData (bytes:Bytes):Void {

        b = bytes.b;
        __length = bytes.length;

        #if js
        data = bytes.data;
        #end

    }
}

class Test {
    static function main() {
        var src:ExtBytes = new ExtBytes(50);
        src.clear();
        src.__resize(25);
        var dst:ExtBytes = new ExtBytes(20);
        dst.clear();
        dst.__resize(25);
        dst.blit(0, src, 0, 25);
    }
}

http://try.haxe.org/#F94C9

@vroad
Copy link
Contributor Author

vroad commented Jul 22, 2016

I fixed this by checking underlying Uint8Array's byteLength instead of Bytes.length. Should I make pull request instead?

vroad@4eddb4d

@tbilou
Copy link

tbilou commented Aug 3, 2016

We're having the same issue.
Tried the "fix" locally and it solved the problem.
@ncannasse how should we proceed here? pull request?

@ncannasse
Copy link
Member

@tbilou yes a PR would work, thanks

@tbilou
Copy link

tbilou commented Aug 4, 2016

@vroad can you create the pull request?

@vroad
Copy link
Contributor Author

vroad commented Aug 4, 2016

@tbilou Created a PR. #5499

@ncannasse
Copy link
Member

Closed by 8c03ac1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants