fix(watcher): close residual gaps in #207

Follow-up to #207:
- cbm_watcher_new: NULL-check cbm_ht_create() and free w on OOM so
  later watch/unwatch/touch don't dereference a NULL hash table.
- cbm_watcher_watch_count: drop const from the signature and remove
  the const_cast around projects_lock. The function necessarily
  touches synchronization primitives so a const receiver was
  misleading; all current callers pass a non-const pointer.
This commit is contained in:
Martin Vogel
2026-05-08 21:33:06 +02:00
parent 8fbdb0fa20
commit c768b6154d
2 changed files with 8 additions and 4 deletions
+7 -3
View File
@@ -238,6 +238,10 @@ cbm_watcher_t *cbm_watcher_new(cbm_store_t *store, cbm_index_fn index_fn, void *
w->index_fn = index_fn;
w->user_data = user_data;
w->projects = cbm_ht_create(CBM_SZ_32);
if (!w->projects) {
free(w);
return NULL;
}
cbm_mutex_init(&w->projects_lock);
atomic_init(&w->stopped, 0);
return w;
@@ -319,13 +323,13 @@ void cbm_watcher_touch(cbm_watcher_t *w, const char *project_name) {
cbm_mutex_unlock(&w->projects_lock);
}
int cbm_watcher_watch_count(const cbm_watcher_t *w) {
int cbm_watcher_watch_count(cbm_watcher_t *w) {
if (!w) {
return 0;
}
cbm_mutex_lock(&((cbm_watcher_t *)w)->projects_lock);
cbm_mutex_lock(&w->projects_lock);
int count = (int)cbm_ht_count(w->projects);
cbm_mutex_unlock(&((cbm_watcher_t *)w)->projects_lock);
cbm_mutex_unlock(&w->projects_lock);
return count;
}
+1 -1
View File
@@ -64,7 +64,7 @@ void cbm_watcher_stop(cbm_watcher_t *w);
/* ── Introspection (for testing) ────────────────────────────────── */
/* Return the number of projects in the watch list. */
int cbm_watcher_watch_count(const cbm_watcher_t *w);
int cbm_watcher_watch_count(cbm_watcher_t *w);
/* Return the adaptive poll interval (ms) for a given file count. */
int cbm_watcher_poll_interval_ms(int file_count);