1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

Fix: allow reading into a 0-length array (#3329)

This commit is contained in:
Alexandre Szymocha 2019-12-28 14:48:36 +01:00 committed by Ry Dahl
parent 954a0c64e7
commit 4d4908dde3
3 changed files with 13 additions and 1 deletions

View file

@ -50,6 +50,9 @@ export async function open(
*
*/
export function readSync(rid: number, p: Uint8Array): number | EOF {
if (p.length == 0) {
return 0;
}
const nread = sendSyncMinimal(dispatch.OP_READ, rid, p);
if (nread < 0) {
throw new Error("read error");
@ -70,6 +73,9 @@ export function readSync(rid: number, p: Uint8Array): number | EOF {
* const text = new TextDecoder().decode(buf);
*/
export async function read(rid: number, p: Uint8Array): Promise<number | EOF> {
if (p.length == 0) {
return 0;
}
const nread = await sendAsyncMinimal(dispatch.OP_READ, rid, p);
if (nread < 0) {
throw new Error("read error");

View file

@ -118,6 +118,10 @@ testPerm(
const filename = tempDir + "hello.txt";
const file = await Deno.open(filename, "w+");
// reading into an empty buffer should return 0 immediately
const bytesRead = await file.read(new Uint8Array(0));
assert(bytesRead === 0);
// reading file into null buffer should throw an error
let err;
try {

View file

@ -18,12 +18,14 @@ export enum SeekMode {
// https://golang.org/pkg/io/#Reader
export interface Reader {
/** Reads up to p.byteLength bytes into `p`. It resolves to the number
* of bytes read (`0` < `n` <= `p.byteLength`) and rejects if any error encountered.
* of bytes read (`0` <= `n` <= `p.byteLength`) and rejects if any error encountered.
* Even if `read()` returns `n` < `p.byteLength`, it may use all of `p` as
* scratch space during the call. If some data is available but not
* `p.byteLength` bytes, `read()` conventionally returns what is available
* instead of waiting for more.
*
* When `p.byteLength` == `0`, `read()` returns `0` and has no other effects.
*
* When `read()` encounters end-of-file condition, it returns EOF symbol.
*
* When `read()` encounters an error, it rejects with an error.