Commit 3e242830b089e74e9bc35a0960757ea2b53d4a21

Authored by Danny Al-Gaaf
1 parent f6936562
Exists in master and in 2 other branches v2, v3

tools/gf_poly.c: fix undefined allocation of 0 bytes

Due to man page of malloc the behaviour in case of allocation size of
0 bytes is undefined: "If size was equal to 0, either NULL or a
pointer suitable to be passed  to free() is returned"

Fix for clang scan-build report:

Unix API     Undefined allocation of 0 bytes (CERT MEM04-C; CWE-131)

210 poly = (gf_general_t *) malloc(sizeof(gf_general_t)*(n+1));

    9 Call to 'malloc' has an allocation size of 0 bytes

Signed-off-by: Danny Al-Gaaf <danny.al-gaaf@bisect.de>
Showing 1 changed file with 8 additions and 2 deletions   Show diff stats
tools/gf_poly.c
... ... @@ -52,6 +52,7 @@
52 52 #include <stdio.h>
53 53 #include <stdlib.h>
54 54 #include <string.h>
  55 +#include <assert.h>
55 56  
56 57 char *BM = "Bad Method: ";
57 58  
... ... @@ -203,9 +204,14 @@ int main(int argc, char **argv)
203 204 sprintf(string, "Argument '%s' not in proper format of power:coefficient\n", argv[i]);
204 205 usage(string);
205 206 }
206   - if (power < 0) usage("Can't have negative powers\n");
207   - if (power > n) n = power;
  207 + if (power < 0) {
  208 + usage("Can't have negative powers\n");
  209 + } else {
  210 + n = power;
  211 + }
208 212 }
  213 + // in case the for-loop header fails
  214 + assert (n >= 0);
209 215  
210 216 poly = (gf_general_t *) malloc(sizeof(gf_general_t)*(n+1));
211 217 for (i = 0; i <= n; i++) gf_general_set_zero(poly+i, w);
... ...