From 9bf5f2857d2015e3d440feb03c102ed9730c8f4d Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 9 Oct 2014 16:53:37 +0100 Subject: [PATCH] py: Add further checks for failed malloc in lexer init functions. --- py/lexer.c | 2 +- py/lexerstr.c | 2 +- py/lexerunix.c | 5 ++++- py/misc.h | 1 + stmhal/lexerfatfs.c | 5 ++++- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/py/lexer.c b/py/lexer.c index ff137fbbb4..a9444645a0 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -731,7 +731,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs } 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) { - mp_lexer_t *lex = m_new_maybe(mp_lexer_t, 1); + mp_lexer_t *lex = m_new_obj_maybe(mp_lexer_t); // check for memory allocation error if (lex == NULL) { diff --git a/py/lexerstr.c b/py/lexerstr.c index 3a68441107..a1f7ce41d0 100644 --- a/py/lexerstr.c +++ b/py/lexerstr.c @@ -52,7 +52,7 @@ STATIC void str_buf_free(mp_lexer_str_buf_t *sb) { } mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) { - mp_lexer_str_buf_t *sb = m_new_maybe(mp_lexer_str_buf_t, 1); + mp_lexer_str_buf_t *sb = m_new_obj_maybe(mp_lexer_str_buf_t); if (sb == NULL) { return NULL; } diff --git a/py/lexerunix.c b/py/lexerunix.c index 9d669f2bfe..52eac9eda5 100644 --- a/py/lexerunix.c +++ b/py/lexerunix.c @@ -69,7 +69,10 @@ STATIC void file_buf_close(mp_lexer_file_buf_t *fb) { } mp_lexer_t *mp_lexer_new_from_file(const char *filename) { - mp_lexer_file_buf_t *fb = m_new_obj(mp_lexer_file_buf_t); + mp_lexer_file_buf_t *fb = m_new_obj_maybe(mp_lexer_file_buf_t); + if (fb == NULL) { + return NULL; + } fb->fd = open(filename, O_RDONLY); if (fb->fd < 0) { m_del_obj(mp_lexer_file_buf_t, fb); diff --git a/py/misc.h b/py/misc.h index d828998cfd..2f53dcbd3d 100644 --- a/py/misc.h +++ b/py/misc.h @@ -54,6 +54,7 @@ typedef unsigned int uint; #define m_new_maybe(type, num) ((type*)(m_malloc_maybe(sizeof(type) * (num)))) #define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num)))) #define m_new_obj(type) (m_new(type, 1)) +#define m_new_obj_maybe(type) (m_new_maybe(type, 1)) #define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num))) #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))) #if MICROPY_ENABLE_FINALISER diff --git a/stmhal/lexerfatfs.c b/stmhal/lexerfatfs.c index 21e3a2007e..ea47aaf884 100644 --- a/stmhal/lexerfatfs.c +++ b/stmhal/lexerfatfs.c @@ -64,7 +64,10 @@ STATIC void file_buf_close(mp_lexer_file_buf_t *fb) { } mp_lexer_t *mp_lexer_new_from_file(const char *filename) { - mp_lexer_file_buf_t *fb = m_new_obj(mp_lexer_file_buf_t); + mp_lexer_file_buf_t *fb = m_new_obj_maybe(mp_lexer_file_buf_t); + if (fb == NULL) { + return NULL; + } FRESULT res = f_open(&fb->fp, filename, FA_READ); if (res != FR_OK) { m_del_obj(mp_lexer_file_buf_t, fb);