Skip to content

Commit 9bf5f28

Browse files
committed
py: Add further checks for failed malloc in lexer init functions.
1 parent a820276 commit 9bf5f28

File tree

5 files changed

+11
-4
lines changed

5 files changed

+11
-4
lines changed

py/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
731731
}
732732

733733
mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_byte_t stream_next_byte, mp_lexer_stream_close_t stream_close) {
734-
mp_lexer_t *lex = m_new_maybe(mp_lexer_t, 1);
734+
mp_lexer_t *lex = m_new_obj_maybe(mp_lexer_t);
735735

736736
// check for memory allocation error
737737
if (lex == NULL) {

py/lexerstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ STATIC void str_buf_free(mp_lexer_str_buf_t *sb) {
5252
}
5353

5454
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) {
55-
mp_lexer_str_buf_t *sb = m_new_maybe(mp_lexer_str_buf_t, 1);
55+
mp_lexer_str_buf_t *sb = m_new_obj_maybe(mp_lexer_str_buf_t);
5656
if (sb == NULL) {
5757
return NULL;
5858
}

py/lexerunix.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ STATIC void file_buf_close(mp_lexer_file_buf_t *fb) {
6969
}
7070

7171
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
72-
mp_lexer_file_buf_t *fb = m_new_obj(mp_lexer_file_buf_t);
72+
mp_lexer_file_buf_t *fb = m_new_obj_maybe(mp_lexer_file_buf_t);
73+
if (fb == NULL) {
74+
return NULL;
75+
}
7376
fb->fd = open(filename, O_RDONLY);
7477
if (fb->fd < 0) {
7578
m_del_obj(mp_lexer_file_buf_t, fb);

py/misc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ typedef unsigned int uint;
5454
#define m_new_maybe(type, num) ((type*)(m_malloc_maybe(sizeof(type) * (num))))
5555
#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
5656
#define m_new_obj(type) (m_new(type, 1))
57+
#define m_new_obj_maybe(type) (m_new_maybe(type, 1))
5758
#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
5859
#define m_new_obj_var_maybe(obj_type, var_type, var_num) ((obj_type*)m_malloc_maybe(sizeof(obj_type) + sizeof(var_type) * (var_num)))
5960
#if MICROPY_ENABLE_FINALISER

stmhal/lexerfatfs.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ STATIC void file_buf_close(mp_lexer_file_buf_t *fb) {
6464
}
6565

6666
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
67-
mp_lexer_file_buf_t *fb = m_new_obj(mp_lexer_file_buf_t);
67+
mp_lexer_file_buf_t *fb = m_new_obj_maybe(mp_lexer_file_buf_t);
68+
if (fb == NULL) {
69+
return NULL;
70+
}
6871
FRESULT res = f_open(&fb->fp, filename, FA_READ);
6972
if (res != FR_OK) {
7073
m_del_obj(mp_lexer_file_buf_t, fb);

0 commit comments

Comments
 (0)