Skip to content

Commit 684e35b

Browse files
authored
Merge pull request DeusData#562 from DeusData/fix/493-gitignore-merge-atomic
fix(discover): make cbm_gitignore_merge atomic on allocation failure
2 parents acb7eaa + 5ea5579 commit 684e35b

4 files changed

Lines changed: 78 additions & 9 deletions

File tree

‎src/discover/discover.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,10 @@ int cbm_discover_ex(const char *repo_path, const cbm_discover_opts_t *opts, cbm_
563563
if (!gitignore) {
564564
gitignore = git_exclude;
565565
} else {
566-
cbm_gitignore_merge(gitignore, git_exclude);
566+
/* On allocation failure the merge is atomic (dst unchanged), so
567+
* the .gitignore patterns still apply; the exclude patterns are
568+
* simply skipped — same as if .git/info/exclude were absent. */
569+
(void)cbm_gitignore_merge(gitignore, git_exclude);
567570
cbm_gitignore_free(git_exclude);
568571
}
569572
}

‎src/discover/discover.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ void cbm_gitignore_free(cbm_gitignore_t *gi);
6060

6161
/* Append all patterns from src into dst. dst takes ownership of deep copies
6262
* of each src pattern; src is unchanged and must still be freed by the caller.
63-
* NULL-safe on either argument. */
64-
void cbm_gitignore_merge(cbm_gitignore_t *dst, const cbm_gitignore_t *src);
63+
* NULL-safe on either argument.
64+
* Returns true on success (or when there is nothing to merge). Returns false on
65+
* allocation failure, in which case dst is left exactly as it was (atomic) — no
66+
* partial merge — so a failed merge degrades to "as if src was absent". */
67+
bool cbm_gitignore_merge(cbm_gitignore_t *dst, const cbm_gitignore_t *src);
6568

6669
/* ── Directory skip / suffix filters ─────────────────────────────── */
6770

‎src/discover/gitignore.c‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,28 +380,46 @@ void cbm_gitignore_free(cbm_gitignore_t *gi) {
380380
free(gi);
381381
}
382382

383-
void cbm_gitignore_merge(cbm_gitignore_t *dst, const cbm_gitignore_t *src) {
384-
if (!dst || !src || src->count == 0) {
385-
return;
383+
/* Test seam: lets a unit test simulate strdup() failure mid-merge so the
384+
* atomic-rollback path can be exercised without real OOM. NULL = use strdup. */
385+
char *(*cbm_gitignore_merge_dup_hook_for_test)(const char *) = NULL;
386+
387+
bool cbm_gitignore_merge(cbm_gitignore_t *dst, const cbm_gitignore_t *src) {
388+
if (!dst) {
389+
return false;
390+
}
391+
if (!src || src->count == 0) {
392+
return true; /* nothing to merge */
386393
}
387394
int needed = dst->count + src->count;
388395
if (needed > dst->capacity) {
389396
gi_pattern_t *grown = realloc(dst->patterns, (size_t)needed * sizeof(gi_pattern_t));
390397
if (!grown) {
391-
return;
398+
return false; /* dst left unchanged */
392399
}
393400
dst->patterns = grown;
394401
dst->capacity = needed;
395402
}
403+
int start_count = dst->count;
396404
for (int i = 0; i < src->count; i++) {
397-
char *pat = strdup(src->patterns[i].pattern);
405+
char *pat = cbm_gitignore_merge_dup_hook_for_test
406+
? cbm_gitignore_merge_dup_hook_for_test(src->patterns[i].pattern)
407+
: strdup(src->patterns[i].pattern);
398408
if (!pat) {
399-
return;
409+
/* Roll back partial copies so dst is unchanged on failure (atomic
410+
* merge). A silent partial merge could drop the very exclude
411+
* pattern the caller relied on while keeping others. */
412+
for (int j = start_count; j < dst->count; j++) {
413+
free(dst->patterns[j].pattern);
414+
}
415+
dst->count = start_count;
416+
return false;
400417
}
401418
dst->patterns[dst->count].pattern = pat;
402419
dst->patterns[dst->count].negated = src->patterns[i].negated;
403420
dst->patterns[dst->count].dir_only = src->patterns[i].dir_only;
404421
dst->patterns[dst->count].rooted = src->patterns[i].rooted;
405422
dst->count++;
406423
}
424+
return true;
407425
}

‎tests/test_gitignore.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../src/foundation/compat.h"
77
#include "test_framework.h"
88
#include "discover/discover.h"
9+
#include <string.h> /* strdup (test seam) */
910

1011
/* ── Basic pattern matching ────────────────────────────────────── */
1112

@@ -230,6 +231,49 @@ TEST(gi_merge_null_safe) {
230231
PASS();
231232
}
232233

234+
/* Reproduce-first guard for #493: when an allocation fails mid-merge,
235+
* cbm_gitignore_merge must leave dst exactly as it was (atomic) and signal
236+
* failure — never a partial merge that keeps some src patterns and drops
237+
* others. The seam below injects a strdup failure on the 2nd src pattern.
238+
* Without the atomic rollback this test is RED: the first src pattern ("a/")
239+
* leaks into dst, so `matches(dst, "a", true)` is true. */
240+
extern char *(*cbm_gitignore_merge_dup_hook_for_test)(const char *);
241+
242+
static int gi_dup_calls;
243+
static int gi_dup_fail_at;
244+
static char *gi_failing_dup(const char *s) {
245+
if (++gi_dup_calls > gi_dup_fail_at) {
246+
return NULL;
247+
}
248+
return strdup(s);
249+
}
250+
251+
TEST(gi_merge_atomic_on_alloc_failure) {
252+
cbm_gitignore_t *dst = cbm_gitignore_parse("*.log\n"); /* 1 pattern */
253+
cbm_gitignore_t *src = cbm_gitignore_parse("a/\nb/\nc/\n"); /* 3 patterns */
254+
ASSERT_NOT_NULL(dst);
255+
ASSERT_NOT_NULL(src);
256+
257+
gi_dup_calls = 0;
258+
gi_dup_fail_at = 1; /* first src pattern copies; second fails */
259+
cbm_gitignore_merge_dup_hook_for_test = gi_failing_dup;
260+
261+
bool ok = cbm_gitignore_merge(dst, src);
262+
263+
cbm_gitignore_merge_dup_hook_for_test = NULL; /* restore for other tests */
264+
265+
ASSERT_FALSE(ok); /* failure is signalled, not silent */
266+
/* dst unchanged: its own pattern still matches, and the partially copied
267+
* src patterns were rolled back. */
268+
ASSERT_TRUE(cbm_gitignore_matches(dst, "x.log", false));
269+
ASSERT_FALSE(cbm_gitignore_matches(dst, "a", true));
270+
ASSERT_FALSE(cbm_gitignore_matches(dst, "b", true));
271+
272+
cbm_gitignore_free(src);
273+
cbm_gitignore_free(dst);
274+
PASS();
275+
}
276+
233277
/* ── Suite ─────────────────────────────────────────────────────── */
234278

235279
SUITE(gitignore) {
@@ -253,4 +297,5 @@ SUITE(gitignore) {
253297
RUN_TEST(gi_merge_patterns);
254298
RUN_TEST(gi_merge_into_empty);
255299
RUN_TEST(gi_merge_null_safe);
300+
RUN_TEST(gi_merge_atomic_on_alloc_failure);
256301
}

0 commit comments

Comments
 (0)