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

Apply DenoError TooLarge #1298

Merged
merged 3 commits into from Dec 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion js/buffer.ts
Expand Up @@ -6,6 +6,7 @@
import { Reader, Writer, ReadResult } from "./io";
import { assert } from "./util";
import { TextDecoder } from "./text_encoding";
import { DenoError, ErrorKind } from "./errors";

// MIN_READ is the minimum ArrayBuffer size passed to a read call by
// buffer.ReadFrom. As long as the Buffer has at least MIN_READ bytes beyond
Expand Down Expand Up @@ -170,7 +171,10 @@ export class Buffer implements Reader, Writer {
// don't spend all our time copying.
copyBytes(this.buf, this.buf.subarray(this.off));
} else if (c > MAX_SIZE - c - n) {
throw Error("ErrTooLarge"); // TODO DenoError(TooLarge)
throw new DenoError(
ErrorKind.TooLarge,
"The buffer cannot be grown beyond the maximum size."
);
} else {
// Not enough space anywhere, we need to allocate.
const buf = new Uint8Array(2 * c + n);
Expand Down
21 changes: 20 additions & 1 deletion js/buffer_test.ts
@@ -1,9 +1,9 @@
import { Buffer, readAll } from "deno";
import * as deno from "deno";
// This code has been ported almost directly from Go's src/bytes/buffer_test.go
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
// https://github.com/golang/go/blob/master/LICENSE
import { assert, assertEqual, test } from "./test_util.ts";

// N controls how many iterations of certain checks are performed.
const N = 100;
let testBytes: Uint8Array | null;
Expand Down Expand Up @@ -130,6 +130,25 @@ test(async function bufferLargeByteWrites() {
check(buf, "");
});

test(async function bufferTooLargeByteWrites() {
init();
const tmp = new Uint8Array(72);
const growLen = Number.MAX_VALUE;
const xBytes = repeat("x", 0);
const buf = new Buffer(xBytes.buffer as ArrayBuffer);
const { nread, eof } = await buf.read(tmp);

let err;
try {
buf.grow(growLen);
} catch (e) {
err = e;
}

assertEqual(err.kind, deno.ErrorKind.TooLarge);
assertEqual(err.name, "TooLarge");
});

test(async function bufferLargeByteReads() {
init();
const buf = new Buffer();
Expand Down
1 change: 1 addition & 0 deletions src/msg.fbs
Expand Up @@ -105,6 +105,7 @@ enum ErrorKind: byte {
HttpCanceled,
HttpParse,
HttpOther,
TooLarge,
}

table Cwd {}
Expand Down