Skip to content

Commit 30aa0f8

Browse files
Deomid Ryabkovcesantabot
Deomid Ryabkov
authored andcommitted
Make cs_varint_decode only return valid data
PUBLISHED_FROM=726d015857ed29cec877e5bc82041f75db3e02bf
1 parent 8c2428f commit 30aa0f8

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

common/cs_varint.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,26 @@ size_t cs_varint_encode(uint64_t num, uint8_t *buf, size_t buf_size) {
3232
bool cs_varint_decode(const uint8_t *buf, size_t buf_size, uint64_t *num,
3333
size_t *llen) {
3434
size_t i = 0, shift = 0;
35-
*num = 0;
35+
uint64_t n = 0;
3636

3737
do {
3838
if (i == buf_size || i == (8 * sizeof(*num) / 7 + 1)) return false;
3939
/*
4040
* Each byte of varint contains 7 bits, in little endian order.
4141
* MSB is a continuation bit: it tells whether next byte is used.
4242
*/
43-
*num |= ((uint64_t)(buf[i] & 0x7f)) << shift;
43+
n |= ((uint64_t)(buf[i] & 0x7f)) << shift;
4444
/*
4545
* First we increment i, then check whether it is within boundary and
4646
* whether decoded byte had continuation bit set.
4747
*/
48-
*llen = ++i;
48+
i++;
4949
shift += 7;
5050
} while (shift < sizeof(uint64_t) * 8 && (buf[i - 1] & 0x80));
5151

52+
*num = n;
53+
*llen = i;
54+
5255
return true;
5356
}
5457

mjs.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -4877,23 +4877,26 @@ size_t cs_varint_encode(uint64_t num, uint8_t *buf, size_t buf_size) {
48774877
bool cs_varint_decode(const uint8_t *buf, size_t buf_size, uint64_t *num,
48784878
size_t *llen) {
48794879
size_t i = 0, shift = 0;
4880-
*num = 0;
4880+
uint64_t n = 0;
48814881

48824882
do {
48834883
if (i == buf_size || i == (8 * sizeof(*num) / 7 + 1)) return false;
48844884
/*
48854885
* Each byte of varint contains 7 bits, in little endian order.
48864886
* MSB is a continuation bit: it tells whether next byte is used.
48874887
*/
4888-
*num |= ((uint64_t)(buf[i] & 0x7f)) << shift;
4888+
n |= ((uint64_t)(buf[i] & 0x7f)) << shift;
48894889
/*
48904890
* First we increment i, then check whether it is within boundary and
48914891
* whether decoded byte had continuation bit set.
48924892
*/
4893-
*llen = ++i;
4893+
i++;
48944894
shift += 7;
48954895
} while (shift < sizeof(uint64_t) * 8 && (buf[i - 1] & 0x80));
48964896

4897+
*num = n;
4898+
*llen = i;
4899+
48974900
return true;
48984901
}
48994902

0 commit comments

Comments
 (0)