fix(proton): accept sign-extended uint32 field values

This commit is contained in:
DrSmoothl committed 2026-07-10 18:20:11 +08:00
1 parent c04864dd21
commit b53004ee23
4 files changed
+139 -4

No files matched your search

+9 -3
View File
@@ -22,6 +22,13 @@ function varintDec64(varName: string, ind: string): string {
].join('\n');
}
function varintDec32Value(varName: string, ind: string): string {
return [
`${ind}const [${varName}, ${varName}_offset] = __readVarint32Value(data, offset, end);`,
`${ind}offset = ${varName}_offset;`,
].join('\n');
}
function isVarint64(typeName: string): boolean {
return typeName === 'uint_64' || typeName === 'int_64' || typeName === 'sint_64';
}
@@ -153,15 +160,14 @@ function decodeField(field: ProtobufField, index: number): string {
} else if (typeName === 'int_32') {
// Protobuf encodes negative int32 values as a sign-extended 10-byte
// varint. Read the full uint64 representation, then keep its signed low
// 32 bits. uint32/sint32/tag/length values must remain on the stricter
// five-byte reader.
// 32 bits.
L.push(varintDec64('_val', I));
L.push(assign(`Number(BigInt.asIntN(32, _val))`));
} else if (typeName === 'sint_32') {
L.push(varintDec('_val', I));
L.push(assign(`(_val >>> 1) ^ -(_val & 1)`));
} else if (wireType === WireType.Varint) {
L.push(varintDec('_val', I));
L.push(varintDec32Value('_val', I));
L.push(assign(`_val >>> 0`));
} else if (typeName === 'float') {
L.push(`${I}if (end - offset < 4) throw new Error('protobuf truncated float field');`);
+7
View File
@@ -117,6 +117,13 @@ function __readVarint64(data, offset, end) {
}
throw new Error('protobuf uint64 varint overflow');
}
function __readVarint32Value(data, offset, end) {
const [value, next] = __readVarint64(data, offset, end);
if (value > 0xffffffffn && value < 0xffffffff80000000n) {
throw new Error('protobuf uint32 field varint overflow');
}
return [Number(BigInt.asUintN(32, value)), next];
}
function __skipVarint(data, offset, end) {
for (let i = 0; i < 10; i++) {
if (offset >= end) throw new Error('protobuf truncated varint');