Skip to content

Commit 3fa63bc

Browse files
saddamr3ematz
authored andcommitted
debug.c: bound the packed line-map decode to its buffer
1 parent b1612ff commit 3fa63bc

4 files changed

Lines changed: 14 additions & 10 deletions

File tree

‎include/mruby/internal.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ mrb_value mrb_obj_equal_m(mrb_state *mrb, mrb_value);
9999
/* debug */
100100
size_t mrb_packed_int_len(uint32_t num);
101101
size_t mrb_packed_int_encode(uint32_t num, uint8_t *p);
102-
uint32_t mrb_packed_int_decode(const uint8_t *p, const uint8_t **newpos);
102+
uint32_t mrb_packed_int_decode(const uint8_t *p, const uint8_t *end, const uint8_t **newpos);
103103

104104

105105
/* codedump */

‎mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ check_lineno(mrb_irep_debug_info_file *info_file, uint16_t lineno)
5050
const uint8_t *pend = p + count;
5151
uint32_t line = 0;
5252
while (p < pend) {
53-
mrb_packed_int_decode(p, &p);
54-
line += mrb_packed_int_decode(p, &p);
53+
mrb_packed_int_decode(p, pend, &p);
54+
line += mrb_packed_int_decode(p, pend, &p);
5555
if (line == lineno) return lineno;
5656
}
5757
}

‎src/debug.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,21 @@ mrb_packed_int_encode(uint32_t num, uint8_t *p)
7777
* Decodes a 32-bit unsigned integer from the variable-length packed format
7878
* in the byte array `p`. It reads bytes until it finds one where the most
7979
* significant bit is 0.
80+
* If `end` is not NULL, decoding stops at `end`, so a truncated or corrupt
81+
* buffer (a trailing byte whose continuation bit is set) cannot be read
82+
* past; callers that decode trusted, self-produced data pass NULL.
8083
* If `newpos` is not NULL, it will be updated to point to the byte
8184
* following the last byte read.
8285
* Returns the decoded 32-bit unsigned integer.
8386
*/
8487
uint32_t
85-
mrb_packed_int_decode(const uint8_t *p, const uint8_t **newpos)
88+
mrb_packed_int_decode(const uint8_t *p, const uint8_t *end, const uint8_t **newpos)
8689
{
8790
size_t i = 0, shift = 0;
8891
uint32_t n = 0;
8992

9093
do {
94+
if (end && p + i >= end) break;
9195
n |= ((uint32_t)(p[i] & 0x7f)) << shift;
9296
i++;
9397
shift += 7;
@@ -119,8 +123,8 @@ debug_get_line(mrb_state *mrb, mrb_irep_debug_info_file* f, uint32_t pc)
119123
const uint8_t *pend = p + f->line_entry_count;
120124
uint32_t pos = 0, line = 0;
121125
while (p < pend) {
122-
pos += mrb_packed_int_decode(p, &p);
123-
uint32_t line_diff = mrb_packed_int_decode(p, &p);
126+
pos += mrb_packed_int_decode(p, pend, &p);
127+
uint32_t line_diff = mrb_packed_int_decode(p, pend, &p);
124128
if (pc < pos) break;
125129
line += line_diff;
126130
}

‎src/symbol.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ sym_check(mrb_state *mrb, const char *name, size_t len, mrb_sym i)
227227
else {
228228
/* length in BER */
229229
const uint8_t *p = (const uint8_t*)symname;
230-
symlen = mrb_packed_int_decode(p, &p);
230+
symlen = mrb_packed_int_decode(p, NULL, &p);
231231
symname = (const char*)p;
232232
}
233233
if (len == symlen && memcmp(symname, name, len) == 0) {
@@ -335,7 +335,7 @@ migrate_to_hash_table(mrb_state *mrb)
335335
else {
336336
/* This is a packed length string */
337337
const uint8_t *p = (const uint8_t*)name;
338-
len = mrb_packed_int_decode(p, &p);
338+
len = mrb_packed_int_decode(p, NULL, &p);
339339
name = (const char*)p;
340340
}
341341

@@ -667,7 +667,7 @@ sym2name_len(mrb_state *mrb, mrb_sym sym, char *buf, mrb_int *lenp)
667667

668668
if (!symtbl_is_literal(tagged_ptr)) {
669669
const uint8_t *p = (const uint8_t*)symname;
670-
uint32_t len = mrb_packed_int_decode(p, &p);
670+
uint32_t len = mrb_packed_int_decode(p, NULL, &p);
671671
symname = (const char*)p;
672672
if (lenp) *lenp = (mrb_int)len;
673673
}
@@ -971,7 +971,7 @@ mrb_symbol_gc(mrb_state *mrb)
971971
}
972972
else {
973973
const uint8_t *p = (const uint8_t*)name;
974-
len = mrb_packed_int_decode(p, &p);
974+
len = mrb_packed_int_decode(p, NULL, &p);
975975
name = (const char*)p;
976976
}
977977
uint8_t hash = mrb_byte_hash((const uint8_t*)name, len);

0 commit comments

Comments
 (0)