From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id A4910158087 for ; Fri, 29 Oct 2021 05:37:50 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 8724AE08C3; Fri, 29 Oct 2021 05:37:49 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 6F54FE08C3 for ; Fri, 29 Oct 2021 05:37:49 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 5240C343036 for ; Fri, 29 Oct 2021 05:37:48 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 5DD33178 for ; Fri, 29 Oct 2021 05:37:46 +0000 (UTC) From: "Mike Frysinger" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Mike Frysinger" Message-ID: <1635478738.c029863b70ca77f59cd181974cfab0fa18c0a265.vapier@gentoo> Subject: [gentoo-commits] proj/sandbox:master commit in: src/ X-VCS-Repository: proj/sandbox X-VCS-Files: src/sandbox.c X-VCS-Directories: src/ X-VCS-Committer: vapier X-VCS-Committer-Name: Mike Frysinger X-VCS-Revision: c029863b70ca77f59cd181974cfab0fa18c0a265 X-VCS-Branch: master Date: Fri, 29 Oct 2021 05:37:46 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 0b58e6ee-58c3-4cc6-a3ff-e996488ea74c X-Archives-Hash: 82ae5405624bffd0521a7a4548ea9400 commit: c029863b70ca77f59cd181974cfab0fa18c0a265 Author: Mike Frysinger gentoo org> AuthorDate: Fri Oct 29 03:38:58 2021 +0000 Commit: Mike Frysinger gentoo org> CommitDate: Fri Oct 29 03:38:58 2021 +0000 URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=c029863b sandbox: avoid repetitive strlen calculations when building cmdline Signed-off-by: Mike Frysinger gentoo.org> src/sandbox.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/sandbox.c b/src/sandbox.c index 6cd5f38..7e8a769 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -263,21 +263,19 @@ int main(int argc, char **argv) str_list_add_item_copy(argv_bash, sandbox_info.sandbox_rc, oom_error); if (argc >= 2) { int i; + size_t cmdlen; + char *cmd = NULL; str_list_add_item_copy(argv_bash, run_str, oom_error); str_list_add_item_copy(argv_bash, argv[1], oom_error); + cmdlen = strlen(argv_bash[4]); for (i = 2; i < argc; i++) { - char *tmp_ptr; - - tmp_ptr = xrealloc(argv_bash[4], - (strlen(argv_bash[4]) + - strlen(argv[i]) + 2) * - sizeof(char)); - argv_bash[4] = tmp_ptr; - - snprintf(argv_bash[4] + strlen(argv_bash[4]), - strlen(argv[i]) + 2, " %s", - argv[i]); + size_t arglen = strlen(argv[i]); + argv_bash[4] = xrealloc(argv_bash[4], cmdlen + arglen + 2); + argv_bash[4][cmdlen] = ' '; + memcpy(argv_bash[4] + cmdlen + 1, argv[i], arglen); + cmdlen += arglen + 1; + argv_bash[4][cmdlen] = '\0'; } }