Commit a21b2733a964bcb3649d78bb7afd23c15dce3d96
1 parent
31810e1f
Exists in
master
jerasure.c: add more checks for talloc/malloc results
Signed-off-by: Danny Al-Gaaf <danny.al-gaaf@bisect.de>
Showing
1 changed file
with
91 additions
and
0 deletions
Show diff stats
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 | } |
... | ... | @@ -697,6 +703,12 @@ int jerasure_bitmatrix_decode(int k, int m, int w, int *bitmatrix, int row_k_one |
697 | 703 | |
698 | 704 | if (edd > 0) { |
699 | 705 | tmpids = talloc(int, k); |
706 | + if (!tmpids) { | |
707 | + free(erased); | |
708 | + free(dm_ids); | |
709 | + free(decoding_matrix); | |
710 | + return -1; | |
711 | + } | |
700 | 712 | for (i = 0; i < k; i++) { |
701 | 713 | tmpids[i] = (i < lastdrive) ? i : i+1; |
702 | 714 | } |
... | ... | @@ -748,6 +760,10 @@ static char **set_up_ptrs_for_scheduled_decoding(int k, int m, int *erasures, ch |
748 | 760 | */ |
749 | 761 | |
750 | 762 | ptrs = talloc(char *, k+m); |
763 | + if (!ptrs) { | |
764 | + free(erased); | |
765 | + return NULL; | |
766 | + } | |
751 | 767 | |
752 | 768 | j = k; |
753 | 769 | x = k; |
... | ... | @@ -837,7 +853,12 @@ static int **jerasure_generate_decoding_schedule(int k, int m, int w, int *bitma |
837 | 853 | } |
838 | 854 | |
839 | 855 | row_ids = talloc(int, k+m); |
856 | + if (!row_ids) return NULL; | |
840 | 857 | ind_to_row = talloc(int, k+m); |
858 | + if (!ind_to_row) { | |
859 | + free(row_ids); | |
860 | + return NULL; | |
861 | + } | |
841 | 862 | |
842 | 863 | if (set_up_ids_for_scheduled_decoding(k, m, erasures, row_ids, ind_to_row) < 0) { |
843 | 864 | free(row_ids); |
... | ... | @@ -851,6 +872,11 @@ static int **jerasure_generate_decoding_schedule(int k, int m, int w, int *bitma |
851 | 872 | number of erasures (ddf+cdf) */ |
852 | 873 | |
853 | 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 | + } | |
854 | 880 | |
855 | 881 | /* First, if any data drives have failed, then initialize the first |
856 | 882 | ddf*w rows of the decoding matrix from the standard decoding |
... | ... | @@ -859,6 +885,11 @@ static int **jerasure_generate_decoding_schedule(int k, int m, int w, int *bitma |
859 | 885 | if (ddf > 0) { |
860 | 886 | |
861 | 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 | + } | |
862 | 893 | ptr = decoding_matrix; |
863 | 894 | for (i = 0; i < k; i++) { |
864 | 895 | if (row_ids[i] == i) { |
... | ... | @@ -872,6 +903,12 @@ static int **jerasure_generate_decoding_schedule(int k, int m, int w, int *bitma |
872 | 903 | ptr += (k*w*w); |
873 | 904 | } |
874 | 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 | + } | |
875 | 912 | jerasure_invert_bitmatrix(decoding_matrix, inverse, k*w); |
876 | 913 | |
877 | 914 | /* printf("\nMatrix to invert\n"); |
... | ... | @@ -1213,6 +1250,7 @@ int **jerasure_dumb_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
1213 | 1250 | int index, optodo, i, j; |
1214 | 1251 | |
1215 | 1252 | operations = talloc(int *, k*m*w*w+1); |
1253 | + if (!operations) return NULL; | |
1216 | 1254 | op = 0; |
1217 | 1255 | |
1218 | 1256 | index = 0; |
... | ... | @@ -1221,6 +1259,10 @@ int **jerasure_dumb_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
1221 | 1259 | for (j = 0; j < k*w; j++) { |
1222 | 1260 | if (bitmatrix[index]) { |
1223 | 1261 | operations[op] = talloc(int, 5); |
1262 | + if (!operations[op]) { | |
1263 | + // -ENOMEM | |
1264 | + goto error; | |
1265 | + } | |
1224 | 1266 | operations[op][4] = optodo; |
1225 | 1267 | operations[op][0] = j/w; |
1226 | 1268 | operations[op][1] = j%w; |
... | ... | @@ -1234,8 +1276,19 @@ int **jerasure_dumb_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
1234 | 1276 | } |
1235 | 1277 | } |
1236 | 1278 | operations[op] = talloc(int, 5); |
1279 | + if (!operations[op]) { | |
1280 | + // -ENOMEM | |
1281 | + goto error; | |
1282 | + } | |
1237 | 1283 | operations[op][0] = -1; |
1238 | 1284 | return operations; |
1285 | + | |
1286 | +error: | |
1287 | + for (i = 0; i <= op; i++) { | |
1288 | + free(operations[op]); | |
1289 | + } | |
1290 | + free(operations); | |
1291 | + return NULL; | |
1239 | 1292 | } |
1240 | 1293 | |
1241 | 1294 | int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
... | ... | @@ -1252,12 +1305,35 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
1252 | 1305 | jerasure_print_bitmatrix(bitmatrix, m*w, k*w, w); */ |
1253 | 1306 | |
1254 | 1307 | operations = talloc(int *, k*m*w*w+1); |
1308 | + if (!operations) return NULL; | |
1255 | 1309 | op = 0; |
1256 | 1310 | |
1257 | 1311 | diff = talloc(int, m*w); |
1312 | + if (!diff) { | |
1313 | + free(operations); | |
1314 | + return NULL; | |
1315 | + } | |
1258 | 1316 | from = talloc(int, m*w); |
1317 | + if (!from) { | |
1318 | + free(operations); | |
1319 | + free(diff); | |
1320 | + return NULL; | |
1321 | + } | |
1259 | 1322 | flink = talloc(int, m*w); |
1323 | + if (!flink) { | |
1324 | + free(operations); | |
1325 | + free(diff); | |
1326 | + free(from); | |
1327 | + return NULL; | |
1328 | + } | |
1260 | 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 | + } | |
1261 | 1337 | |
1262 | 1338 | ptr = bitmatrix; |
1263 | 1339 | |
... | ... | @@ -1301,6 +1377,7 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
1301 | 1377 | for (j = 0; j < k*w; j++) { |
1302 | 1378 | if (ptr[j]) { |
1303 | 1379 | operations[op] = talloc(int, 5); |
1380 | + if (!operations[op]) goto error; | |
1304 | 1381 | operations[op][4] = optodo; |
1305 | 1382 | operations[op][0] = j/w; |
1306 | 1383 | operations[op][1] = j%w; |
... | ... | @@ -1312,6 +1389,7 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
1312 | 1389 | } |
1313 | 1390 | } else { |
1314 | 1391 | operations[op] = talloc(int, 5); |
1392 | + if (!operations[op]) goto error; | |
1315 | 1393 | operations[op][4] = 0; |
1316 | 1394 | operations[op][0] = k+from[row]/w; |
1317 | 1395 | operations[op][1] = from[row]%w; |
... | ... | @@ -1322,6 +1400,7 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
1322 | 1400 | for (j = 0; j < k*w; j++) { |
1323 | 1401 | if (ptr[j] ^ b1[j]) { |
1324 | 1402 | operations[op] = talloc(int, 5); |
1403 | + if (!operations[op]) goto error; | |
1325 | 1404 | operations[op][4] = 1; |
1326 | 1405 | operations[op][0] = j/w; |
1327 | 1406 | operations[op][1] = j%w; |
... | ... | @@ -1349,6 +1428,7 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
1349 | 1428 | } |
1350 | 1429 | |
1351 | 1430 | operations[op] = talloc(int, 5); |
1431 | + if (!operations[op]) goto error; | |
1352 | 1432 | operations[op][0] = -1; |
1353 | 1433 | free(from); |
1354 | 1434 | free(diff); |
... | ... | @@ -1356,6 +1436,17 @@ int **jerasure_smart_bitmatrix_to_schedule(int k, int m, int w, int *bitmatrix) |
1356 | 1436 | free(flink); |
1357 | 1437 | |
1358 | 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; | |
1359 | 1450 | } |
1360 | 1451 | |
1361 | 1452 | void jerasure_bitmatrix_encode(int k, int m, int w, int *bitmatrix, | ... | ... |