summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbinaryta <naritatakuya0000@gmail.com>2018-12-10 05:38:30 +0900
committerRyan Dahl <ry@tinyclouds.org>2018-12-09 15:38:30 -0500
commitc427c2df427f477eb1214d8ff3fdfad36e191a6c (patch)
treef14068269fba8e495b5d5f5bb289257792a04255
parentf2447f6a2307369c16322861735a87fdd30f7233 (diff)
Add TooLarge error code for buffers (#1298)
In collaboration with @yushimatenjin
-rw-r--r--js/buffer.ts6
-rw-r--r--js/buffer_test.ts21
-rw-r--r--src/msg.fbs1
3 files changed, 26 insertions, 2 deletions
diff --git a/js/buffer.ts b/js/buffer.ts
index 4e9cd6eb2..9e06eecd4 100644
--- a/js/buffer.ts
+++ b/js/buffer.ts
@@ -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
@@ -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);
diff --git a/js/buffer_test.ts b/js/buffer_test.ts
index 58668de26..2683b3862 100644
--- a/js/buffer_test.ts
+++ b/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;
@@ -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();
diff --git a/src/msg.fbs b/src/msg.fbs
index d6bbc220e..ecfb9e50c 100644
--- a/src/msg.fbs
+++ b/src/msg.fbs
@@ -105,6 +105,7 @@ enum ErrorKind: byte {
HttpCanceled,
HttpParse,
HttpOther,
+ TooLarge,
}
table Cwd {}