unicode: mkutf8data: Add malloc return value detection
PATCH 1/1 unicode: mkutf8data: Add malloc return value detection
Add the check and judgment statement of malloc return value.
Signed-off-by Li zeming <[email protected]>
--- fs/unicode/mkutf8data.c | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
fs/unicode/mkutf8data.c +42 -0
@@ -495,6 +495,9 @@ static struct node *alloc_node(struct node *parent)int bitnum; node =malloc (sizeof (*node)); +if (unlikely (!node)) +return NULL ; + node->left = node->right =NULL ; node->parent = parent; node->leftnode =NODE ;
@@ -2160,6 +2163,9 @@ static void nfdi_init(void) mapping[i++] =0 ; um =malloc (i *sizeof (unsigned int )); +if (unlikely (!um)) +return ; +memcpy (um, mapping, i *sizeof (unsigned int )); unicode_data[unichar].utf32nfdi = um;
@@ -2216,6 +2222,9 @@ static void nfdicf_init(void) mapping[i++] =0 ; um =malloc (i *sizeof (unsigned int )); +if (unlikely (!um)) +return ; +memcpy (um, mapping, i *sizeof (unsigned int )); unicode_data[unichar].utf32nfdicf = um;
@@ -2257,10 +2266,16 @@ static void ignore_init(void)for (unichar = first; unichar <= last; unichar++) {free (unicode_data[unichar].utf32nfdi); um =malloc (sizeof (unsigned int )); +if (unlikely (!um)) +return ; + *um =0 ; unicode_data[unichar].utf32nfdi = um;free (unicode_data[unichar].utf32nfdicf); um =malloc (sizeof (unsigned int )); +if (unlikely (!um)) +return ; + *um =0 ; unicode_data[unichar].utf32nfdicf = um; count++;
@@ -2278,10 +2293,16 @@ static void ignore_init(void)line_fail (prop_name, line);free (unicode_data[unichar].utf32nfdi); um =malloc (sizeof (unsigned int )); +if (unlikely (!um)) +return ; + *um =0 ; unicode_data[unichar].utf32nfdi = um;free (unicode_data[unichar].utf32nfdicf); um =malloc (sizeof (unsigned int )); +if (unlikely (!um)) +return ; + *um =0 ; unicode_data[unichar].utf32nfdicf = um;if (verbose >1 )
@@ -2360,6 +2381,9 @@ static void corrections_init(void) mapping[i++] =0 ; um =malloc (i *sizeof (unsigned int )); +if (unlikely (!um)) +return ; +memcpy (um, mapping, i *sizeof (unsigned int )); corrections[count].utf32nfdi = um;
@@ -2460,11 +2484,17 @@ static void hangul_decompose(void)assert (!unicode_data[unichar].utf32nfdi); um =malloc (i *sizeof (unsigned int )); +if (unlikely (!um)) +return ; +memcpy (um, mapping, i *sizeof (unsigned int )); unicode_data[unichar].utf32nfdi = um;assert (!unicode_data[unichar].utf32nfdicf); um =malloc (i *sizeof (unsigned int )); +if (unlikely (!um)) +return ; +memcpy (um, mapping, i *sizeof (unsigned int )); unicode_data[unichar].utf32nfdicf = um;
@@ -2474,6 +2504,9 @@ static void hangul_decompose(void)* trie. */ unicode_data[unichar].utf8nfdi =malloc (2 ); +if (unlikely (!(unicode_data[unichar].utf8nfdi))) +return ; + unicode_data[unichar].utf8nfdi[0 ] =HANGUL ; unicode_data[unichar].utf8nfdi[1 ] ='\0' ;
@@ -2524,12 +2557,18 @@ static void nfdi_decompose(void)break ;free (unicode_data[unichar].utf32nfdi); um =malloc (i *sizeof (unsigned int )); +if (unlikely (!um)) +return ; +memcpy (um, mapping, i *sizeof (unsigned int )); unicode_data[unichar].utf32nfdi = um; }/* Add this decomposition to nfdicf if there is no entry. */ if (!unicode_data[unichar].utf32nfdicf) { um =malloc (i *sizeof (unsigned int )); +if (unlikely (!um)) +return ; +memcpy (um, mapping, i *sizeof (unsigned int )); unicode_data[unichar].utf32nfdicf = um; }
@@ -2578,6 +2617,9 @@ static void nfdicf_decompose(void)break ;free (unicode_data[unichar].utf32nfdicf); um =malloc (i *sizeof (unsigned int )); +if (unlikely (!um)) +return ; +memcpy (um, mapping, i *sizeof (unsigned int )); unicode_data[unichar].utf32nfdicf = um; }
Signature · 1 line
2.18.2
Quoted 1 line · Li zeming
Add the check and judgment statement of malloc return value.
Why? Just to shut up some static checker?
fs/unicode/mkutf8data.c static struct node *alloc_node(struct node *parent) · line 500
Quoted 8 lines
+++
b/fs/unicode/mkutf8data.cfs/unicode/mkutf8data.c +3
-0@@ -495,6 +495,9 @@ static struct node *alloc_node(struct node *parent)int bitnum; node =malloc (sizeof (*node)); +if (unlikely (!node)) +return NULL ; +
Right, so now alloc_node() can return NULL when it couldn't before. Look at the callers ...
while (keybits) {
if (!*cursor)
*cursor = alloc_node(node);
node = *cursor;
if (node->nextbyte)
key++;They're unprepared for alloc_node() to return NULL, so all you've done is move the crash.