In env_mtd_load(), put_mtd_device(mtd_env) should only be called after
setup_mtd_device(&mtd_env) returned success. Failing to do so will trigger
BUG_ON(mtd->usecount < 0) crash in __put_mtd_device().

Adding an early error out path for setup_mtd_device() to skip
put_mtd_device() will solve this issue.

Fixes: 39ae954b04ef ("env: mtd: add the missing put_mtd_device()")
Signed-off-by: Weijie Gao <[email protected]>
---
 env/mtd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/env/mtd.c b/env/mtd.c
index b26ee80985a..67d207272d8 100644
--- a/env/mtd.c
+++ b/env/mtd.c
@@ -157,7 +157,7 @@ static int env_mtd_load(void)
 
        ret = setup_mtd_device(&mtd_env);
        if (ret)
-               goto out;
+               goto early_out;
 
        sect_size = mtd_env->erasesize;
 
@@ -192,6 +192,7 @@ static int env_mtd_load(void)
 out:
        put_mtd_device(mtd_env);
 
+early_out:
        free(buf);
 
        return ret;
-- 
2.45.2

Reply via email to