Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJS-223] Metadata column name gets sporadic corrupted #215
  • Loading branch information
rusher committed Oct 5, 2022
1 parent cb0b7c4 commit 6bdb463
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
66 changes: 49 additions & 17 deletions lib/cmd/column-definition.js
Expand Up @@ -65,27 +65,47 @@ class ColumnDef {
}

db() {
return this.#stringParser.packet.readString(this.#stringParser.dbOffset, this.#stringParser.dbLength);
return this.#stringParser.readString(
this.#stringParser.buf,
this.#stringParser.dbOffset,
this.#stringParser.dbLength
);
}

schema() {
return this.#stringParser.packet.readString(this.#stringParser.dbOffset, this.#stringParser.dbLength);
return this.#stringParser.readString(
this.#stringParser.buf,
this.#stringParser.dbOffset,
this.#stringParser.dbLength
);
}

table() {
return this.#stringParser.packet.readString(this.#stringParser.tableOffset, this.#stringParser.tableLength);
return this.#stringParser.readString(
this.#stringParser.buf,
this.#stringParser.tableOffset,
this.#stringParser.tableLength
);
}

orgTable() {
return this.#stringParser.packet.readString(this.#stringParser.orgTableOffset, this.#stringParser.orgTableLength);
return this.#stringParser.readString(
this.#stringParser.buf,
this.#stringParser.orgTableOffset,
this.#stringParser.orgTableLength
);
}

name() {
return this.#stringParser.name();
}

orgName() {
return this.#stringParser.packet.readString(this.#stringParser.orgNameOffset, this.#stringParser.orgNameLength);
return this.#stringParser.readString(
this.#stringParser.buf,
this.#stringParser.orgNameOffset,
this.#stringParser.orgNameLength
);
}

signed() {
Expand All @@ -103,32 +123,39 @@ class ColumnDef {
*/
class StringParser {
constructor(packet) {
const initPos = packet.pos;
packet.skip(4); // skip 'def'

this.dbLength = packet.readUnsignedLength();
this.dbOffset = packet.pos;
this.dbOffset = packet.pos - initPos;
packet.skip(this.dbLength);

this.tableLength = packet.readUnsignedLength();
this.tableOffset = packet.pos;
this.tableOffset = packet.pos - initPos;
packet.skip(this.tableLength);

this.orgTableLength = packet.readUnsignedLength();
this.orgTableOffset = packet.pos;
this.orgTableOffset = packet.pos - initPos;
packet.skip(this.orgTableLength);

this.nameLength = packet.readUnsignedLength();
this.nameOffset = packet.pos;
this.nameOffset = packet.pos - initPos;
packet.skip(this.nameLength);

this.orgNameLength = packet.readUnsignedLength();
this.orgNameOffset = packet.pos;
this.orgNameOffset = packet.pos - initPos;
packet.skip(this.orgNameLength);
this.packet = packet;

//copy buffer since cannot rely on socket onData chunk buffer that can be reused
const saveBuf = Buffer.alloc(packet.end - initPos);
packet.buf.copy(saveBuf, 0, initPos, packet.end);

this.buf = saveBuf;
this.readString = packet.readString;
}

name = function () {
return this.packet.readString(this.nameOffset, this.nameLength);
return this.readString(this.buf, this.nameOffset, this.nameLength);
};
}

Expand All @@ -138,25 +165,30 @@ class StringParser {
*/
class StringParserWithName {
constructor(packet) {
const initPos = packet.pos;
packet.skip(4); // skip 'def'
this.dbLength = packet.readUnsignedLength();
this.dbOffset = packet.pos;
this.dbOffset = packet.pos - initPos;
packet.skip(this.dbLength);
this.tableLength = packet.readUnsignedLength();
this.tableOffset = packet.pos;
this.tableOffset = packet.pos - initPos;
packet.skip(this.tableLength);

this.orgTableLength = packet.readUnsignedLength();
this.orgTableOffset = packet.pos;
this.orgTableOffset = packet.pos - initPos;
packet.skip(this.orgTableLength);

this.colName = packet.readStringLengthEncoded();

this.orgNameLength = packet.readUnsignedLength();
this.orgNameOffset = packet.pos;
this.orgNameOffset = packet.pos - initPos;
packet.skip(this.orgNameLength);

this.packet = packet;
//copy buffer since cannot rely on socket onData chunk buffer that can be reused
const saveBuf = Buffer.alloc(packet.end - initPos);
packet.buf.copy(saveBuf, 0, initPos, packet.end);
this.buf = saveBuf;
this.readString = packet.readString;
}

name = function () {
Expand Down
4 changes: 2 additions & 2 deletions lib/io/packet-node-encoded.js
Expand Up @@ -22,8 +22,8 @@ class PacketNodeEncoded extends Packet {
return this.buf.toString(this.encoding, this.pos - len, this.pos);
}

readString(beg, len) {
return this.buf.toString(this.encoding, beg, beg + len);
readString(buf, beg, len) {
return buf.toString(this.encoding, beg, beg + len);
}

subPacketLengthEncoded() {
Expand Down
4 changes: 2 additions & 2 deletions lib/io/packet-node-iconv.js
Expand Up @@ -21,8 +21,8 @@ class PacketIconvEncoded extends Packet {
return Iconv.decode(this.buf.slice(this.pos - len, this.pos), this.encoding);
}

readString(beg, len) {
return Iconv.decode(this.buf.slice(beg, beg + len), this.encoding);
readString(buf, beg, len) {
return Iconv.decode(buf.slice(beg, beg + len), this.encoding);
}

subPacketLengthEncoded() {
Expand Down

0 comments on commit 6bdb463

Please sign in to comment.