Skip to content

Commit 39f37ce

Browse files
committed
revert
1 parent f65b752 commit 39f37ce

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

ddtrace/internal/datadog/profiling/profiling_helpers/linetable_parser.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <cstdint>
4+
35
/* Shared line table parsing for profiling.
46
*
57
* Provides allocation-free, GIL-free line number resolution from
@@ -24,20 +26,33 @@ read_varint(const unsigned char* table, Py_ssize_t len, Py_ssize_t* i)
2426
Py_ssize_t guard = len - 1;
2527
if (*i >= guard)
2628
return 0;
27-
int val = table[++*i] & 63;
28-
int shift = 0;
29+
30+
uint32_t val = table[++*i] & 63;
31+
uint32_t shift = 0;
2932
while (*i < guard && table[*i] & 64) {
3033
shift += 6;
31-
val |= (table[++*i] & 63) << shift;
34+
35+
if (shift >= 32) {
36+
// Guard against UB from over-large shift on malformed input;
37+
// advance `i` past remaining continuation bytes to leave it in a
38+
// consistent state, even though callers don't rely on the value of
39+
// i or reuse it
40+
for (; *i < guard && table[*i] & 64; ++*i)
41+
;
42+
43+
return 0;
44+
}
45+
46+
val |= static_cast<uint32_t>(table[++*i] & 63) << shift;
3247
}
33-
return val;
48+
return static_cast<int>(val);
3449
}
3550

3651
inline int
3752
read_signed_varint(const unsigned char* table, Py_ssize_t len, Py_ssize_t* i)
3853
{
39-
int val = read_varint(table, len, i);
40-
return (val & 1) ? -(val >> 1) : (val >> 1);
54+
uint32_t val = static_cast<uint32_t>(read_varint(table, len, i));
55+
return (val & 1) ? -static_cast<int>(val >> 1) : static_cast<int>(val >> 1);
4156
}
4257

4358
#endif /* PY_VERSION_HEX >= 0x030b0000 */

0 commit comments

Comments
 (0)