Skip to content

Commit 316bce2

Browse files
committed
fix(doclinks): skip qsort on empty Perl tables; bound the Jenkins restore regex
PR #21 review follow-ups. UBSan reported "null pointer passed as argument 1" at four qsort calls in the Perl doc-link code that arrived with the step-6 upstream merge: internal/cbm/doclink_perl.c (pl_number_packages twice, pl_put_subs) and src/pipeline/doc_links_perl.c (the decl index sort). When a file has no packages, subs or declarations, the array pointer is still NULL and the count is 0. glibc declares qsort's base nonnull, so the call is undefined even with a zero count. Each call now runs only when there are at least two elements, which is also when sorting has any effect. The reports reproduce on the pipeline and doc_mentions_perl suites before this change and are gone after it; both suites pass. Upstream has the same code, so this is worth sending upstream too. tests/test_jenkins_daemon_recovery.py: a4c0a8f let the post/always pattern skip any text before the restore sh step, so with re.S it could match a sh step outside the always block. The gap is now limited to 200 characters without a quote, enough for the script/try wrapper that 9f26f16 added around cleanWs, so the extracted step must still be the first sh step inside always { }.
1 parent a4c0a8f commit 316bce2

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

‎internal/cbm/doclink_perl.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,14 +1538,18 @@ static int pl_pkg_order_cmp(const void *a, const void *b) {
15381538
/* Give every declaration the index of its name's P record (names in order of
15391539
* their first declaration) and write the P records. */
15401540
static void pl_number_packages(pl_walk_t *w, pl_sb_t *sb) {
1541-
qsort(w->pkgs, (size_t)w->npkgs, sizeof(pl_pkg_t), pl_pkg_name_cmp);
1541+
if (w->npkgs > 1) {
1542+
qsort(w->pkgs, (size_t)w->npkgs, sizeof(pl_pkg_t), pl_pkg_name_cmp);
1543+
}
15421544
for (int i = 0; i < w->npkgs; i++) { /* index = order of the name's first declaration */
15431545
bool repeat =
15441546
i > 0 && pl_name_cmp(w->pkgs[i].name, w->pkgs[i].len, w->pkgs[i - SKIP_ONE].name,
15451547
w->pkgs[i - SKIP_ONE].len) == 0;
15461548
w->pkgs[i].index = repeat ? w->pkgs[i - SKIP_ONE].index : w->pkgs[i].order;
15471549
}
1548-
qsort(w->pkgs, (size_t)w->npkgs, sizeof(pl_pkg_t), pl_pkg_order_cmp);
1550+
if (w->npkgs > 1) {
1551+
qsort(w->pkgs, (size_t)w->npkgs, sizeof(pl_pkg_t), pl_pkg_order_cmp);
1552+
}
15491553
int records = 0;
15501554
for (int i = 0; i < w->npkgs; i++) {
15511555
pl_pkg_t *p = &w->pkgs[i];
@@ -1572,7 +1576,9 @@ static int pl_subdecl_cmp(const void *a, const void *b) {
15721576
/* One S record per sub definition of the file, with the package the tree
15731577
* places it in. */
15741578
static void pl_put_subs(CBMExtractCtx *ctx, pl_walk_t *w, pl_sb_t *sb) {
1575-
qsort(w->subs, (size_t)w->nsubs, sizeof(pl_subdecl_t), pl_subdecl_cmp);
1579+
if (w->nsubs > 1) {
1580+
qsort(w->subs, (size_t)w->nsubs, sizeof(pl_subdecl_t), pl_subdecl_cmp);
1581+
}
15761582
const CBMDefArray *defs = &ctx->result->defs;
15771583
for (int i = 0; i < defs->count; i++) {
15781584
const CBMDefinition *d = &defs->items[i];

‎src/pipeline/doc_links_perl.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ static bool pr_index_packages(pr_index_t *ix, const cbm_doclink_build_in_t *in)
244244
if (ix->oom) {
245245
return false;
246246
}
247-
qsort(ix->decls, (size_t)ix->ndecls, sizeof(pr_decl_t), pr_decl_cmp);
247+
if (ix->ndecls > 1) {
248+
qsort(ix->decls, (size_t)ix->ndecls, sizeof(pr_decl_t), pr_decl_cmp);
249+
}
248250
ix->pkg = cbm_ht_create_in(CBM_MEM_CLASS_OTHER, (uint32_t)(ix->ndecls + SKIP_ONE));
249251
if (!ix->pkg) {
250252
return false;

‎tests/test_jenkins_daemon_recovery.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
pipeline = (Path(__file__).resolve().parents[1] / "Jenkinsfile").read_text()
1111
stop = re.search(r"stage\('Stop active CBM daemon'\).*?sh '''(.*?)'''", pipeline, re.S).group(1)
12-
# The always block may wrap the restore step (script/try for cleanWs), so allow
13-
# any content before the first sh step inside it.
14-
restore = re.search(r"post\s*\{\s*always\s*\{.*?sh '''(.*?)'''", pipeline, re.S).group(1)
12+
# The always block may wrap the restore step (script/try for cleanWs). Allow a
13+
# short wrapper prefix only, so the sh step must still sit inside always { }.
14+
restore = re.search(r"post\s*\{\s*always\s*\{[^']{0,200}?sh '''(.*?)'''", pipeline, re.S).group(1)
1515

1616
with tempfile.TemporaryDirectory() as directory:
1717
root = Path(directory)

0 commit comments

Comments
 (0)