]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ASoC: sma1307: Add NULL check in sma1307_setting_loaded()
authorChenyuan Yang <chenyuan0y@gmail.com>
Tue, 11 Mar 2025 01:57:14 +0000 (20:57 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 29 May 2025 09:12:40 +0000 (11:12 +0200)
[ Upstream commit 0ec6bd16705fe21d6429d6b8f7981eae2142bba8 ]

All varibale allocated by kzalloc and devm_kzalloc could be NULL.
Multiple pointer checks and their cleanup are added.

This issue is found by our static analysis tool

Signed-off-by: Chenyuan Yang <chenyuan0y@gmail.com>
Link: https://patch.msgid.link/20250311015714.1333857-1-chenyuan0y@gmail.com
Signed-off-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
sound/soc/codecs/sma1307.c

index 480bcea48541e6b88f375b6fc5b04256a2f30bc1..b9d8136fe3dc1aa11780df90b27d735fa522b5fa 100644 (file)
@@ -1728,6 +1728,11 @@ static void sma1307_setting_loaded(struct sma1307_priv *sma1307, const char *fil
        }
 
        data = kzalloc(fw->size, GFP_KERNEL);
+       if (!data) {
+               release_firmware(fw);
+               sma1307->set.status = false;
+               return;
+       }
        size = fw->size >> 2;
        memcpy(data, fw->data, fw->size);
 
@@ -1741,6 +1746,12 @@ static void sma1307_setting_loaded(struct sma1307_priv *sma1307, const char *fil
        sma1307->set.header = devm_kzalloc(sma1307->dev,
                                           sma1307->set.header_size,
                                           GFP_KERNEL);
+       if (!sma1307->set.header) {
+               kfree(data);
+               sma1307->set.status = false;
+               return;
+       }
+
        memcpy(sma1307->set.header, data,
               sma1307->set.header_size * sizeof(int));
 
@@ -1756,6 +1767,13 @@ static void sma1307_setting_loaded(struct sma1307_priv *sma1307, const char *fil
        sma1307->set.def
            = devm_kzalloc(sma1307->dev,
                           sma1307->set.def_size * sizeof(int), GFP_KERNEL);
+       if (!sma1307->set.def) {
+               kfree(data);
+               kfree(sma1307->set.header);
+               sma1307->set.status = false;
+               return;
+       }
+
        memcpy(sma1307->set.def,
               &data[sma1307->set.header_size],
               sma1307->set.def_size * sizeof(int));
@@ -1768,6 +1786,16 @@ static void sma1307_setting_loaded(struct sma1307_priv *sma1307, const char *fil
                    = devm_kzalloc(sma1307->dev,
                                   sma1307->set.mode_size * 2 * sizeof(int),
                                   GFP_KERNEL);
+               if (!sma1307->set.mode_set[i]) {
+                       kfree(data);
+                       kfree(sma1307->set.header);
+                       kfree(sma1307->set.def);
+                       for (int j = 0; j < i; j++)
+                               kfree(sma1307->set.mode_set[j]);
+                       sma1307->set.status = false;
+                       return;
+               }
+
                for (int j = 0; j < sma1307->set.mode_size; j++) {
                        sma1307->set.mode_set[i][2 * j]
                            = data[offset + ((num_mode + 1) * j)];