Skip to content

Commit 7429b8f

Browse files
committed
py/bc: Move mp_decode_uint_checked to py/bc.c.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 14ba32b commit 7429b8f

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

extmod/vfs_rom.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,6 @@ struct _mp_obj_vfs_rom_t {
102102
const uint8_t *filesystem_end;
103103
};
104104

105-
// Returns 0 for success, -1 for failure.
106-
static int mp_decode_uint_checked(const uint8_t **ptr, const uint8_t *ptr_max, mp_uint_t *value_out) {
107-
mp_uint_t unum = 0;
108-
byte val;
109-
const uint8_t *p = *ptr;
110-
do {
111-
if (p >= ptr_max) {
112-
return -1;
113-
}
114-
val = *p++;
115-
unum = (unum << 7) | (val & 0x7f);
116-
} while ((val & 0x80) != 0);
117-
*ptr = p;
118-
*value_out = unum;
119-
return 0;
120-
}
121-
122105
static record_kind_t extract_record(const uint8_t **fs, const uint8_t **fs_next, const uint8_t *fs_max) {
123106
mp_uint_t record_kind;
124107
if (mp_decode_uint_checked(fs, fs_max, &record_kind) != 0) {

py/bc.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ const byte *mp_decode_uint_skip(const byte *ptr) {
8888
return ptr;
8989
}
9090

91+
// This function does bounds checking when decoding a value.
92+
// Returns 0 for success, -1 for failure.
93+
int mp_decode_uint_checked(const uint8_t **ptr, const uint8_t *ptr_max, mp_uint_t *value_out) {
94+
mp_uint_t unum = 0;
95+
byte val;
96+
const uint8_t *p = *ptr;
97+
do {
98+
if (p >= ptr_max) {
99+
return -1;
100+
}
101+
val = *p++;
102+
unum = (unum << 7) | (val & 0x7f);
103+
} while ((val & 0x80) != 0);
104+
*ptr = p;
105+
*value_out = unum;
106+
return 0;
107+
}
108+
91109
static NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, size_t expected, size_t given) {
92110
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
93111
// generic message, used also for other argument issues

py/bc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ void mp_encode_uint(void *env, mp_encode_uint_allocator_t allocator, mp_uint_t v
272272
mp_uint_t mp_decode_uint(const byte **ptr);
273273
mp_uint_t mp_decode_uint_value(const byte *ptr);
274274
const byte *mp_decode_uint_skip(const byte *ptr);
275+
int mp_decode_uint_checked(const uint8_t **ptr, const uint8_t *ptr_max, mp_uint_t *value_out);
275276

276277
mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state,
277278
#ifndef __cplusplus

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy