From patchwork Fri Dec 8 14:53:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13485482 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="sJjg2zj+"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="igqYCdye" Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C55E9212D for ; Fri, 8 Dec 2023 06:53:31 -0800 (PST) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 16A663200A1E; Fri, 8 Dec 2023 09:53:31 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Fri, 08 Dec 2023 09:53:31 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm1; t=1702047210; x=1702133610; bh=Zt xTvqpV4ITb7Bp9x0Pj9C61/47efdYtZ8Tp3tEfEWM=; b=sJjg2zj+TSFH0OBgEg ZN7Jbl5o6ywWWH+CS5uGWioMQ9268hK5Fix0P/eUtvkxjRugLeUJpYiQFE4Frvi0 lRzu1E4oOXu/jVffEeaXN+yXNTbhD3kOm9OTHl8Bz6wzCx/15P0+nVMOZg/4opi+ DKHe9Rg2rDfG0RixFZtGvuyXuM2ntN7QI69YgW3vAar/sBYYz1GIVBhmNmMaBKmB TxX6VQ04vECht93FaU2TS0uYFJ6Xk0pwFsVZ0Ee1DA4lY6N+NZLNFsQnU2g4dyLP AJrYktambczX1Lj8ZfysR7GGcxFqtKPRH03jMRQr1vPzXd6H8FdufFM6/67KLQgS eQJg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1702047210; x=1702133610; bh=ZtxTvqpV4ITb7 Bp9x0Pj9C61/47efdYtZ8Tp3tEfEWM=; b=igqYCdyekeRQwZLmZXLI8OzKhAQp8 e9dXWzoNiia+CHW8VdMo+CVygOMcsXSWegaElIv013ASuaplv/5L/91n2mddFQzs qOAZmggvyLeQ8PvA5R6tkT0uy6DvAV0mnmIg+jOv++3dsqCwpz/NXWN/xVYmNH4h 8Rv4fwNYPZBXfecfkoOoQ0k3003U1gWnoH+dvzUFRGNA8/Y3+XiFSdZqbOskqR6m VBVM+o5+yZiyf6BJ2+U2XWGgKPCehP7pRVzTo6HaLsL4V2pMPfWKWPvU07ZTOuB2 Z4ZJCFiQz1Erlm0XKirxlRWSUqfuD1v0bJghwDyIF3Nkaq4vOebLjTn8g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudekiedgjedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepgeevtdffhedvheehgeeuuefffeetiedtfeehjeehteevieehkeekieefhfdvveeh necuffhomhgrihhnpehuphgurghtvgdrnhgvfienucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 8 Dec 2023 09:53:29 -0500 (EST) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 38a14f24 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 8 Dec 2023 14:51:58 +0000 (UTC) Date: Fri, 8 Dec 2023 15:53:27 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Jonathan Nieder Subject: [PATCH v2 08/11] reftable/stack: fix use of unseeded randomness Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: When writing a new reftable stack, Git will first create the stack with a random suffix so that concurrent updates will not try to write to the same file. This random suffix is computed via a call to rand(3P). But we never seed the function via srand(3P), which means that the suffix is in fact always the same. Fix this bug by using `git_rand()` instead, which does not need to be initialized. While this function is likely going to be slower depending on the platform, this slowness should not matter in practice as we only use it when writing a new reftable stack. Signed-off-by: Patrick Steinhardt --- reftable/readwrite_test.c | 6 +++--- reftable/stack.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/reftable/readwrite_test.c b/reftable/readwrite_test.c index 469ab79a5a..278663f22d 100644 --- a/reftable/readwrite_test.c +++ b/reftable/readwrite_test.c @@ -141,8 +141,8 @@ static void test_log_buffer_size(void) */ uint8_t hash1[GIT_SHA1_RAWSZ], hash2[GIT_SHA1_RAWSZ]; for (i = 0; i < GIT_SHA1_RAWSZ; i++) { - hash1[i] = (uint8_t)(rand() % 256); - hash2[i] = (uint8_t)(rand() % 256); + hash1[i] = (uint8_t)(git_rand() % 256); + hash2[i] = (uint8_t)(git_rand() % 256); } log.value.update.old_hash = hash1; log.value.update.new_hash = hash2; @@ -320,7 +320,7 @@ static void test_log_zlib_corruption(void) }; for (i = 0; i < sizeof(message) - 1; i++) - message[i] = (uint8_t)(rand() % 64 + ' '); + message[i] = (uint8_t)(git_rand() % 64 + ' '); reftable_writer_set_limits(w, 1, 1); diff --git a/reftable/stack.c b/reftable/stack.c index 2f1494aef2..95963f67a2 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -434,7 +434,7 @@ int reftable_stack_add(struct reftable_stack *st, static void format_name(struct strbuf *dest, uint64_t min, uint64_t max) { char buf[100]; - uint32_t rnd = (uint32_t)rand(); + uint32_t rnd = (uint32_t)git_rand(); snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x", min, max, rnd); strbuf_reset(dest);