[PATCH] libceph: check pointer before assigned to "c->rules[]"

Li Qiong posted 1 patch 3 years, 10 months ago
net/ceph/osdmap.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] libceph: check pointer before assigned to "c->rules[]"
Posted by Li Qiong 3 years, 10 months ago
It should be better to check pointer firstly, then assign it
to c->rules[]. Refine code a little bit.

Signed-off-by: Li Qiong <liqiong@nfschina.com>
---
 net/ceph/osdmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ceph/osdmap.c b/net/ceph/osdmap.c
index 2823bb3cff55..6ec5007f96dc 100644
--- a/net/ceph/osdmap.c
+++ b/net/ceph/osdmap.c
@@ -571,9 +571,9 @@ static struct crush_map *crush_decode(void *pbyval, void *end)
 			goto bad;
 #endif
 		r = kmalloc(struct_size(r, steps, yes), GFP_NOFS);
-		c->rules[i] = r;
 		if (r == NULL)
 			goto badmem;
+		c->rules[i] = r;
 		dout(" rule %d is at %p\n", i, r);
 		r->len = yes;
 		ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
-- 
2.11.0
Re: [PATCH] libceph: check pointer before assigned to "c->rules[]"
Posted by Ilya Dryomov 3 years, 10 months ago
On Tue, Jun 14, 2022 at 9:12 AM Li Qiong <liqiong@nfschina.com> wrote:
>
> It should be better to check pointer firstly, then assign it
> to c->rules[]. Refine code a little bit.
>
> Signed-off-by: Li Qiong <liqiong@nfschina.com>
> ---
>  net/ceph/osdmap.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/ceph/osdmap.c b/net/ceph/osdmap.c
> index 2823bb3cff55..6ec5007f96dc 100644
> --- a/net/ceph/osdmap.c
> +++ b/net/ceph/osdmap.c
> @@ -571,9 +571,9 @@ static struct crush_map *crush_decode(void *pbyval, void *end)
>                         goto bad;
>  #endif
>                 r = kmalloc(struct_size(r, steps, yes), GFP_NOFS);
> -               c->rules[i] = r;
>                 if (r == NULL)
>                         goto badmem;
> +               c->rules[i] = r;
>                 dout(" rule %d is at %p\n", i, r);
>                 r->len = yes;
>                 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
> --
> 2.11.0
>

This doesn't change anything as c->rules pointer array is zeroed out.
Anyway, applied.

Thanks,

                Ilya