From 8cc43dafc8f0d4ca04aa162573a44016f45ea364 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Thu, 12 Jan 2023 14:36:09 -0800 Subject: modules/compose: fix segfault introduced by 83e41d It was assumed (n_modules - n_overlayable) would give the number of non-overlayable modules appropriate as base layers. But with the skipping of hermetic and experimental modules the base_idx could be out of reach leaving layers NULL after the loop, which will segfault later when strlen() assumes it's non-NULL. This commit does the simple thing and also counts the unusable modules to subtract from those eligible for base layers along with n_overlayable. --- src/modules/compose/compose.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/modules/compose/compose.c b/src/modules/compose/compose.c index f3a1442..38ea539 100644 --- a/src/modules/compose/compose.c +++ b/src/modules/compose/compose.c @@ -171,21 +171,24 @@ static void compose_render_fragment(til_module_context_t *context, til_stream_t /* return a randomized valid layers= setting */ static char * compose_random_layers_setting(unsigned seed) { - size_t n_modules, n_rand_overlays, n_overlayable = 0, base_idx; + size_t n_modules, n_rand_overlays, n_overlayable = 0, n_unusable = 0, base_idx; char *layers = NULL; const til_module_t **modules; til_get_modules(&modules, &n_modules); for (size_t i = 0; i < n_modules; i++) { - if ((modules[i]->flags & (TIL_MODULE_HERMETIC | TIL_MODULE_EXPERIMENTAL))) + if ((modules[i]->flags & (TIL_MODULE_HERMETIC | TIL_MODULE_EXPERIMENTAL))) { + n_unusable++; + continue; + } if (modules[i]->flags & TIL_MODULE_OVERLAYABLE) n_overlayable++; } - base_idx = rand_r(&seed) % (n_modules - n_overlayable); + base_idx = rand_r(&seed) % (n_modules - (n_overlayable + n_unusable)); for (size_t i = 0, j = 0; !layers && i < n_modules; i++) { if ((modules[i]->flags & (TIL_MODULE_HERMETIC | TIL_MODULE_EXPERIMENTAL))) continue; -- cgit v1.2.3