@@ -210,8 +210,9 @@ STATIC void next_char(mp_lexer_t *lex) {
210210
211211void indent_push (mp_lexer_t * lex , uint indent ) {
212212 if (lex -> num_indent_level >= lex -> alloc_indent_level ) {
213- lex -> indent_level = m_renew (uint16_t , lex -> indent_level , lex -> alloc_indent_level , lex -> alloc_indent_level * 2 );
214- lex -> alloc_indent_level *= 2 ;
213+ // TODO use m_renew_maybe and somehow indicate an error if it fails... probably by using MP_TOKEN_MEMORY_ERROR
214+ lex -> indent_level = m_renew (uint16_t , lex -> indent_level , lex -> alloc_indent_level , lex -> alloc_indent_level + MP_ALLOC_LEXEL_INDENT_INC );
215+ lex -> alloc_indent_level += MP_ALLOC_LEXEL_INDENT_INC ;
215216 }
216217 lex -> indent_level [lex -> num_indent_level ++ ] = indent ;
217218}
@@ -696,7 +697,15 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
696697}
697698
698699mp_lexer_t * mp_lexer_new (qstr src_name , void * stream_data , mp_lexer_stream_next_char_t stream_next_char , mp_lexer_stream_close_t stream_close ) {
699- mp_lexer_t * lex = m_new (mp_lexer_t , 1 );
700+ mp_lexer_t * lex = m_new_maybe (mp_lexer_t , 1 );
701+
702+ // check for memory allocation error
703+ if (lex == NULL ) {
704+ if (stream_close ) {
705+ stream_close (stream_data );
706+ }
707+ return NULL ;
708+ }
700709
701710 lex -> source_name = src_name ;
702711 lex -> stream_data = stream_data ;
@@ -706,12 +715,20 @@ mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_
706715 lex -> column = 1 ;
707716 lex -> emit_dent = 0 ;
708717 lex -> nested_bracket_level = 0 ;
709- lex -> alloc_indent_level = 16 ;
718+ lex -> alloc_indent_level = MP_ALLOC_LEXER_INDENT_INIT ;
710719 lex -> num_indent_level = 1 ;
711- lex -> indent_level = m_new (uint16_t , lex -> alloc_indent_level );
712- lex -> indent_level [0 ] = 0 ;
720+ lex -> indent_level = m_new_maybe (uint16_t , lex -> alloc_indent_level );
713721 vstr_init (& lex -> vstr , 32 );
714722
723+ // check for memory allocation error
724+ if (lex -> indent_level == NULL || vstr_had_error (& lex -> vstr )) {
725+ mp_lexer_free (lex );
726+ return NULL ;
727+ }
728+
729+ // store sentinel for first indentation level
730+ lex -> indent_level [0 ] = 0 ;
731+
715732 // preload characters
716733 lex -> chr0 = stream_next_char (stream_data );
717734 lex -> chr1 = stream_next_char (stream_data );
0 commit comments