Skip to content

Check for broken implementation of Uint8Array.prototype.toHex #1439

Description

@brc-dd

Not sure if v8 staging features are covered, but when using deno run --v8-flags=--js-base-64 or node --js-base-64 or chrome://flags/#enable-javascript-harmony, the following code gives wrong output:

// foo.ts, run with:
// deno run --v8-flags=--js-base-64 foo.ts

import 'https://deno.land/x/corejs@v3.43.0/index.js'

console.log(new Uint8Array(8).fill(255).toHex()) // gives VfVfVfVfVfVfVfVf

Solution can be adding a check similar to:

var INCORRECT_BEHAVIOR_OR_DOESNT_EXISTS = !Uint8Array || !Uint8Array.prototype.setFromBase64 || !(function () {
var target = new Uint8Array([255, 255, 255, 255, 255]);
try {
target.setFromBase64('MjYyZg===');
} catch (error) {
return target[0] === 50 && target[1] === 54 && target[2] === 50 && target[3] === 255 && target[4] === 255;
}
})();

From denoland/deno#25051 (comment)

Activity

  1. zloirock commented on Jun 28, 2025

    @zloirock
    Owner

    Thanks for the note. Feel free to add a PR -)

  2. brc-dd commented on Jun 29, 2025

    @brc-dd
    ContributorAuthor

    Created #1440.

    There are other issues as well, but not sure if they need fixing.


      assert.throws(() => fromBase64('SGVsbG8gV29ybGQ1=', null), TypeError, 'incorrect options argument #1');
      assert.throws(() => fromBase64('SGVsbG8gV29ybGQ1=', 1), TypeError, 'incorrect options argument #2');

    ^in v8 implementation, these throw:

    Uncaught SyntaxError: Found a character that cannot be part of a valid base64 string.
    

    The SyntaxError itself is correct (because of wrong padding), but seems like v8 ignores malformed options.


      assert.throws(() => array.toBase64(null), TypeError, 'incorrect options argument #1');
      assert.throws(() => array.toBase64(1), TypeError, 'incorrect options argument #2');

    ^these don't throw anything.


    I'll probably report these on the Chromium issue.

  3. zloirock commented on Jun 29, 2025

    @zloirock
    Owner

    @brc-dd I think that those cases also should be fixed.

  4. brc-dd commented on Jun 29, 2025

    @brc-dd
    ContributorAuthor

    In those cases should we replace the whole implementation (like similar to above PR) or is it possible to validate the options and then call the builtin functions?

  5. zloirock commented on Jun 29, 2025

    @zloirock
    Owner

    Since it's flagged, I think it's fine just to replace them - I hope that it will be fixed when it will be shipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions