py: Add further checks for failed malloc in lexer init functions.
This commit is contained in:
parent
a8202762f0
commit
9bf5f2857d
|
@ -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 *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
|
// check for memory allocation error
|
||||||
if (lex == NULL) {
|
if (lex == NULL) {
|
||||||
|
|
|
@ -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_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) {
|
if (sb == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_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);
|
fb->fd = open(filename, O_RDONLY);
|
||||||
if (fb->fd < 0) {
|
if (fb->fd < 0) {
|
||||||
m_del_obj(mp_lexer_file_buf_t, fb);
|
m_del_obj(mp_lexer_file_buf_t, fb);
|
||||||
|
|
|
@ -54,6 +54,7 @@ typedef unsigned int uint;
|
||||||
#define m_new_maybe(type, num) ((type*)(m_malloc_maybe(sizeof(type) * (num))))
|
#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_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
|
||||||
#define m_new_obj(type) (m_new(type, 1))
|
#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(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)))
|
#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
|
#if MICROPY_ENABLE_FINALISER
|
||||||
|
|
|
@ -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_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);
|
FRESULT res = f_open(&fb->fp, filename, FA_READ);
|
||||||
if (res != FR_OK) {
|
if (res != FR_OK) {
|
||||||
m_del_obj(mp_lexer_file_buf_t, fb);
|
m_del_obj(mp_lexer_file_buf_t, fb);
|
||||||
|
|
Loading…
Reference in New Issue