diff --git a/ChangeLog b/ChangeLog index 322faa0..bf558e0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2021-09-18 Theppitak Karoonboonyanan + + Handle possible malloc failures. + + * src/thbrk/brk-maximal.c (brk_maximal_do_impl, brk_recover_try): + - Handle cases where brk_pool_node_new() possibly returns NULL. + In both places, fail gracefully by behaving as if the dict state + were single there. + + Partially addresses Issue #15. + 2021-09-18 Theppitak Karoonboonyanan Fix typo in TIS-620 character name. diff --git a/src/thbrk/brk-maximal.c b/src/thbrk/brk-maximal.c index dc65aac..dc8427b 100644 --- a/src/thbrk/brk-maximal.c +++ b/src/thbrk/brk-maximal.c @@ -231,9 +231,12 @@ brk_maximal_do_impl (const thwchar_t *ws, int len, !trie_state_is_single (shot->dict_state)) { /* add node to mark break position instead of current */ - node = brk_pool_node_new (shot, env); - pool = brk_pool_add (pool, node); - shot = &node->shot; + BrkPool *new_node = brk_pool_node_new (shot, env); + if (LIKELY (new_node)) { + node = new_node; + pool = brk_pool_add (pool, node); + shot = &node->shot; + } } trie_state_rewind (shot->dict_state); @@ -326,9 +329,12 @@ brk_recover_try (const thwchar_t *ws, int len, !trie_state_is_single (shot->dict_state)) { /* add node to mark break position instead of current */ - node = brk_pool_node_new (shot, env); - pool = brk_pool_add (pool, node); - shot = &node->shot; + BrkPool *new_node = brk_pool_node_new (shot, env); + if (LIKELY (new_node)) { + node = new_node; + pool = brk_pool_add (pool, node); + shot = &node->shot; + } } trie_state_rewind (shot->dict_state);