Fix bounds check in get_fragment_partition

See also: https://github.com/openstack/liberasurecode/pull/4 and
https://github.com/openstack/liberasurecode/pull/5

Co-Authored-By: utree <aproc@qq.com>
Co-Authored-By: Martin Raiber <martin@urbackup.org>
Change-Id: I174160dc1190398b6a8bbf60bec55f72ae1bec37
This commit is contained in:
Tim Burke 2023-10-04 15:01:53 -07:00
parent cc7de356d6
commit 9c16df272a
2 changed files with 20 additions and 2 deletions

View File

@ -247,7 +247,7 @@ int get_fragment_partition(
*/ */
for (i = 0; i < num_fragments; i++) { for (i = 0; i < num_fragments; i++) {
index = get_fragment_idx(fragments[i]); index = get_fragment_idx(fragments[i]);
if (index < 0 || index > (k + m)) { if (index < 0 || index >= (k + m)) {
return -EBADHEADER; return -EBADHEADER;
} }
if (index < k) { if (index < k) {

View File

@ -957,6 +957,24 @@ static void test_get_fragment_partition()
free(avail_frags); free(avail_frags);
free(missing); free(missing);
missing = alloc_and_set_buffer(sizeof(char*) * null_args.k, -1);
num_avail_frags = create_frags_array(&avail_frags, encoded_data,
encoded_parity, &null_args, skips);
/* Passing k+m frags for a k+(m-1) policy, we should notice the
* too-high frag index and call its header "bad".
*/
rc = get_fragment_partition(null_args.k, null_args.m - 1, avail_frags, num_avail_frags,
encoded_data, encoded_parity, missing);
assert(-EBADHEADER == rc);
for(i = 0; i < null_args.m; i++) assert(missing[i] == -1);
// Loop already pushed us one past
assert(missing[i] == -1);
free(avail_frags);
free(missing);
skips[i] = 1; skips[i] = 1;
if (i < null_args.k) { if (i < null_args.k) {
free(encoded_data[i]); free(encoded_data[i]);