Commit 71170f6
committed
fix(encoding/varint): throw on buffer overflow instead of truncating (#7147)
`encodeVarint` walked the loop with `i <= Math.min(buf.length,
MaxVarintLen64)`. For a value that needed all 10 uint64 varint bytes
plus a continuation (e.g. `0x1234567891234567891n`), the 11th iteration
wrote to `buf[10]` on the default 10-byte buffer — silently dropped by
`Uint8Array` — and the function returned `[Uint8Array(10), 11]`, a
slice shorter than the reported byte count. Callers then handed that
truncated buffer to `decodeVarint` and got a wrong number back.
Cap the loop at `min(buf.length - offset, MaxVarintLen64)`, write
through `offset + i` so the offset path can't drift either, and split
the post-loop throw into two messages: "buffer holds at most N
byte(s) after offset" when the user supplied (or defaulted to) a short
buffer, and the existing "overflows uint64" when the value genuinely
exceeds the protocol limit. The success path returns identical
results to before.
Three new tests in `encoding/varint_test.ts`: the issue's exact 76-bit
value (must throw "overflows uint64"), a 3-byte value into a 2-byte
buffer (must throw "buffer holds at most 2 bytes"), and offset == buf
length (must throw "buffer holds at most 0 bytes"). Full suite stays
green: 18 passed.1 parent f0c9f14 commit 71170f6
2 files changed
Lines changed: 67 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
224 | | - | |
225 | | - | |
| 224 | + | |
| 225 | + | |
226 | 226 | | |
227 | | - | |
| 227 | + | |
228 | 228 | | |
229 | 229 | | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
239 | 244 | | |
240 | | - | |
241 | | - | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
242 | 254 | | |
243 | 255 | | |
244 | | - | |
| 256 | + | |
245 | 257 | | |
246 | 258 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
123 | 163 | | |
124 | 164 | | |
125 | 165 | | |
| |||
0 commit comments