Skip to content

Commit 95d997f

Browse files
committed
remove redundant buffer overflow checks
1 parent d873996 commit 95d997f

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

src/buffer/base.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ abstract class SenderBufferBase implements SenderBuffer {
8080
);
8181
}
8282
this.bufferSize = bufferSize;
83-
// Allocating an extra byte because Buffer.write() does not fail if the length of the data to be written is
84-
// longer than the size of the buffer. It simply just writes whatever it can, and returns.
85-
// If we can write into the extra byte, that indicates buffer overflow.
86-
// See the check in the write() function.
87-
const newBuffer = Buffer.alloc(this.bufferSize + 1, 0);
83+
const newBuffer = Buffer.alloc(this.bufferSize, 0);
8884
if (this.buffer) {
8985
this.buffer.copy(newBuffer);
9086
}
@@ -378,32 +374,14 @@ abstract class SenderBufferBase implements SenderBuffer {
378374

379375
protected write(data: string) {
380376
this.position += this.buffer.write(data, this.position);
381-
if (this.position > this.bufferSize) {
382-
// should never happen, if checkCapacity() is correctly used
383-
throw new Error(
384-
`Buffer overflow [position=${this.position}, bufferSize=${this.bufferSize}]`,
385-
);
386-
}
387377
}
388378

389379
protected writeByte(data: number) {
390380
this.position = this.buffer.writeInt8(data, this.position);
391-
if (this.position > this.bufferSize) {
392-
// should never happen, if checkCapacity() is correctly used
393-
throw new Error(
394-
`Buffer overflow [position=${this.position}, bufferSize=${this.bufferSize}]`,
395-
);
396-
}
397381
}
398382

399383
protected writeDouble(data: number) {
400384
this.position = this.buffer.writeDoubleLE(data, this.position);
401-
if (this.position > this.bufferSize) {
402-
// should never happen, if checkCapacity() is correctly used
403-
throw new Error(
404-
`Buffer overflow [position=${this.position}, bufferSize=${this.bufferSize}]`,
405-
);
406-
}
407385
}
408386

409387
private writeEscaped(data: string, quoted = false) {

test/sender.buffer.test.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ describe("Sender message builder test suite (anything not covered in client inte
732732
await sender.close();
733733
});
734734

735-
it("extends the size of the buffer if data does not fit", async function () {
735+
it("extends the size of the buffer v1 if data does not fit", async function () {
736736
const sender = new Sender({
737737
protocol: "tcp",
738738
protocol_version: "1",
@@ -767,6 +767,39 @@ describe("Sender message builder test suite (anything not covered in client inte
767767
await sender.close();
768768
});
769769

770+
it("extends the size of the buffer v2 if data does not fit", async function () {
771+
const sender = new Sender({
772+
protocol: "tcp",
773+
protocol_version: "2",
774+
host: "host",
775+
init_buf_size: 8,
776+
});
777+
expect(bufferSize(sender)).toBe(8);
778+
expect(bufferPosition(sender)).toBe(0);
779+
sender.table("tableName");
780+
expect(bufferSize(sender)).toBe(24);
781+
expect(bufferPosition(sender)).toBe("tableName".length);
782+
sender.floatColumn("floatField", 123.456);
783+
expect(bufferSize(sender)).toBe(48);
784+
expect(bufferPosition(sender)).toBe("tableName floatField=".length + 10);
785+
sender.stringColumn("strField", "hoho");
786+
expect(bufferSize(sender)).toBe(96);
787+
expect(bufferPosition(sender)).toBe(
788+
"tableName floatField=".length + 10 + ',strField="hoho"'.length,
789+
);
790+
await sender.atNow();
791+
expect(bufferSize(sender)).toBe(96);
792+
expect(bufferPosition(sender)).toBe(
793+
"tableName floatField=".length + 10 + ',strField="hoho"\n'.length,
794+
);
795+
expect(bufferContentHex(sender)).toBe(
796+
toHex("tableName floatField=") +
797+
" 3d 10 77 be 9f 1a 2f dd 5e 40 " +
798+
toHex(',strField="hoho"\n'),
799+
);
800+
await sender.close();
801+
});
802+
770803
it("throws exception if tries to extend the size of the buffer above max buffer size", async function () {
771804
const sender = await Sender.fromConfig(
772805
"tcp::addr=host;init_buf_size=8;max_buf_size=64;",
@@ -838,6 +871,21 @@ function bufferContent(sender: Sender) {
838871
return sender.buffer.toBufferView().toString();
839872
}
840873

874+
function bufferContentHex(sender: Sender) {
875+
// @ts-expect-error - Accessing private field
876+
return toHexString(sender.buffer.toBufferView());
877+
}
878+
879+
function toHex(str: string) {
880+
return toHexString(Buffer.from(str));
881+
}
882+
883+
function toHexString(buffer: Buffer) {
884+
return Array.from(buffer)
885+
.map((b) => b.toString(16).padStart(2, "0"))
886+
.join(" ");
887+
}
888+
841889
function bufferSize(sender: Sender) {
842890
// @ts-expect-error - Accessing private field
843891
return sender.buffer.bufferSize;

0 commit comments

Comments
 (0)