unicode: mkutf8data: Add malloc return value detection

Message ID 20221014075710.310943-1-zeming@nfschina.com
State New
Headers
Series unicode: mkutf8data: Add malloc return value detection |

Commit Message

Li zeming Oct. 14, 2022, 7:57 a.m. UTC
  Add the check and judgment statement of malloc return value.

Signed-off-by: Li zeming <zeming@nfschina.com>
---
 fs/unicode/mkutf8data.c | 42 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
  

Comments

Matthew Wilcox Oct. 14, 2022, 1:03 p.m. UTC | #1
On Fri, Oct 14, 2022 at 03:57:10PM +0800, Li zeming wrote:
> Add the check and judgment statement of malloc return value.

Why?  Just to shut up some static checker?

> +++ b/fs/unicode/mkutf8data.c
> @@ -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.
  

Patch

diff --git a/fs/unicode/mkutf8data.c b/fs/unicode/mkutf8data.c
index bc1a7c8b5c8d..d7f7f7c4cf56 100644
--- a/fs/unicode/mkutf8data.c
+++ b/fs/unicode/mkutf8data.c
@@ -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;
 		}