diff mbox series

[ndctl,1/4] ndctl/namespace.c: fix resource leak

Message ID 20190510190839.29637-2-vishal.l.verma@intel.com (mailing list archive)
State Accepted
Commit a898d071486b961dc9de9f41f4a590230bf54b4f
Headers show
Series static analysis fixes | expand

Commit Message

Vishal Verma May 10, 2019, 7:08 p.m. UTC
Static analysis warns that we could be leaking cmd_cap and cmd_clear.
Fix the error handling to avoid the leaks.

Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 ndctl/namespace.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/ndctl/namespace.c b/ndctl/namespace.c
index c7abcbf..e281298 100644
--- a/ndctl/namespace.c
+++ b/ndctl/namespace.c
@@ -1083,40 +1083,42 @@  static int bus_send_clear(struct ndctl_bus *bus, unsigned long long start,
 	rc = ndctl_cmd_submit_xlat(cmd_cap);
 	if (rc < 0) {
 		debug("bus: %s failed to submit cmd: %d\n", busname, rc);
-		ndctl_cmd_unref(cmd_cap);
-		return rc;
+		goto out_cap;
 	}
 
 	/* send clear_error */
 	if (ndctl_cmd_ars_cap_get_range(cmd_cap, &range)) {
 		debug("bus: %s failed to get ars_cap range\n", busname);
-		return -ENXIO;
+		rc = -ENXIO;
+		goto out_cap;
 	}
 
 	cmd_clear = ndctl_bus_cmd_new_clear_error(range.address,
 					range.length, cmd_cap);
 	if (!cmd_clear) {
 		debug("bus: %s failed to create cmd\n", busname);
-		return -ENOTTY;
+		rc = -ENOTTY;
+		goto out_cap;
 	}
 
 	rc = ndctl_cmd_submit_xlat(cmd_clear);
 	if (rc < 0) {
 		debug("bus: %s failed to submit cmd: %d\n", busname, rc);
-		ndctl_cmd_unref(cmd_clear);
-		return rc;
+		goto out_clr;
 	}
 
 	cleared = ndctl_cmd_clear_error_get_cleared(cmd_clear);
 	if (cleared != range.length) {
 		debug("bus: %s expected to clear: %lld actual: %lld\n",
 				busname, range.length, cleared);
-		return -ENXIO;
+		rc = -ENXIO;
 	}
 
-	ndctl_cmd_unref(cmd_cap);
+out_clr:
 	ndctl_cmd_unref(cmd_clear);
-	return 0;
+out_cap:
+	ndctl_cmd_unref(cmd_cap);
+	return rc;
 }
 
 static int nstype_clear_badblocks(struct ndctl_namespace *ndns,