Skip to content
Myr

match: fix false-positive use-after-free after failed reallocarray()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

With GCC 14.2.0 on Debian sid, fuzzel fails to build [1] on some non-x86 archs [2] due to a false-positive use-after-free error for free(tokens) or free(tok_lengths), in the case where the corresponding reallocarray() call fails. Revise error handling to free() immediately after a failing reallocarray(), so that GCC follows that the calls to free() are valid. [1] https://buildd.debian.org/status/fetch.php?pkg=fuzzel&arch=riscv64&ver=1.11.1%2Bds-1&stamp=1726441493&raw=0 [2] loong64, m68k, mips64el, powerpc, ppc64, ppc64el, riscv64, and s390x.

authored by Peter Colberg · committed by Peter Colberg · parent 7219296 · Browse files · 1 file+13 added−23 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+13 added−23 removed

  • other:+13 added−23 removed

1 file+13 added−23 removed

modified:match.c+13 added-23 removed
1453 unmodified lines
1454 size_t *tok_lengths = malloc(sizeof(tok_lengths[0]));14551456 if (copy == NULL || tokens == NULL || tok_lengths == NULL) {1457 LOG_ERR("failed to allocate tokens");1458 free(copy);1459 free(tokens);1460 free(tok_lengths);1461 goto unlock_and_return;1458 goto free_unlock_and_return;1459 }14601461 size_t tok_count = 1;1462 tokens[0] = copy;
14 unmodified lines
1477 tok_count++;14781479 char32_t **new_tokens = reallocarray(1480 tokens, tok_count, sizeof(tokens[0]));1481 if (new_tokens == NULL)1482 goto free_unlock_and_return;1483 tokens = new_tokens;14841485 size_t *new_tok_lengths = reallocarray(1486 tok_lengths, tok_count, sizeof(tok_lengths[0]));14861487 if (new_tokens == NULL || new_tok_lengths == NULL) {1488 if (new_tokens != NULL)1489 free(new_tokens);1490 else1491 free(tokens);1492 if (new_tok_lengths != NULL)1493 free(new_tok_lengths);1494 else1495 free(tok_lengths);1496 free(copy);1497 goto unlock_and_return;1498 }14991500 tokens = new_tokens;1487 if (new_tok_lengths == NULL)1488 goto free_unlock_and_return;1489 tok_lengths = new_tok_lengths;14901491 tokens[tok_count - 1] = p + 1;1492 tok_lengths[tok_count - 1] = 0;1493 }1494 }
84 unmodified lines
1579 matches->workers.tokens = NULL;1580 matches->workers.tok_lengths = NULL;1581 }15821594 free(tok_lengths);1595 free(tokens);1596 free(copy);15971583 LOG_DBG("match update done");15841585 /* Sort */1586 if (matches->sort_result) {
8 unmodified lines
15951596 if (matches->selected >= matches->match_count && matches->selected > 0)1597 matches->selected = matches->match_count - 1;15981599free_unlock_and_return:1600 free(tok_lengths);1601 free(tokens);1602 free(copy);16031604unlock_and_return:1605 matches_unlock(matches);1606}1607
69 unmodified lines