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

encoding/ascii85.ts: encode() returns a wrong result with a 4-byte subarray #3223

Closed
dahlia opened this issue Feb 28, 2023 · 1 comment · Fixed by #3310
Closed

encoding/ascii85.ts: encode() returns a wrong result with a 4-byte subarray #3223

dahlia opened this issue Feb 28, 2023 · 1 comment · Fixed by #3310
Labels
bug Something isn't working needs triage

Comments

@dahlia
Copy link

dahlia commented Feb 28, 2023

Describe the bug

The encode() function from encoding/ascii.ts returns a wrong result with a subarray. More specifically, if a subarray is a 4-byte (or multiples of 4 bytes) slice of a buffer longer than it, and the slice refers to a non-zero offset in the buffer, the function encodes the first 4 bytes of the buffer instead of the subarray bytes.

Steps to Reproduce

import { encode } from "https://deno.land/std@0.178.0/encoding/ascii85.ts";
import { assertEquals } from "https://deno.land/std@0.178.0/testing/asserts.ts";

Deno.test("base85 encoding", () => {
  const data = new Uint8Array([0x73, 0x70, 0x61, 0x6d]);
  const encoded = encode(data);
  assertEquals(encoded, "F)YQ)");
});

Deno.test("base85 encoding with a 4-byte subarray", () => {
  const data = new Uint8Array(
    [0x01, 0x02, 0x03, 0x04, 0x73, 0x70, 0x61, 0x6d],
  );
  const encoded = encode(data.subarray(4));

  // The following assertion fails; it returns "!<N?+" instead, which encodes
  // the first 4 bytes of the array: [0x01, 0x02, 0x03, 0x04].
  assertEquals(encoded, "F)YQ)");
});

Expected behavior

The below test should pass, but it does not.

Environment

  • OS: Ubuntu 22.04 & Windows 11 Pro
  • Deno version: 1.30.3
  • std version: 0.178.0
@dahlia dahlia added bug Something isn't working needs triage labels Feb 28, 2023
dahlia added a commit to planetarium/bencodex.js that referenced this issue Feb 28, 2023
@kamilogorek
Copy link
Contributor

kamilogorek commented Mar 1, 2023

Probably the same issue that I've fixed here - #3208

https://github.com/denoland/deno_std/blob/69fa1e956c2469f168756d757b1653323327ed53/encoding/ascii85.ts#L78-L79

Should be trivial to port the patch there with the appropriate test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants