[PATCH 1/7] EDAC/amd64: Improve error handling in amd64_edac_init()

From: Markus Elfring
Date: Mon Dec 25 2023 - 12:35:23 EST


From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 25 Dec 2023 16:21:34 +0100

The kfree() function was called in one case by the
amd64_edac_init() function during error handling
even if the passed variable contained a null pointer.
This issue was detected by using the Coccinelle software.

1. Return directly after a call of the function “kcalloc” failed.

2. Move an error code assignment into an if branch.

Signed-off-by: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/edac/amd64_edac.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/edac/amd64_edac.c b/drivers/edac/amd64_edac.c
index 537b9987a431..f912cbadefa4 100644
--- a/drivers/edac/amd64_edac.c
+++ b/drivers/edac/amd64_edac.c
@@ -4414,14 +4414,15 @@ static int __init amd64_edac_init(void)

opstate_init();

- err = -ENOMEM;
ecc_stngs = kcalloc(amd_nb_num(), sizeof(ecc_stngs[0]), GFP_KERNEL);
if (!ecc_stngs)
- goto err_free;
+ return -ENOMEM;

msrs = msrs_alloc();
- if (!msrs)
+ if (!msrs) {
+ err = -ENOMEM;
goto err_free;
+ }

for (i = 0; i < amd_nb_num(); i++) {
err = probe_one_instance(i);
--
2.43.0