Skip to content

Commit 402bd4f

Browse files
tkanteckmdcornu
authored andcommitted
erasure_code: various fixes for static code analysis issues
Signed-off-by: Tomasz Kantecki <[email protected]>
1 parent ac2ee91 commit 402bd4f

8 files changed

+61
-31
lines changed

erasure_code/erasure_code_base_perf.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stdio.h>
3131
#include <stdlib.h>
3232
#include <string.h> // for memset, memcmp
33+
#include <assert.h>
3334
#include "erasure_code.h"
3435
#include "test.h"
3536

@@ -112,10 +113,8 @@ int main(int argc, char *argv[])
112113

113114
printf("erasure_code_base_perf: %dx%d %d\n", m, TEST_LEN(m), nerrs);
114115

115-
if (m > MMAX || k > KMAX || nerrs > (m - k)) {
116-
printf(" Input test parameter error\n");
117-
return -1;
118-
}
116+
// check input parameters
117+
assert(!(m > MMAX || k > KMAX || nerrs > (m - k)));
119118

120119
memcpy(src_err_list, err_list, nerrs);
121120
memset(src_in_err, 0, TEST_SOURCES);

erasure_code/erasure_code_base_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stdio.h>
3131
#include <stdlib.h>
3232
#include <string.h> // for memset, memcmp
33+
#include <assert.h>
3334
#include "erasure_code.h"
3435
#include "test.h"
3536

@@ -264,8 +265,7 @@ int main(int argc, char *argv[])
264265
// Pick a first test
265266
m = 9;
266267
k = 5;
267-
if (m > MMAX || k > KMAX)
268-
return -1;
268+
assert((m <= MMAX) && (k <= KMAX));
269269

270270
// Make random data
271271
for (i = 0; i < k; i++)

erasure_code/erasure_code_perf.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ int main(int argc, char *argv[])
142142
return -1;
143143
}
144144
}
145-
m = k + p;
146145

147146
if (nerrs > k) {
148147
printf
@@ -151,20 +150,37 @@ int main(int argc, char *argv[])
151150
return -1;
152151
}
153152

153+
if (k <= 0) {
154+
printf("Number of source buffers (%d) must be > 0\n", k);
155+
return -1;
156+
}
157+
158+
if (p <= 0) {
159+
printf("Number of parity buffers (%d) must be > 0\n", p);
160+
return -1;
161+
}
162+
163+
if (nerrs <= 0) {
164+
printf("Number of errors (%d) must be > 0\n", nerrs);
165+
return -1;
166+
}
167+
154168
if (nerrs > p) {
155169
printf
156170
("Number of errors (%d) cannot be higher than number of parity buffers (%d)\n",
157171
nerrs, p);
158172
return -1;
159173
}
160174

175+
m = k + p;
176+
161177
if (m > MMAX) {
162178
printf("Number of total buffers (data and parity) cannot be higher than %d\n",
163179
MMAX);
164180
return -1;
165181
}
166182

167-
u8 *err_list = malloc(nerrs);
183+
u8 *err_list = malloc((size_t)nerrs);
168184
if (err_list == NULL) {
169185
printf("Error allocating list of array of error indices\n");
170186
return -1;
@@ -185,7 +201,7 @@ int main(int argc, char *argv[])
185201
printf("Testing with %u data buffers and %u parity buffers (num errors = %u, in [ ", k,
186202
p, nerrs);
187203
for (i = 0; i < nerrs; i++)
188-
printf("%d ", err_list[i]);
204+
printf("%d ", (int)err_list[i]);
189205

190206
printf("])\n");
191207

erasure_code/erasure_code_test.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stdio.h>
3131
#include <stdlib.h>
3232
#include <string.h> // for memset, memcmp
33+
#include <assert.h>
3334
#include "erasure_code.h"
3435
#include "test.h"
3536

@@ -266,10 +267,7 @@ int main(int argc, char *argv[])
266267
// Pick a first test
267268
m = 9;
268269
k = 5;
269-
if (m > MMAX || k > KMAX) {
270-
re = -1;
271-
goto exit;
272-
}
270+
assert((m <= MMAX) && (k <= KMAX));
273271

274272
// Make random data
275273
for (i = 0; i < k; i++)

erasure_code/erasure_code_update_perf.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ int main(int argc, char *argv[])
179179
return -1;
180180
}
181181
}
182-
m = k + p;
183182

184183
if (nerrs > k) {
185184
printf
@@ -188,20 +187,37 @@ int main(int argc, char *argv[])
188187
return -1;
189188
}
190189

190+
if (k <= 0) {
191+
printf("Number of source buffers (%d) must be > 0\n", k);
192+
return -1;
193+
}
194+
195+
if (p <= 0) {
196+
printf("Number of parity buffers (%d) must be > 0\n", p);
197+
return -1;
198+
}
199+
191200
if (nerrs > p) {
192201
printf
193202
("Number of errors (%d) cannot be higher than number of parity buffers (%d)\n",
194203
nerrs, p);
195204
return -1;
196205
}
197206

207+
if (nerrs <= 0) {
208+
printf("Number of errors (%d) must be > 0\n", nerrs);
209+
return -1;
210+
}
211+
212+
m = k + p;
213+
198214
if (m > MMAX) {
199215
printf("Number of total buffers (data and parity) cannot be higher than %d\n",
200216
MMAX);
201217
return -1;
202218
}
203219

204-
u8 *err_list = malloc(nerrs);
220+
u8 *err_list = malloc((size_t)nerrs);
205221
if (err_list == NULL) {
206222
printf("Error allocating list of array of error indices\n");
207223
return -1;

erasure_code/erasure_code_update_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stdio.h>
3131
#include <stdlib.h>
3232
#include <string.h> // for memset, memcmp
33+
#include <assert.h>
3334
#include "erasure_code.h"
3435
#include "test.h"
3536

@@ -290,8 +291,7 @@ int main(int argc, char *argv[])
290291
// Pick a first test
291292
m = 14;
292293
k = 10;
293-
if (m > MMAX || k > KMAX)
294-
goto exit;
294+
assert(!(m > MMAX || k > KMAX));
295295

296296
// Make random data
297297
for (i = 0; i < k; i++) {

erasure_code/gen_rs_matrix_limits.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99
#define ROWS M_MAX
1010
#define COLS K_MAX
1111

12-
static inline int min(int a, int b)
12+
static inline uint64_t min(const uint64_t a, const uint64_t b)
1313
{
1414
if (a <= b)
1515
return a;
1616
else
1717
return b;
1818
}
1919

20-
void gen_sub_matrix(unsigned char *out_matrix, int dim, unsigned char *in_matrix, int rows,
21-
int cols, uint64_t row_indicator, uint64_t col_indicator)
20+
void gen_sub_matrix(unsigned char *out_matrix, const uint64_t dim, unsigned char *in_matrix,
21+
const uint64_t rows, const uint64_t cols, const uint64_t row_indicator,
22+
const uint64_t col_indicator)
2223
{
23-
int i, j, r, s;
24+
uint64_t i, j, r, s;
2425

2526
for (i = 0, r = 0; i < rows; i++) {
2627
if (!(row_indicator & ((uint64_t) 1 << i)))
@@ -51,23 +52,23 @@ uint64_t next_subset(uint64_t * subset, uint64_t element_count, uint64_t subsize
5152
return 0;
5253
}
5354

54-
int are_submatrices_singular(unsigned char *vmatrix, int rows, int cols)
55+
int are_submatrices_singular(unsigned char *vmatrix, const uint64_t rows, const uint64_t cols)
5556
{
5657
unsigned char matrix[COLS * COLS];
5758
unsigned char invert_matrix[COLS * COLS];
58-
uint64_t row_indicator, col_indicator, subset_init, subsize;
59+
uint64_t subsize;
5960

6061
/* Check all square subsize x subsize submatrices of the rows x cols
6162
* vmatrix for singularity*/
6263
for (subsize = 1; subsize <= min(rows, cols); subsize++) {
63-
subset_init = (1 << subsize) - 1;
64-
col_indicator = subset_init;
64+
const uint64_t subset_init = (1ULL << subsize) - 1ULL;
65+
uint64_t col_indicator = subset_init;
6566
do {
66-
row_indicator = subset_init;
67+
uint64_t row_indicator = subset_init;
6768
do {
6869
gen_sub_matrix(matrix, subsize, vmatrix, rows,
6970
cols, row_indicator, col_indicator);
70-
if (gf_invert_matrix(matrix, invert_matrix, subsize))
71+
if (gf_invert_matrix(matrix, invert_matrix, (int)subsize))
7172
return 1;
7273

7374
} while (next_subset(&row_indicator, rows, subsize) == 0);
@@ -80,7 +81,7 @@ int are_submatrices_singular(unsigned char *vmatrix, int rows, int cols)
8081
int main(int argc, char **argv)
8182
{
8283
unsigned char vmatrix[(ROWS + COLS) * COLS];
83-
int rows, cols;
84+
uint64_t rows, cols;
8485

8586
if (K_MAX > MAX_CHECK) {
8687
printf("K_MAX too large for this test\n");
@@ -108,7 +109,7 @@ int main(int argc, char **argv)
108109
break;
109110

110111
}
111-
printf(" k = %2d, m <= %2d \n", cols, rows + cols - 1);
112+
printf(" k = %2u, m <= %2u \n", (unsigned)cols, (unsigned)(rows + cols - 1));
112113

113114
}
114115
return 0;

erasure_code/gf_vect_dot_prod_base_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stdio.h>
3131
#include <stdlib.h>
3232
#include <string.h> // for memset, memcmp
33+
#include <assert.h>
3334
#include "erasure_code.h"
3435
#include "test.h"
3536

@@ -134,8 +135,7 @@ int main(int argc, char *argv[])
134135
// Pick a first test
135136
m = 9;
136137
k = 5;
137-
if (m > MMAX || k > KMAX)
138-
return -1;
138+
assert(!(m > MMAX || k > KMAX));
139139

140140
gf_gen_cauchy1_matrix(a, m, k);
141141

0 commit comments

Comments
 (0)