[PATCH 2/2] tools/testing/nvdimm: Improve error detection in __test_alloc()

From: SF Markus Elfring
Date: Thu Jul 02 2015 - 15:05:20 EST


From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 2 Jul 2015 20:45:53 +0200

Three checks were combined at the beginning of the __test_alloc() function.
This implementation detail can become safer according to the Linux coding
style convention.

* Return directly if a null pointer was passed for the variable "buf".
Delete an unnecessary check then.

* Allocate memory for the data structure "nfit_test_resource"
only if a previous allocation succeeded.

* Rename a jump label.

Signed-off-by: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
---
tools/testing/nvdimm/test/nfit.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/tools/testing/nvdimm/test/nfit.c b/tools/testing/nvdimm/test/nfit.c
index 9c910f1..d5f22ea 100644
--- a/tools/testing/nvdimm/test/nfit.c
+++ b/tools/testing/nvdimm/test/nfit.c
@@ -244,16 +244,21 @@ static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
void *buf)
{
struct device *dev = &t->pdev.dev;
- struct resource *res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
- struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
- GFP_KERNEL);
+ struct resource *res;
+ struct nfit_test_resource *nfit_res = NULL;
int rc;

- if (!res || !buf || !nfit_res)
- goto err;
+ if (!buf)
+ return NULL;
+ res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
+ if (!res)
+ goto free_memory;
+ nfit_res = kzalloc(sizeof(*nfit_res), GFP_KERNEL);
+ if (!nfit_res)
+ goto free_memory;
rc = devm_add_action(dev, release_nfit_res, nfit_res);
if (rc)
- goto err;
+ goto free_memory;
INIT_LIST_HEAD(&nfit_res->list);
memset(buf, 0, size);
nfit_res->dev = dev;
@@ -267,8 +272,8 @@ static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
spin_unlock(&nfit_test_lock);

return nfit_res->buf;
- err:
- if (buf && !is_vmalloc_addr(buf))
+free_memory:
+ if (!is_vmalloc_addr(buf))
dma_free_coherent(dev, size, buf, *dma);
else
vfree(buf);
--
2.4.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/