Browse code

Check return value from allocations in choices.c

Co-authored-by: mooon04 <mirae.kim040506@gmail.com>

John Hawthorn authored on 27/07/2025 07:00:58
Showing 1 changed files

... ...
@@ -265,6 +265,10 @@ void choices_search(choices_t *c, const char *search) {
265 265
 	choices_reset_search(c);
266 266
 
267 267
 	struct search_job *job = calloc(1, sizeof(struct search_job));
268
+	if (!job) {
269
+		fprintf(stderr, "Error: Can't allocate memory\n");
270
+		abort();
271
+	}
268 272
 	job->search = search;
269 273
 	job->choices = c;
270 274
 	if (pthread_mutex_init(&job->lock, NULL) != 0) {
... ...
@@ -272,6 +276,10 @@ void choices_search(choices_t *c, const char *search) {
272 276
 		abort();
273 277
 	}
274 278
 	job->workers = calloc(c->worker_count, sizeof(struct worker));
279
+	if (!job->workers) {
280
+		fprintf(stderr, "Error: Can't allocate memory\n");
281
+		abort();
282
+	}
275 283
 
276 284
 	struct worker *workers = job->workers;
277 285
 	for (int i = c->worker_count - 1; i >= 0; i--) {
... ...
@@ -279,6 +287,10 @@ void choices_search(choices_t *c, const char *search) {
279 287
 		workers[i].worker_num = i;
280 288
 		workers[i].result.size = 0;
281 289
 		workers[i].result.list = malloc(c->size * sizeof(struct scored_result)); /* FIXME: This is overkill */
290
+		if (!workers[i].result.list) {
291
+			fprintf(stderr, "Error: Can't allocate memory\n");
292
+			abort();
293
+		}
282 294
 
283 295
 		/* These must be created last-to-first to avoid a race condition when fanning in */
284 296
 		if ((errno = pthread_create(&workers[i].thread_id, NULL, &choices_search_worker, &workers[i]))) {