ASoC: soc-card: soc-card-test: Fix some error handling in init()
authorDan Carpenter <dan.carpenter@linaro.org>
Fri, 12 Apr 2024 12:07:01 +0000 (15:07 +0300)
committerMark Brown <broonie@kernel.org>
Sun, 14 Apr 2024 07:54:39 +0000 (16:54 +0900)
There are two issues here:
1) The get_device() needs a matching put_device() on error paths.
2) The "if (!ret)" was supposed to be "if (ret)".

I re-arranged the code a bit to do the allocation before the
get_device().

Fixes: ef7784e41db7 ("ASoC: soc-card: Add KUnit test case for snd_soc_card_get_kcontrol")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Reviewed-by: Richard Fitzgerald <rf@opensource.cirrus.com>
Link: https://lore.kernel.org/r/450dd21a-b24b-48ba-9aa4-c02e4617852f@moroto.mountain
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/soc/soc-card-test.c

index 075c52fe82e51d349464b6503a500e52e0240ddd..e4a4b101d7438dda2913405acbe6d86457f66566 100644 (file)
@@ -134,22 +134,24 @@ static int soc_card_test_case_init(struct kunit *test)
 
        test->priv = priv;
 
+       priv->card = kunit_kzalloc(test, sizeof(*priv->card), GFP_KERNEL);
+       if (!priv->card)
+               return -ENOMEM;
+
        priv->card_dev = kunit_device_register(test, "sound-soc-card-test");
        priv->card_dev = get_device(priv->card_dev);
        if (!priv->card_dev)
                return -ENODEV;
 
-       priv->card = kunit_kzalloc(test, sizeof(*priv->card), GFP_KERNEL);
-       if (!priv->card)
-               return -ENOMEM;
-
        priv->card->name = "soc-card-test";
        priv->card->dev = priv->card_dev;
        priv->card->owner = THIS_MODULE;
 
        ret = snd_soc_register_card(priv->card);
-       if (!ret)
+       if (ret) {
+               put_device(priv->card_dev);
                return ret;
+       }
 
        return 0;
 }