Skip to content

Commit 6a343a7

Browse files
committed
test(similarity): cover the empty-graph qsort null base (DeusData#1367)
The production guard for DeusData#1367 — collect_fp_entries only sorting when count > 1 — is on main, but nothing exercises it. A fix without a test is a fix that can be silently undone, and this one is unusually easy to undo: removing the guard produces no visible symptom on most lanes. Adds the missing case: a graph with zero Function/Method nodes, where the entry array is never allocated and the null base would reach qsort. The test comment records WHERE the test binds, because that turned out to be the interesting part. Removing the guard again: macOS 25 passed (Apple libc does not mark qsort nonnull, so there is no UB to see) Linux ASan/UBSan, recovering 25 passed (prints "null pointer passed as argument 1", then keeps going) Linux UBSan, trapping exit 1 (process aborts — the RED) So the assertions here are the behavioural half (empty graph → rc 0, no SIMILAR_TO edges), and the UB half is caught by the trap-UBSan leg alone. Both were measured on ubuntu-arm64 in each direction. Spelling that out in the test keeps a future reader from checking it on a recovering lane, seeing green, and concluding the null-base guard is still there when it is not. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 0d6f26a commit 6a343a7

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

‎tests/test_simhash.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,45 @@ TEST(pass_similarity_short_funcs_skipped) {
803803
PASS();
804804
}
805805

806+
TEST(pass_similarity_empty_graph_no_entries) {
807+
/* Zero Function/Method nodes → the entry array is never allocated. The
808+
* collect phase must not hand that null base to qsort: glibc declares qsort
809+
* nonnull, so passing NULL is UB even at count 0 (#1367).
810+
*
811+
* WHERE THIS BINDS — read before "verifying" it. Remove the count > 1 guard
812+
* in collect_fp_entries and this test still PASSES on macOS (Apple libc does
813+
* not mark qsort nonnull) and on an ordinary Linux ASan/UBSan build, which
814+
* merely prints "null pointer passed as argument 1" and keeps going. It goes
815+
* RED only where UBSan TRAPS (-fno-sanitize-recover), where the process
816+
* aborts and the runner exits 1. Measured both ways on ubuntu-arm64:
817+
* recovering lane = 25 passed with AND without the guard; trapping lane =
818+
* exit 1 without it, 25 passed with it.
819+
*
820+
* The assertions below are therefore the behavioural half (an empty graph
821+
* yields rc 0 and no SIMILAR_TO edges); the UB half is caught by the
822+
* trap-UBSan leg alone. Do not read a green recovering-lane run as proof
823+
* that the null-base guard is still in place. */
824+
cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp");
825+
826+
atomic_int cancelled = 0;
827+
cbm_pipeline_ctx_t ctx = {
828+
.project_name = "test",
829+
.repo_path = "/tmp",
830+
.gbuf = gb,
831+
.registry = NULL,
832+
.cancelled = &cancelled,
833+
};
834+
835+
int rc = cbm_pipeline_pass_similarity(&ctx);
836+
ASSERT_EQ(rc, 0);
837+
838+
int sim_count = count_similar_to_edges(gb);
839+
ASSERT_EQ(sim_count, 0);
840+
841+
cbm_gbuf_free(gb);
842+
PASS();
843+
}
844+
806845
/* ═══════════════════════════════════════════════════════════════════
807846
* Suite 4: Full Pipeline Integration (generated test project)
808847
* ═══════════════════════════════════════════════════════════════════ */
@@ -1165,6 +1204,7 @@ SUITE(simhash) {
11651204
RUN_TEST(pass_similarity_edge_properties);
11661205
RUN_TEST(pass_similarity_max_edges_cap);
11671206
RUN_TEST(pass_similarity_short_funcs_skipped);
1207+
RUN_TEST(pass_similarity_empty_graph_no_entries);
11681208

11691209
/* Suite 4: Full Pipeline Integration */
11701210
RUN_TEST(pipeline_minhash_end_to_end);

0 commit comments

Comments
 (0)