Skip to content

Commit

Permalink
[fix] extractProtocolMessage, was loosely testing the result from pro…
Browse files Browse the repository at this point in the history
…toLen, which returned -1. In such a case, it was possible to attempt processing a frame that wasn't complete. This only happened during connect. (#201)
  • Loading branch information
aricart authored Sep 27, 2021
1 parent a10845e commit df44a49
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions nats-base-client/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export function protoLen(ba: Uint8Array): number {
return n + 1;
}
}
return -1;
return 0;
}

export function extractProtocolMessage(a: Uint8Array): string {
// protocol messages are ascii, so Uint8Array
const len = protoLen(a);
if (len) {
if (len > 0) {
const ba = new Uint8Array(a);
const out = ba.slice(0, len);
return TD.decode(out);
Expand Down
2 changes: 1 addition & 1 deletion src/deno_transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class DenoTransport implements Transport {
inbound.fill(frame);
const raw = inbound.peek();
pm = extractProtocolMessage(raw);
if (pm) {
if (pm !== "") {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ Deno.test("protocol - cancel unknown sub", () => {
});

Deno.test("protocol - protolen -1 on empty", () => {
assertEquals(protoLen(Empty), -1);
assertEquals(protoLen(Empty), 0);
assertEquals(extractProtocolMessage(Empty), "");
});

0 comments on commit df44a49

Please sign in to comment.