From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 197911F56C for ; Sat, 9 Sep 2023 12:01:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1694260904; bh=4nsGNrovhefe/Lqlt+miY591zJxmS6nQAlMSKr9J8ug=; h=From:To:Subject:Date:In-Reply-To:References:From; b=pAyQmmPOenXTSZb6PwO+p5Ir63mu76zIQxpf9S+WAOytq+Svx+Um5Q4dqOUxo2q6h 0fGdAHPpcp+JkwYn4a6tqmPTV12hvsqYCtdU1Gfl+X9RbsSb1BqCykw+4rrgeOnvy2 hODOLChy1OL5x34BSIaJ265+JVk5pvR84uH29uvM= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 8/8] xap_helper: clamp workers to USHRT_MAX Date: Sat, 9 Sep 2023 12:01:42 +0000 Message-ID: <20230909120142.1041752-9-e@80x24.org> In-Reply-To: <20230909120142.1041752-1-e@80x24.org> References: <20230909120142.1041752-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This allows us to avoid any integer overflow problems while having enough room to grow for some future hardware, though it looks like having hundreds of cores isn't ever going to make it to typical servers nor workstations. --- lib/PublicInbox/xap_helper.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/PublicInbox/xap_helper.h b/lib/PublicInbox/xap_helper.h index add2fe8c..7210c940 100644 --- a/lib/PublicInbox/xap_helper.h +++ b/lib/PublicInbox/xap_helper.h @@ -78,6 +78,7 @@ static FILE *orig_err = stderr; static int orig_err_fd = -1; static void *srch_tree; // tsearch + tdelete + twalk static pid_t *worker_pids; // nr => pid +#define WORKER_MAX USHRT_MAX static unsigned long nworker, nworker_hwm; static int pipefds[2]; @@ -1063,6 +1064,10 @@ static void do_sigchld(void) static void do_sigttin(void) { if (!alive) return; + if (nworker >= WORKER_MAX) { + warnx("workers cannot exceed %zu", (size_t)WORKER_MAX); + return; + } void *p = reallocarray(worker_pids, nworker + 1, sizeof(pid_t)); if (!p) { warn("reallocarray"); @@ -1117,7 +1122,7 @@ int main(int argc, char *argv[]) #ifdef _SC_NPROCESSORS_ONLN long j = sysconf(_SC_NPROCESSORS_ONLN); if (j > 0) - nworker = j > UCHAR_MAX ? UCHAR_MAX : j; + nworker = j > WORKER_MAX ? WORKER_MAX : j; #endif // _SC_NPROCESSORS_ONLN // make warn/warnx/err multi-process friendly: @@ -1130,7 +1135,7 @@ int main(int argc, char *argv[]) switch (c) { case 'j': nworker = strtoul(optarg, &end, 10); - if (*end != 0 || nworker > USHRT_MAX) + if (*end != 0 || nworker > WORKER_MAX) errx(EXIT_FAILURE, "-j %s invalid", optarg); break; case ':':