Commit b8221e7d75d2bc74782cd1490a41ada2decef1ba

Authored by KMG
2 parents 9e6b3483 a21b2733
Exists in master

Merged in dalgaaf/jerasure/wip-da-coverity-rebase (pull request #21)

[UPDATED][Rebased #8] Fixes for SCA issues
Examples/decoder.c
... ... @@ -62,6 +62,7 @@ same arguments, and encoder.c does error check.
62 62 #include <stdlib.h>
63 63 #include <string.h>
64 64 #include <time.h>
  65 +#include <unistd.h>
65 66 #include <sys/time.h>
66 67 #include <sys/stat.h>
67 68 #include <signal.h>
... ...
Examples/encoder.c
... ... @@ -59,6 +59,7 @@ is the file name with &quot;_k#&quot; or &quot;_m#&quot; and then the extension.
59 59 #include <time.h>
60 60 #include <sys/time.h>
61 61 #include <sys/stat.h>
  62 +#include <unistd.h>
62 63 #include <string.h>
63 64 #include <stdio.h>
64 65 #include <stdlib.h>
... ...
Examples/reed_sol_time_gf.c
... ... @@ -44,6 +44,7 @@
44 44 Revision 1.0 - 2007: James S. Plank.
45 45 */
46 46  
  47 +#include <sys/time.h>
47 48 #include <stdio.h>
48 49 #include <stdlib.h>
49 50 #include <string.h>
... ...
src/jerasure.c
... ... @@ -245,6 +245,12 @@ int jerasure_matrix_decode(int k, int m, int w, int *matrix, int row_k_ones, int
245 245  
246 246 if (edd > 0) {
247 247 tmpids = talloc(int, k);
  248 + if (!tmpids) {
  249 + free(erased);
  250 + free(dm_ids);
  251 + free(decoding_matrix);
  252 + return -1;
  253 + }
248 254 for (i = 0; i < k; i++) {
249 255 tmpids[i] = (i < lastdrive) ? i : i+1;
250 256 }
... ... @@ -273,9 +279,11 @@ int *jerasure_matrix_to_bitmatrix(int k, int m, int w, int *matrix)
273 279 int *bitmatrix;
274 280 int rowelts, rowindex, colindex, elt, i, j, l, x;
275 281  
276   - bitmatrix = talloc(int, k*m*w*w);
277 282 if (matrix == NULL) { return NULL; }
278 283  
  284 + bitmatrix = talloc(int, k*m*w*w);
  285 + if (!bitmatrix) return NULL;
  286 +
279 287 rowelts = k * w;
280 288 rowindex = 0;
281 289  
... ... @@ -695,6 +703,12 @@ int jerasure_bitmatrix_decode(int k, int m, int w, int *bitmatrix, int row_k_one
695 703  
696 704 if (edd > 0) {
697 705 tmpids = talloc(int, k);
  706 + if (!tmpids) {
  707 + free(erased);
  708 + free(dm_ids);
  709 + free(decoding_matrix);
  710 + return -1;
  711 + }
698 712 for (i = 0; i < k; i++) {
699 713 tmpids[i] = (i < lastdrive) ? i : i+1;
700 714 }
... ... @@ -746,6 +760,10 @@ static char **set_up_ptrs_for_scheduled_decoding(int k, int m, int *erasures, ch
746 760 */
747 761  
748 762 ptrs = talloc(char *, k+m);
  763 + if (!ptrs) {
  764 + free(erased);
  765 + return NULL;
  766 + }
749 767  
750 768 j = k;
751 769 x = k;
... ... @@ -835,9 +853,18 @@ static int **jerasure_generate_decoding_schedule(int k, int m, int w, int *bitma
835 853 }
836 854  
837 855 row_ids = talloc(int, k+m);
  856 + if (!row_ids) return NULL;
838 857 ind_to_row = talloc(int, k+m);
  858 + if (!ind_to_row) {
  859 + free(row_ids);
  860 + return NULL;
  861 + }
839 862  
840   - if (set_up_ids_for_scheduled_decoding(k, m, erasures, row_ids, ind_to_row) < 0) return NULL;
  863 + if (set_up_ids_for_scheduled_decoding(k, m, erasures, row_ids, ind_to_row) < 0) {
  864 + free(row_ids);
  865 + free(ind_to_row);
  866 + return NULL;
  867 + }
841 868  
842 869 /* Now, we're going to create one decoding matrix which is going to
843 870 decode everything with one call. The hope is that the scheduler
... ... @@ -845,6 +872,11 @@ static int **jerasure_generate_decoding_schedule(int k, int m, int w, int *bitma
845 872 number of erasures (ddf+cdf) */
846 873  
847 874 real_decoding_matrix = talloc(int, k*w*(cdf+ddf)*w);
  875 + if (!real_decoding_matrix) {
  876 + free(row_ids);
  877 + free(ind_to_row);
  878 + return NULL;
  879 + }
848 880  
849 881 /* First, if any data drives have failed, then initialize the first
850 882 ddf*w rows of the decoding matrix from the standard decoding
... ... @@ -853,6 +885,11 @@ static int **jerasure_generate_decoding_schedule(int k, int m, int w, int *bitma
853 885 if (ddf > 0) {
854 886  
855 887 decoding_matrix = talloc(int, k*k*w*w);
  888 + if (!decoding_matrix) {
  889 + free(row_ids);
  890 + free(ind_to_row);
  891 + return NULL;
  892 + }
856 893 ptr = decoding_matrix;
857 894 for (i = 0; i < k; i++) {
858 895 if (row_ids[i] == i) {
... ... @@ -866,6 +903,12 @@ static int **jerasure_generate_decoding_schedule(int k, int m, int w, int *bitma
866 903 ptr += (k*w*w);
867 904 }
868 905 inverse = talloc(int, k*k*w*w);
  906 + if (!inverse) {
  907 + free(row_ids);
  908 + free(ind_to_row);
  909 + free(decoding_matrix);
  910 + return NULL;
  911 + }
869 912 jerasure_invert_bitmatrix(decoding_matrix, inverse, k*w);
870 913  
871 914 /* printf("\nMatrix to invert\n");
... ... @@ -1207,6 +1250,7 @@ int **jerasure_dumb_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
1207 1250 int index, optodo, i, j;
1208 1251  
1209 1252 operations = talloc(int *, k*m*w*w+1);
  1253 + if (!operations) return NULL;
1210 1254 op = 0;
1211 1255  
1212 1256 index = 0;
... ... @@ -1215,6 +1259,10 @@ int **jerasure_dumb_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
1215 1259 for (j = 0; j < k*w; j++) {
1216 1260 if (bitmatrix[index]) {
1217 1261 operations[op] = talloc(int, 5);
  1262 + if (!operations[op]) {
  1263 + // -ENOMEM
  1264 + goto error;
  1265 + }
1218 1266 operations[op][4] = optodo;
1219 1267 operations[op][0] = j/w;
1220 1268 operations[op][1] = j%w;
... ... @@ -1228,8 +1276,19 @@ int **jerasure_dumb_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
1228 1276 }
1229 1277 }
1230 1278 operations[op] = talloc(int, 5);
  1279 + if (!operations[op]) {
  1280 + // -ENOMEM
  1281 + goto error;
  1282 + }
1231 1283 operations[op][0] = -1;
1232 1284 return operations;
  1285 +
  1286 +error:
  1287 + for (i = 0; i <= op; i++) {
  1288 + free(operations[op]);
  1289 + }
  1290 + free(operations);
  1291 + return NULL;
1233 1292 }
1234 1293  
1235 1294 int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
... ... @@ -1246,12 +1305,35 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
1246 1305 jerasure_print_bitmatrix(bitmatrix, m*w, k*w, w); */
1247 1306  
1248 1307 operations = talloc(int *, k*m*w*w+1);
  1308 + if (!operations) return NULL;
1249 1309 op = 0;
1250 1310  
1251 1311 diff = talloc(int, m*w);
  1312 + if (!diff) {
  1313 + free(operations);
  1314 + return NULL;
  1315 + }
1252 1316 from = talloc(int, m*w);
  1317 + if (!from) {
  1318 + free(operations);
  1319 + free(diff);
  1320 + return NULL;
  1321 + }
1253 1322 flink = talloc(int, m*w);
  1323 + if (!flink) {
  1324 + free(operations);
  1325 + free(diff);
  1326 + free(from);
  1327 + return NULL;
  1328 + }
1254 1329 blink = talloc(int, m*w);
  1330 + if (!blink) {
  1331 + free(operations);
  1332 + free(diff);
  1333 + free(from);
  1334 + free(flink);
  1335 + return NULL;
  1336 + }
1255 1337  
1256 1338 ptr = bitmatrix;
1257 1339  
... ... @@ -1295,6 +1377,7 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
1295 1377 for (j = 0; j < k*w; j++) {
1296 1378 if (ptr[j]) {
1297 1379 operations[op] = talloc(int, 5);
  1380 + if (!operations[op]) goto error;
1298 1381 operations[op][4] = optodo;
1299 1382 operations[op][0] = j/w;
1300 1383 operations[op][1] = j%w;
... ... @@ -1306,6 +1389,7 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
1306 1389 }
1307 1390 } else {
1308 1391 operations[op] = talloc(int, 5);
  1392 + if (!operations[op]) goto error;
1309 1393 operations[op][4] = 0;
1310 1394 operations[op][0] = k+from[row]/w;
1311 1395 operations[op][1] = from[row]%w;
... ... @@ -1316,6 +1400,7 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
1316 1400 for (j = 0; j < k*w; j++) {
1317 1401 if (ptr[j] ^ b1[j]) {
1318 1402 operations[op] = talloc(int, 5);
  1403 + if (!operations[op]) goto error;
1319 1404 operations[op][4] = 1;
1320 1405 operations[op][0] = j/w;
1321 1406 operations[op][1] = j%w;
... ... @@ -1343,6 +1428,7 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
1343 1428 }
1344 1429  
1345 1430 operations[op] = talloc(int, 5);
  1431 + if (!operations[op]) goto error;
1346 1432 operations[op][0] = -1;
1347 1433 free(from);
1348 1434 free(diff);
... ... @@ -1350,6 +1436,17 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix)
1350 1436 free(flink);
1351 1437  
1352 1438 return operations;
  1439 +
  1440 +error:
  1441 + for (i = 0; i <= op; i++) {
  1442 + free(operations[op]);
  1443 + }
  1444 + free(operations);
  1445 + free(from);
  1446 + free(diff);
  1447 + free(blink);
  1448 + free(flink);
  1449 + return NULL;
1353 1450 }
1354 1451  
1355 1452 void jerasure_bitmatrix_encode(int k, int m, int w, int *bitmatrix,
... ...