Skip to content

Commit

Permalink
rslib: Simplify error path
Browse files Browse the repository at this point in the history
The four error path labels in rs_init() can be reduced to one by allocating
the struct with kzalloc so the pointers in the struct are NULL and can be
unconditionally handed in to kfree() because they either point to an
allocation or are NULL.

Signed-off-by: Thomas Gleixner <[email protected]>
Signed-off-by: Kees Cook <[email protected]>
  • Loading branch information
KAGA-KOKO authored and kees committed Apr 25, 2018
1 parent 689c6ef commit a85e126
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions lib/reed_solomon/reed_solomon.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
struct rs_control *rs;
int i, j, sr, root, iprim;

/* Allocate the control structure */
rs = kmalloc(sizeof(*rs), gfp);
rs = kzalloc(sizeof(*rs), gfp);
if (!rs)
return NULL;

Expand All @@ -78,15 +77,15 @@ static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
/* Allocate the arrays */
rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp);
if (rs->alpha_to == NULL)
goto errrs;
goto err;

rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp);
if (rs->index_of == NULL)
goto erralp;
goto err;

rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), gfp);
if(rs->genpoly == NULL)
goto erridx;
goto err;

/* Generate Galois field lookup tables */
rs->index_of[0] = rs->nn; /* log(zero) = -inf */
Expand All @@ -111,7 +110,7 @@ static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
}
/* If it's not primitive, exit */
if(sr != rs->alpha_to[0])
goto errpol;
goto err;

/* Find prim-th root of 1, used in decoding */
for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn);
Expand Down Expand Up @@ -141,14 +140,10 @@ static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int),
rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
return rs;

/* Error exit */
errpol:
err:
kfree(rs->genpoly);
erridx:
kfree(rs->index_of);
erralp:
kfree(rs->alpha_to);
errrs:
kfree(rs);
return NULL;
}
Expand Down

0 comments on commit a85e126

Please sign in to comment.