Skip to content

Commit

Permalink
read dataTypeID and tableID as unsigned uint (#3347)
Browse files Browse the repository at this point in the history
* read dataTypeID and tableID as unsigned uint

this is causing issues in other projects, like sequelize/sequelize#15466

* added tests for oids larger than 2^31
  • Loading branch information
alexDevBR authored Jan 13, 2025
1 parent 373093d commit 9fbcf17
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/pg-protocol/src/buffer-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export class BufferReader {
return result
}

public uint32(): number {
const result = this.buffer.readUInt32BE(this.offset)
this.offset += 4
return result
}

public string(length: number): string {
const result = this.buffer.toString(this.encoding, this.offset, this.offset + length)
this.offset += length
Expand Down
28 changes: 28 additions & 0 deletions packages/pg-protocol/src/inbound-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ var twoRowBuf = buffers.rowDescription([
},
])

var rowWithBigOids = {
name: 'bigoid',
tableID: 3000000001,
attributeNumber: 2,
dataTypeID: 3000000003,
dataTypeSize: 4,
typeModifier: 5,
formatCode: 0,
}
var bigOidDescBuff = buffers.rowDescription([rowWithBigOids])

var emptyRowFieldBuf = new BufferList().addInt16(0).join(true, 'D')

var emptyRowFieldBuf = buffers.dataRow([])
Expand Down Expand Up @@ -132,6 +143,22 @@ var expectedTwoRowMessage = {
},
],
}
var expectedBigOidMessage = {
name: 'rowDescription',
length: 31,
fieldCount: 1,
fields: [
{
name: 'bigoid',
tableID: 3000000001,
columnID: 2,
dataTypeID: 3000000003,
dataTypeSize: 4,
dataTypeModifier: 5,
format: 'text',
},
],
}

var emptyParameterDescriptionBuffer = new BufferList()
.addInt16(0) // number of parameters
Expand Down Expand Up @@ -261,6 +288,7 @@ describe('PgPacketStream', function () {
testForMessage(emptyRowDescriptionBuffer, expectedEmptyRowDescriptionMessage)
testForMessage(oneRowDescBuff, expectedOneRowMessage)
testForMessage(twoRowBuf, expectedTwoRowMessage)
testForMessage(bigOidDescBuff, expectedBigOidMessage)
})

describe('parameterDescription messages', function () {
Expand Down
4 changes: 2 additions & 2 deletions packages/pg-protocol/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ export class Parser {

private parseField(): Field {
const name = this.reader.cstring()
const tableID = this.reader.int32()
const tableID = this.reader.uint32()
const columnID = this.reader.int16()
const dataTypeID = this.reader.int32()
const dataTypeID = this.reader.uint32()
const dataTypeSize = this.reader.int16()
const dataTypeModifier = this.reader.int32()
const mode = this.reader.int16() === 0 ? 'text' : 'binary'
Expand Down

0 comments on commit 9fbcf17

Please sign in to comment.