Skip to content

Commit c768b61

Browse files
author
Martin Vogel
committed
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.
1 parent 8fbdb0f commit c768b61

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎src/watcher/watcher.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ cbm_watcher_t *cbm_watcher_new(cbm_store_t *store, cbm_index_fn index_fn, void *
238238
w->index_fn = index_fn;
239239
w->user_data = user_data;
240240
w->projects = cbm_ht_create(CBM_SZ_32);
241+
if (!w->projects) {
242+
free(w);
243+
return NULL;
244+
}
241245
cbm_mutex_init(&w->projects_lock);
242246
atomic_init(&w->stopped, 0);
243247
return w;
@@ -319,13 +323,13 @@ void cbm_watcher_touch(cbm_watcher_t *w, const char *project_name) {
319323
cbm_mutex_unlock(&w->projects_lock);
320324
}
321325

322-
int cbm_watcher_watch_count(const cbm_watcher_t *w) {
326+
int cbm_watcher_watch_count(cbm_watcher_t *w) {
323327
if (!w) {
324328
return 0;
325329
}
326-
cbm_mutex_lock(&((cbm_watcher_t *)w)->projects_lock);
330+
cbm_mutex_lock(&w->projects_lock);
327331
int count = (int)cbm_ht_count(w->projects);
328-
cbm_mutex_unlock(&((cbm_watcher_t *)w)->projects_lock);
332+
cbm_mutex_unlock(&w->projects_lock);
329333
return count;
330334
}
331335

‎src/watcher/watcher.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void cbm_watcher_stop(cbm_watcher_t *w);
6464
/* ── Introspection (for testing) ────────────────────────────────── */
6565

6666
/* Return the number of projects in the watch list. */
67-
int cbm_watcher_watch_count(const cbm_watcher_t *w);
67+
int cbm_watcher_watch_count(cbm_watcher_t *w);
6868

6969
/* Return the adaptive poll interval (ms) for a given file count. */
7070
int cbm_watcher_poll_interval_ms(int file_count);

0 commit comments

Comments
 (0)