mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
Add TooLarge error code for buffers (#1298)
In collaboration with @yushimatenjin
This commit is contained in:
parent
f2447f6a23
commit
c427c2df42
3 changed files with 26 additions and 2 deletions
|
@ -6,6 +6,7 @@
|
||||||
import { Reader, Writer, ReadResult } from "./io";
|
import { Reader, Writer, ReadResult } from "./io";
|
||||||
import { assert } from "./util";
|
import { assert } from "./util";
|
||||||
import { TextDecoder } from "./text_encoding";
|
import { TextDecoder } from "./text_encoding";
|
||||||
|
import { DenoError, ErrorKind } from "./errors";
|
||||||
|
|
||||||
// MIN_READ is the minimum ArrayBuffer size passed to a read call by
|
// 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
|
// 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.
|
// don't spend all our time copying.
|
||||||
copyBytes(this.buf, this.buf.subarray(this.off));
|
copyBytes(this.buf, this.buf.subarray(this.off));
|
||||||
} else if (c > MAX_SIZE - c - n) {
|
} 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 {
|
} else {
|
||||||
// Not enough space anywhere, we need to allocate.
|
// Not enough space anywhere, we need to allocate.
|
||||||
const buf = new Uint8Array(2 * c + n);
|
const buf = new Uint8Array(2 * c + n);
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { Buffer, readAll } from "deno";
|
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
|
// 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.
|
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
|
||||||
// https://github.com/golang/go/blob/master/LICENSE
|
// https://github.com/golang/go/blob/master/LICENSE
|
||||||
import { assert, assertEqual, test } from "./test_util.ts";
|
import { assert, assertEqual, test } from "./test_util.ts";
|
||||||
|
|
||||||
// N controls how many iterations of certain checks are performed.
|
// N controls how many iterations of certain checks are performed.
|
||||||
const N = 100;
|
const N = 100;
|
||||||
let testBytes: Uint8Array | null;
|
let testBytes: Uint8Array | null;
|
||||||
|
@ -130,6 +130,25 @@ test(async function bufferLargeByteWrites() {
|
||||||
check(buf, "");
|
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() {
|
test(async function bufferLargeByteReads() {
|
||||||
init();
|
init();
|
||||||
const buf = new Buffer();
|
const buf = new Buffer();
|
||||||
|
|
|
@ -105,6 +105,7 @@ enum ErrorKind: byte {
|
||||||
HttpCanceled,
|
HttpCanceled,
|
||||||
HttpParse,
|
HttpParse,
|
||||||
HttpOther,
|
HttpOther,
|
||||||
|
TooLarge,
|
||||||
}
|
}
|
||||||
|
|
||||||
table Cwd {}
|
table Cwd {}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue