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

Feature Request: Allow reading into a 0-length array #3287

Closed
qexk opened this issue Nov 7, 2019 · 1 comment · Fixed by #3329
Closed

Feature Request: Allow reading into a 0-length array #3287

qexk opened this issue Nov 7, 2019 · 1 comment · Fixed by #3329

Comments

@qexk
Copy link
Contributor

qexk commented Nov 7, 2019

Hello! I got this error while working with sockets implementing a low-level protocol:

error: Uncaught InvalidInput: no buffer specified
► $deno$/dispatch_minimal.ts:64:11
    at DenoError ($deno$/errors.ts:20:5)
    at unwrapResponse ($deno$/dispatch_minimal.ts:64:11)
    at sendAsyncMinimal ($deno$/dispatch_minimal.ts:106:10)

Turns out core’s implementation of Deno.Conn.read seems to have undefined behaviour when the buffer has no size.
Here’s an example to reproduce the error:

const PORT = 62394;

const server = new Promise(async resolve => {
  const listener = Deno.listen({ hostname: "127.0.0.1", port: PORT });
  const sock = await listener.accept();
  const rdbuf = new Uint8Array(12);
  await sock.read(rdbuf);
  console.log("SERVER", rdbuf);
  await sock.write(rdbuf);
  resolve();
});

const client = new Promise(async resolve => {
  await new Promise(resolve => setTimeout(resolve, 500));
  const sock = await Deno.dial({ hostname: "127.0.0.1", port: PORT });
  await sock.write(new Uint8Array(12).fill(12));
  const rdbuf = new Uint8Array(0);
  await sock.read(rdbuf);
  sock.close();
  resolve();
});

Giving a size other than 0 to the client’s rdbuf makes this piece of code work as expected.

Some solutions:

  • Make it explicit in the JSDoc that p.length must be greater than 0.
  • Mimic POSIX read behaviour: In the absence of any errors, or if read() does not check for errors, a read() with a count of 0 returns zero and has no other effects. (from the manpage). This requires adding a check to the following function (and readSync), and writing tests.

    deno/cli/js/files.ts

    Lines 76 to 85 in 6c5a981

    export async function read(rid: number, p: Uint8Array): Promise<number | EOF> {
    const nread = await sendAsyncMinimal(dispatch.OP_READ, rid, p);
    if (nread < 0) {
    throw new Error("read error");
    } else if (nread == 0) {
    return EOF;
    } else {
    return nread;
    }
    }

If you’re ok with the second solution, I can do the PR :)
I’m using Deno v0.22.0.

@zekth
Copy link
Contributor

zekth commented Nov 7, 2019

Second solution looks like a good one for me

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

Successfully merging a pull request may close this issue.

2 participants