From patchwork Wed Nov 20 19:13:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taylor Blau X-Patchwork-Id: 13881408 Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FE421B3B2E for ; Wed, 20 Nov 2024 19:13:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130029; cv=none; b=S971qnLBHRF3i/1tMZTVf7PXlk0/UAr/dOSqWNXazc8g+ma3EtrQWct9R98Sab7ipIIoyAu0f4XA7H0FfDh7DGtvjuhmjW6OPL5BPcL2b/PpvCamZBTw/KpSkCJsHGSBxpDTi015TNo4tsekpXI2ssZpSWf/0+CqcH8GvrUgGp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130029; c=relaxed/simple; bh=RS2N+33KTVwCx14lPhrijwNT2iWbUnWrkZOr65gwj2M=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=J+N8DcNO7CLwXtmaDe6/zywN0kgrwvQ+GBzEdaxeYVb4ITuVGr0Hk5Oma7ip+rjw0WCHL9MnbrJ1M/teGfJLUDt9tC22v1j2XgjghgR58BB1tteVSfWQoYEbNJXY4a9AqKpm6edZbQ6rNIVK2eweioe+EYIkSBR3OU2KTbboTOk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com; spf=pass smtp.mailfrom=ttaylorr.com; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b=d39fdaPJ; arc=none smtp.client-ip=209.85.219.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b="d39fdaPJ" Received: by mail-yb1-f178.google.com with SMTP id 3f1490d57ef6-e387e50d98eso29736276.2 for ; Wed, 20 Nov 2024 11:13:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ttaylorr-com.20230601.gappssmtp.com; s=20230601; t=1732130026; x=1732734826; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=fzBOar12Datdh+HCps6EpyStY3L2HlOcSH7Kp+yoJ88=; b=d39fdaPJ4guYraGwY7F3b6/EddHRY3D/wDGzMDrvhC9sWGvmKSMzgotdh006aIpTeT UYgMr08qtfuq27wU0XdIURgSSDaFLr2uXOFFPs0XaSeBbCxA2LTLSv8zXfR986QdIxBW x4Op8Xg/KVIvlqNT1nXfTlS6M2UclyTE6dy7+KHuuv8MVOTjA2rONxLC/PaGOpUEQGI6 E5SXJNLgkkLzufcp5EM1ykiDqrNH+DeiCaD/X9SYD+sWjXUTq27KNuHSPr5qcasOzHXY s6EJU2btR60OOMYtZU3CzHBi6rMsrunx5NxNnhqx6sEY5wlYL5GRZB9VKzb0fRGgJWM9 C8fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732130026; x=1732734826; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=fzBOar12Datdh+HCps6EpyStY3L2HlOcSH7Kp+yoJ88=; b=wrjSLxEP8mxnkcMldenp0L/AtW7lPZG4IYrmg27RKwcBzmw/ySpPwkSJ57qkpTblI4 VGexanDILmMsbx/Vjx+iYVEyd3jKJ7vkiLN9qbD5vcFSiIiIqIK7M9zGJlS/cT3i+uCK XIhHUaadr1s0rNFgtgkNHFddiY1Hy0NYYYFfBgC0ZvKUKnF0lFFGgtpn6kgpKRkuYvFa 0nFHztZ5VB5NXIaHRFOCEOhXOK8MBNtJTEm5qsw5xc5yOvi94A2hyi8vsg4Svq/ysl8Y 1GOQ9ilhj1gTaQW0/PXL/F8r60hTydyHsGBg7hSSQuGbx+76xnE3rH8b/MdtkTsHajKV vorQ== X-Gm-Message-State: AOJu0YwlkH9XND74xy3DNsyT3ziMaA4NnsaohTZWTFJd1kSXIgm0Px6k iNSmF0i/h78zgO/nCUTaBKe5TmTDiACUBiYiqMM5pzYszJHYOhTIO8EVG2Wt/SLMeqMUxboAqYo h X-Gm-Gg: ASbGncuLZOZPMXR9k3czwoyQIuRzhpmszdk9YdedhGKMsofLPW/gGKPvus3yW7KxRmH EYojr7jgeXXqaFEGXQwcKT3Dg8xZO17+aiiMCFcFsnzkN7LQMmRU46BIszR01qBKBIsDC42Gtkn g/GH9cnfueCz55aFEHhOvVcxArooq1A7HAULmKy21hVfRjLvy2kyZei1hPVw1cbd/1SIr99g1ll +gIu9H1fKBJlPyqE0UlEi/VzBWNMBv00WSHKv3/wQtJTS4P7bRl1/NEMpof175qtQB8v6ZMt4ud mD+G8jR/jddWJvMvHp/2Tg== X-Google-Smtp-Source: AGHT+IGdfslpspmdKBIdqWKlh2nluPweVJBhojXjRIP3WDLYRDjB33TVWYgbDo7e8gum6Cje7w2Gzg== X-Received: by 2002:a05:6902:1b82:b0:e38:a15d:409d with SMTP id 3f1490d57ef6-e38cb56c5f6mr4304843276.13.1732130026202; Wed, 20 Nov 2024 11:13:46 -0800 (PST) Received: from localhost (104-178-186-189.lightspeed.milwwi.sbcglobal.net. [104.178.186.189]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e387e827436sm3343773276.60.2024.11.20.11.13.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 11:13:45 -0800 (PST) Date: Wed, 20 Nov 2024 14:13:44 -0500 From: Taylor Blau To: git@vger.kernel.org Cc: Elijah Newren , Jeff King , Junio C Hamano , "brian m. carlson" Subject: [PATCH 1/6] csum-file: store the hash algorithm as a struct field Message-ID: <380133a11421fe4ce8428d11be479d78923bc5cc.1732130001.git.me@ttaylorr.com> 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: Throughout the hashfile API, we rely on a reference to 'the_hash_algo', and call its _usnafe function variants directly. Prepare for a future change where we may use a different 'git_hash_algo' pointer (instead of just relying on 'the_hash_algo' throughout) by making the 'git_hash_algo' pointer a member of the 'hashfile' structure itself. Signed-off-by: Taylor Blau --- csum-file.c | 20 +++++++++++--------- csum-file.h | 1 + 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/csum-file.c b/csum-file.c index c203ebf11b3..62f4965f094 100644 --- a/csum-file.c +++ b/csum-file.c @@ -50,7 +50,7 @@ void hashflush(struct hashfile *f) if (offset) { if (!f->skip_hash) - the_hash_algo->unsafe_update_fn(&f->ctx, f->buffer, offset); + f->algop->unsafe_update_fn(&f->ctx, f->buffer, offset); flush(f, f->buffer, offset); f->offset = 0; } @@ -71,14 +71,14 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result, hashflush(f); if (f->skip_hash) - hashclr(f->buffer, the_repository->hash_algo); + hashclr(f->buffer, f->algop); else - the_hash_algo->unsafe_final_fn(f->buffer, &f->ctx); + f->algop->unsafe_final_fn(f->buffer, &f->ctx); if (result) - hashcpy(result, f->buffer, the_repository->hash_algo); + hashcpy(result, f->buffer, f->algop); if (flags & CSUM_HASH_IN_STREAM) - flush(f, f->buffer, the_hash_algo->rawsz); + flush(f, f->buffer, f->algop->rawsz); if (flags & CSUM_FSYNC) fsync_component_or_die(component, f->fd, f->name); if (flags & CSUM_CLOSE) { @@ -128,7 +128,7 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count) * f->offset is necessarily zero. */ if (!f->skip_hash) - the_hash_algo->unsafe_update_fn(&f->ctx, buf, nr); + f->algop->unsafe_update_fn(&f->ctx, buf, nr); flush(f, buf, nr); } else { /* @@ -174,7 +174,9 @@ static struct hashfile *hashfd_internal(int fd, const char *name, f->name = name; f->do_crc = 0; f->skip_hash = 0; - the_hash_algo->unsafe_init_fn(&f->ctx); + + f->algop = the_hash_algo; + f->algop->unsafe_init_fn(&f->ctx); f->buffer_len = buffer_len; f->buffer = xmalloc(buffer_len); @@ -208,7 +210,7 @@ void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpo { hashflush(f); checkpoint->offset = f->total; - the_hash_algo->unsafe_clone_fn(&checkpoint->ctx, &f->ctx); + f->algop->unsafe_clone_fn(&checkpoint->ctx, &f->ctx); } int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) @@ -219,7 +221,7 @@ int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint lseek(f->fd, offset, SEEK_SET) != offset) return -1; f->total = offset; - the_hash_algo->unsafe_clone_fn(&f->ctx, &checkpoint->ctx); + f->algop->unsafe_clone_fn(&f->ctx, &checkpoint->ctx); f->offset = 0; /* hashflush() was called in checkpoint */ return 0; } diff --git a/csum-file.h b/csum-file.h index 7c73da0a40a..2b45f4673a2 100644 --- a/csum-file.h +++ b/csum-file.h @@ -20,6 +20,7 @@ struct hashfile { size_t buffer_len; unsigned char *buffer; unsigned char *check_buffer; + const struct git_hash_algo *algop; /** * If non-zero, skip_hash indicates that we should From patchwork Wed Nov 20 19:13:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taylor Blau X-Patchwork-Id: 13881409 Received: from mail-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 778121BC9E5 for ; Wed, 20 Nov 2024 19:13:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130032; cv=none; b=Q0yEune0uqmPCmXVglUqFOwxmhgDEwC/in4QUfpvHGNtim6haU6hdgkGf/nUYYYrIHWcneKXr0+dtNZKxuno4JyI3g2AqAspPUoLEQ21hwbisqSFv4fmqlFy5ekodS3o5KRdwHSv3z7OsMlr76Xc/GfcpRwLUj5edMKaHetxIbQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130032; c=relaxed/simple; bh=tPCGQE4k2UPI86oP3GzEMCE04ykUmCaCcAoft7Iw8d8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uFt+n6DiZjL1p7q9MdOsazlkz4UVqrLepEdHwrtfAJ+DgKyzqGaOtf3qsrDWtpxLEygw0j2gcmkTso9lYkjLEITHP9x65VcQ+ZXz6lykEyLNgXL7DZOllYa9gr3a9pyKEKXX1PG28SVHAf7DwCpSvO2aRHJZedJ/E/E6f8qaHrU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com; spf=pass smtp.mailfrom=ttaylorr.com; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b=SebVMORn; arc=none smtp.client-ip=209.85.219.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b="SebVMORn" Received: by mail-yb1-f179.google.com with SMTP id 3f1490d57ef6-e387ad7abdaso62990276.0 for ; Wed, 20 Nov 2024 11:13:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ttaylorr-com.20230601.gappssmtp.com; s=20230601; t=1732130029; x=1732734829; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=9C27EFNZlisMrjROfjLk6TYwY56H7cLzp5qM/gRcWao=; b=SebVMORnoQ4tsXJnXyfqew+sbvYyMlPzyTM6cKj8WvfLYr7L/ftBQXlX/jR9VDvcHm H/Uv7wYSJeannHbO2j5qVhXiqm+M2B9C3t8qrSldwgvRDEetjN0k525skk0LfRBYEcDK zb/w0o91AXO2ryCdiK5NEiNRWEXceQ5csSra6tLmp0E1OrxhcMUKwPTy6anOOqetRsdu WDBhPTQrqK9JWme1fBGKOi5KaqUyjE/oCkg52DBRKvrD5KyWDB0kPfzvQxqapavI55F0 EWhYhfpyDlFn42g0qTMcYdknKnq4rsNMhLhYK2DV8lO3KY/dt1jW6gEB7NJWVO6zpbsJ 6VuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732130029; x=1732734829; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=9C27EFNZlisMrjROfjLk6TYwY56H7cLzp5qM/gRcWao=; b=RtZ4VyhsiiezkQ1xe0JwUCe7fRjfCzJS81cjMwleFh/tkp/HslJktt+A919xaR4zAr Bqml5wSdQ+qpB3p8G8+YECMuU1EGYi0GsgbxTUXM1feAQoE6b3c6nbu2Hg1OsYWGC6QE +mlfsRjPP4TU0KwTaPlYRvOsNJVONBHHUn/jbZc9+ntT3nD2dn4SjMGN6iLfztAge4Gu NJchtTxHg3CgEkIkasxiAH/w7FdjomFrCYXlhXKOH1uWbARQutqPqruW62MRj1SG31ZZ UbWd5OMM1ko6tyNA1cKuIWOrc8XFutTb0T+WDBgMlYqd3AERTD0IfJQC6wLPe/kJ7XoG dltQ== X-Gm-Message-State: AOJu0YzNgDe516p7YVSyt4kF5upq6NsnKX243GonzlaN39wn3rfeuZ/Z J3GgH1+HWg1DBcwypOb+xRCjNRr+TxTn7uuR8bSZQz7YiuObW7L7FJxmWbwbzGtDlzyP5U8ak7F p X-Google-Smtp-Source: AGHT+IE/WoAbE2hEVKxLQ74oHANCe0RvgSsTUeJbkxkr2VKveSRqZgCPlDdNQKlLlue1TV6vtSQxdg== X-Received: by 2002:a05:6902:1b10:b0:e2b:b45a:149 with SMTP id 3f1490d57ef6-e38cb5ec160mr4257079276.39.1732130029208; Wed, 20 Nov 2024 11:13:49 -0800 (PST) Received: from localhost (104-178-186-189.lightspeed.milwwi.sbcglobal.net. [104.178.186.189]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e387e71fd7fsm3215980276.9.2024.11.20.11.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 11:13:48 -0800 (PST) Date: Wed, 20 Nov 2024 14:13:47 -0500 From: Taylor Blau To: git@vger.kernel.org Cc: Elijah Newren , Jeff King , Junio C Hamano , "brian m. carlson" Subject: [PATCH 2/6] csum-file.c: extract algop from hashfile_checksum_valid() 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: Perform a similar transformation as in the previous commit, but focused instead on hashfile_checksum_valid(). This function does not work with a hashfile structure itself, and instead validates the raw contents of a file written using the hashfile API. We'll want to be prepared for a similar change to this function in the future, so prepare ourselves for that by extracting 'the_hash_algo' into its own field for use within this function. Signed-off-by: Taylor Blau --- csum-file.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/csum-file.c b/csum-file.c index 62f4965f094..107bc4c96f9 100644 --- a/csum-file.c +++ b/csum-file.c @@ -242,14 +242,15 @@ int hashfile_checksum_valid(const unsigned char *data, size_t total_len) { unsigned char got[GIT_MAX_RAWSZ]; git_hash_ctx ctx; - size_t data_len = total_len - the_hash_algo->rawsz; + const struct git_hash_algo *algop = the_hash_algo; + size_t data_len = total_len - algop->rawsz; - if (total_len < the_hash_algo->rawsz) + if (total_len < algop->rawsz) return 0; /* say "too short"? */ - the_hash_algo->unsafe_init_fn(&ctx); - the_hash_algo->unsafe_update_fn(&ctx, data, data_len); - the_hash_algo->unsafe_final_fn(got, &ctx); + algop->unsafe_init_fn(&ctx); + algop->unsafe_update_fn(&ctx, data, data_len); + algop->unsafe_final_fn(got, &ctx); - return hasheq(got, data + data_len, the_repository->hash_algo); + return hasheq(got, data + data_len, algop); } From patchwork Wed Nov 20 19:13:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taylor Blau X-Patchwork-Id: 13881410 Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7104B1AB6CD for ; Wed, 20 Nov 2024 19:13:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130035; cv=none; b=RPGyzRQUx+qz7dA0IvfhuNl1Qeqve83yVTkTpCH+Eis9unOjWwvV6Y3JdSVKgdyeUH3lICKZPhoSvHHEjTya3pyy7cj1C7u4B2SFG8H5lNRaJd+bsLzPjzQXOLlvpKOvS0p4u45gQx+NlCq9f7MrhwTlDLxOhYeebpPtnAN6fKU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130035; c=relaxed/simple; bh=d7uSPrdyswNUWPJX18eK+kp1UHZ4N/5Qx8z7qzMHmsg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BhDXIbcLS5YelyELimog1/25DcVZLaw+Pk8BXL2Ia71U7BAG1/KheA2NaZMOwproPTcS7l6QTVpERxUwihIXm1J7lPJ+pt6yI6WruH8As80kaDWeTaaxUIzeKPhyG2KpjpNZqROzoD6DpoUgTvZO6TGQWGB03D93ew4/42dmFIU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com; spf=pass smtp.mailfrom=ttaylorr.com; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b=vHYWwAGr; arc=none smtp.client-ip=209.85.128.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b="vHYWwAGr" Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-6eebae6f013so164427b3.0 for ; Wed, 20 Nov 2024 11:13:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ttaylorr-com.20230601.gappssmtp.com; s=20230601; t=1732130032; x=1732734832; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=R5Ymu9EwaSaPZMf/gHFx8uZWWIVfqCC9/p2shHrpc0s=; b=vHYWwAGrynkYtD7hQMhM7VXn5MC0IYlBFPcf5E07HzywnUkURNVZzPfV8MI5V1eKLm +PVbx7pS5nX+UQIFxjCr+fm6zg0ubERpQCC2FdeLbYP/Xx4HNODxkmkz5RKNgSyFtG0/ lEWqbCbK9/+fD1XIjK/NF+Kqci2YrN0Um2AhPECVNCKPTeOgx9ZGUZUoJMvP4VM5mehL vjjs5z71kZfuTzWnAQxe2zlfmWyzcUsv/NtJnJa9ZrIBiZ4n9OIgBrk/Qgs6BjS/cRKG ii5B3n07pUDjMxDxbzvqfJ32uUEx6tsDlOfcyJhFrFmw4fQbFIXj+shCETEjSXHFDj1H imAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732130032; x=1732734832; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=R5Ymu9EwaSaPZMf/gHFx8uZWWIVfqCC9/p2shHrpc0s=; b=TJvcHBffC4ryK+khMrOG/sMDNMCYdY+ggZWKV6uuDp3CGxqakTo5Q98hrKRZOipLlf 8HAyioyXurNHpD5Md/RuJt+sqPEHHjbr1d+FPvUodxQm1Gv0pJvFq8bH9JzYdAX1G7G4 e79ERgtYeWu+NysQJ96knj1sXV1XNnvPZ4DLC5m0amLrcAtdi8GBpt5KnDzUa5ZJkwOJ JfvI8fVpDxMQM7XMqQ9K5NmcdCb1Tl/aZQtleJIWniDVO6o3RgIOFGrTv8ynX62oDezC gJyFrM7gYdigpRBgCk4xFZOPo0semU8qUIB3giNRJXwlbp2WOq61h2JmJhyc4zKah5xk EGgw== X-Gm-Message-State: AOJu0YwDtft0U8Vy9gUvfRMXR9m9ZE7XFFOjkQKWMy95xuxrJQi1qhBH WFCsEdh/l/GmgY4qqBvqzaMqw40skdd+GBR1lEwGbOP/NkloAP+KfWrh1e6nzgdmd4uasksYIRW e X-Google-Smtp-Source: AGHT+IEV4jYZcI//7Qg/vxoP6fUE/Mf4sl9FrMOZomqfddFd0ykhSIQqtF5arTZbuIAuHpwAf1bGow== X-Received: by 2002:a05:690c:74c6:b0:6ee:b7cf:4a8f with SMTP id 00721157ae682-6eebd2e3f20mr45170247b3.38.1732130032164; Wed, 20 Nov 2024 11:13:52 -0800 (PST) Received: from localhost (104-178-186-189.lightspeed.milwwi.sbcglobal.net. [104.178.186.189]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6ee7137f39esm23674297b3.121.2024.11.20.11.13.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 11:13:51 -0800 (PST) Date: Wed, 20 Nov 2024 14:13:50 -0500 From: Taylor Blau To: git@vger.kernel.org Cc: Elijah Newren , Jeff King , Junio C Hamano , "brian m. carlson" Subject: [PATCH 3/6] hash.h: introduce `unsafe_hash_algo()` Message-ID: <17f92dba34bee235177c8100daab49068fe37254.1732130001.git.me@ttaylorr.com> 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: In 253ed9ecff (hash.h: scaffolding for _unsafe hashing variants, 2024-09-26), we introduced "unsafe" variants of the SHA-1 hashing functions by introducing new functions like "unsafe_init_fn()" and so on. This approach has a major shortcoming that callers must remember to consistently use one variant or the other. Failing to consistently use (or not use) the unsafe variants can lead to crashes at best, or subtle memory corruption issues at worst. In the hashfile API, this isn't difficult to achieve, but verifying that all callers consistently use the unsafe variants is somewhat of a chore given how spread out all of the callers are. In the sha1 and sha1-unsafe test helpers, all of the calls to various hash functions are guarded by an "if (unsafe)" conditional, which is repetitive and cumbersome. Address these issues by introducing a new pattern whereby one 'git_hash_algo' can return a pointer to another 'git_hash_algo' that represents the unsafe version of itself. So instead of having something like: if (unsafe) the_hash_algo->init_fn(...); the_hash_algo->update_fn(...); the_hash_algo->final_fn(...); else the_hash_algo->unsafe_init_fn(...); the_hash_algo->unsafe_update_fn(...); the_hash_algo->unsafe_final_fn(...); we can instead write: struct git_hash_algo *algop = the_hash_algo; if (unsafe) algop = unsafe_hash_algo(algop); the_hash_algo->init_fn(...); the_hash_algo->update_fn(...); the_hash_algo->final_fn(...); This removes the existing shortcoming by no longer forcing the caller to "remember" which variant of the hash functions it wants to call, only to hold onto a 'struct git_hash_algo' pointer that is initialized once. Similarly, while there currently is still a way to "mix" safe and unsafe functions, this too will go away after subsequent commits remove all direct calls to the unsafe_ variants. Suggested-by: Jeff King Signed-off-by: Taylor Blau --- hash.h | 5 +++++ object-file.c | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/hash.h b/hash.h index 756166ce5e8..23cf6876e50 100644 --- a/hash.h +++ b/hash.h @@ -305,6 +305,9 @@ struct git_hash_algo { /* The all-zeros OID. */ const struct object_id *null_oid; + + /* The unsafe variant of this hash function, if one exists. */ + const struct git_hash_algo *unsafe; }; extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS]; @@ -323,6 +326,8 @@ static inline int hash_algo_by_ptr(const struct git_hash_algo *p) return p - hash_algos; } +const struct git_hash_algo *unsafe_hash_algo(const struct git_hash_algo *algop); + const struct object_id *null_oid(void); static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop) diff --git a/object-file.c b/object-file.c index b1a3463852c..fddcdbe9ba6 100644 --- a/object-file.c +++ b/object-file.c @@ -204,6 +204,22 @@ static void git_hash_unknown_final_oid(struct object_id *oid UNUSED, BUG("trying to finalize unknown hash"); } +static const struct git_hash_algo sha1_unsafe_algo = { + .name = "sha1", + .format_id = GIT_SHA1_FORMAT_ID, + .rawsz = GIT_SHA1_RAWSZ, + .hexsz = GIT_SHA1_HEXSZ, + .blksz = GIT_SHA1_BLKSZ, + .init_fn = git_hash_sha1_init_unsafe, + .clone_fn = git_hash_sha1_clone_unsafe, + .update_fn = git_hash_sha1_update_unsafe, + .final_fn = git_hash_sha1_final_unsafe, + .final_oid_fn = git_hash_sha1_final_oid_unsafe, + .empty_tree = &empty_tree_oid, + .empty_blob = &empty_blob_oid, + .null_oid = &null_oid_sha1, +}; + const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = { { .name = NULL, @@ -241,6 +257,7 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = { .unsafe_update_fn = git_hash_sha1_update_unsafe, .unsafe_final_fn = git_hash_sha1_final_unsafe, .unsafe_final_oid_fn = git_hash_sha1_final_oid_unsafe, + .unsafe = &sha1_unsafe_algo, .empty_tree = &empty_tree_oid, .empty_blob = &empty_blob_oid, .null_oid = &null_oid_sha1, @@ -307,6 +324,15 @@ int hash_algo_by_length(int len) return GIT_HASH_UNKNOWN; } +const struct git_hash_algo *unsafe_hash_algo(const struct git_hash_algo *algop) +{ + /* If we have a faster "unsafe" implementation, use that. */ + if (algop->unsafe) + return algop->unsafe; + /* Otherwise use the default one. */ + return algop; +} + /* * This is meant to hold a *small* number of objects that you would * want repo_read_object_file() to be able to return, but yet you do not want From patchwork Wed Nov 20 19:13:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taylor Blau X-Patchwork-Id: 13881411 Received: from mail-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7E8641BDA9A for ; Wed, 20 Nov 2024 19:13:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130038; cv=none; b=cbT/eErYrXA9gx9vTov0RtMYmFv/+FTNEV07zaNOHQc+9JjGdBiYPg4BiLvJ4OYr5s6jCPtFP55xLdzbCWDtzlCGZOvlaAoVtyXpsA21HmLzictQFy8an/0wOcQSYXW4KUQnwyQ4r8IPiqiUhxzQBRPnY7h8mi7SQr+W9q9VBSw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130038; c=relaxed/simple; bh=nTqCG5I2Kxuz9wXTLfKstNvFr2xli3WzyjqEAPCyJrE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ZA2/xXtoA/PahCVKt9dGHkub5DaTXPQVmM7c37ExkN+Ne3EFtxhEu9XaVwppUQaQkeXaUKPdCHy+DX+8jQO4hRF1VvTPMsB7xTuxwwlVpssBgciiyfcy9Qu0MBxrtYtV+2bT/Elhmnm7Q9PWPyObQQVtSew1VaYXt2ZzvlN4DAE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com; spf=pass smtp.mailfrom=ttaylorr.com; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b=fWl+jZBd; arc=none smtp.client-ip=209.85.219.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b="fWl+jZBd" Received: by mail-yb1-f173.google.com with SMTP id 3f1490d57ef6-e387afcb162so41987276.1 for ; Wed, 20 Nov 2024 11:13:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ttaylorr-com.20230601.gappssmtp.com; s=20230601; t=1732130035; x=1732734835; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=SZtRvh6sEUDE5sp6mm/ozTAlC4pHkNdLqHxtn/vgvEI=; b=fWl+jZBdRpaW0n3LE9/1V6xmKg76WClT5DVN3aq4xbEubuGg3cTJJJCok9wIWv9mB0 XbmavjagL7O81tJgKA/Xvk4n0Rskk9ZZrA4+N3ApfGKa2VSRTV37GWNqxyCprTT3xyOc c6EOrlrbQizjaTF9HzpE2CsrPC8WgSmlaFl8dSlOJ8uxyGCREjaD337nKQj3pzyUIZlE GR8wkBQMO1iB41LSfemWz4jfGo3eUj1mUwd9vluFgN1veft4+jSEJQ5D7+fP3nOBl73v qzHGgFBSQjTkQ+wG83ijCDSGKViyt7vdM7yEm7WJsLr7Kq9hmR1wt/OUqdpRyGQmY7FU VAIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732130035; x=1732734835; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=SZtRvh6sEUDE5sp6mm/ozTAlC4pHkNdLqHxtn/vgvEI=; b=S4vCE3PTwHEqh30LqSqF+/J00wnzyogeDZy3JdPayOrDRwhlmUlWFPuDADQa/fewpg CuLe2V6CeMh3WXSS51yJT3k3p70TZ0ttmyaiu0QkYSHFwCyqac9xz9HQr+iTVtLS9uLX 0Y6Tb+c+eyWeEMvbYLEiN2ghTePt8dwv/cVDMZmE19ENpIf7FdTWGeKNmTbqYqK/JybM q+r5Ry4EluaZHL7DvypVN16HYaXec22niusrjD1N3VsvfA9oHlY7ifjOkUgn2ItMBat7 /64lM8y3hCr4eObVkmwEAftATezNvHwZuxdoVijd3MhjL1+82kQ/aezym9KYQHvV2uZa iXjg== X-Gm-Message-State: AOJu0YxmPO93BSSOD8CxWfulX4UJqFuOKXa131hojnZTxL08DTvr0K59 GyWAEmz69O46iHlf3SBZCFj2/loVMW49tP7CF+CMyWMNYKYoW3SOS6p8XvSIrT40fyBvfAqscL2 p X-Gm-Gg: ASbGncvcxOT0tPflFjtGurE/XFSDxmqBw/EU1Lc5AVa95NZhXe6I9NmE6XlArahIo/1 8i9oB8KRgcaLbtkf0pLekQWbTwSvqYpljrrIfbKZ7JdjgGVdfD8OkeELzhLrwkSlYNT3I1SDG+6 u8Kb4z5AXa05KTKInT7/obydUJaAQvsgjdoIOmVzz6hadQxIAJcbKCF76GjFe+JO0nnoHg2Sicc KeLspwNiMaqiEyO8fjLlbIf2zriVLjgRr/AkIx4BL1IP1tgWoJPwbyCuLEivYM11kAbBeu+5Z25 qdwMSiFrw3FQEpR8GXo4rw== X-Google-Smtp-Source: AGHT+IHxLGiRAv5zUl3xsQpZ55kK4UWOW2Smy8KiqEKryMPxvkB8pzDq8A2EQFQ0F5F/+yTdBp24TQ== X-Received: by 2002:a05:6902:18c2:b0:e30:e1f3:2a9c with SMTP id 3f1490d57ef6-e38cb3cfc5fmr4608063276.0.1732130035198; Wed, 20 Nov 2024 11:13:55 -0800 (PST) Received: from localhost (104-178-186-189.lightspeed.milwwi.sbcglobal.net. [104.178.186.189]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e387e7e700csm3302253276.45.2024.11.20.11.13.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 11:13:54 -0800 (PST) Date: Wed, 20 Nov 2024 14:13:53 -0500 From: Taylor Blau To: git@vger.kernel.org Cc: Elijah Newren , Jeff King , Junio C Hamano , "brian m. carlson" Subject: [PATCH 4/6] csum-file.c: use unsafe_hash_algo() Message-ID: <0d8e12599e2e20ee9b5d1065856a6ddf7aa5f14a.1732130001.git.me@ttaylorr.com> 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: Instead of calling the unsafe_ hash function variants directly, make use of the shared 'algop' pointer by initializing it to: f->algop = unsafe_hash_algo(the_hash_algo); , thus making all calls use the unsafe variants directly. Signed-off-by: Taylor Blau --- csum-file.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/csum-file.c b/csum-file.c index 107bc4c96f9..fd51f5e432e 100644 --- a/csum-file.c +++ b/csum-file.c @@ -50,7 +50,7 @@ void hashflush(struct hashfile *f) if (offset) { if (!f->skip_hash) - f->algop->unsafe_update_fn(&f->ctx, f->buffer, offset); + f->algop->update_fn(&f->ctx, f->buffer, offset); flush(f, f->buffer, offset); f->offset = 0; } @@ -73,7 +73,7 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result, if (f->skip_hash) hashclr(f->buffer, f->algop); else - f->algop->unsafe_final_fn(f->buffer, &f->ctx); + f->algop->final_fn(f->buffer, &f->ctx); if (result) hashcpy(result, f->buffer, f->algop); @@ -128,7 +128,7 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count) * f->offset is necessarily zero. */ if (!f->skip_hash) - f->algop->unsafe_update_fn(&f->ctx, buf, nr); + f->algop->update_fn(&f->ctx, buf, nr); flush(f, buf, nr); } else { /* @@ -175,8 +175,8 @@ static struct hashfile *hashfd_internal(int fd, const char *name, f->do_crc = 0; f->skip_hash = 0; - f->algop = the_hash_algo; - f->algop->unsafe_init_fn(&f->ctx); + f->algop = unsafe_hash_algo(the_hash_algo); + f->algop->init_fn(&f->ctx); f->buffer_len = buffer_len; f->buffer = xmalloc(buffer_len); @@ -210,7 +210,7 @@ void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpo { hashflush(f); checkpoint->offset = f->total; - f->algop->unsafe_clone_fn(&checkpoint->ctx, &f->ctx); + f->algop->clone_fn(&checkpoint->ctx, &f->ctx); } int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) @@ -221,7 +221,7 @@ int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint lseek(f->fd, offset, SEEK_SET) != offset) return -1; f->total = offset; - f->algop->unsafe_clone_fn(&f->ctx, &checkpoint->ctx); + f->algop->clone_fn(&f->ctx, &checkpoint->ctx); f->offset = 0; /* hashflush() was called in checkpoint */ return 0; } @@ -242,15 +242,15 @@ int hashfile_checksum_valid(const unsigned char *data, size_t total_len) { unsigned char got[GIT_MAX_RAWSZ]; git_hash_ctx ctx; - const struct git_hash_algo *algop = the_hash_algo; + const struct git_hash_algo *algop = unsafe_hash_algo(the_hash_algo); size_t data_len = total_len - algop->rawsz; if (total_len < algop->rawsz) return 0; /* say "too short"? */ - algop->unsafe_init_fn(&ctx); - algop->unsafe_update_fn(&ctx, data, data_len); - algop->unsafe_final_fn(got, &ctx); + algop->init_fn(&ctx); + algop->update_fn(&ctx, data, data_len); + algop->final_fn(got, &ctx); return hasheq(got, data + data_len, algop); } From patchwork Wed Nov 20 19:13:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taylor Blau X-Patchwork-Id: 13881412 Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0E8C1BDA9A for ; Wed, 20 Nov 2024 19:13:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130041; cv=none; b=TWpyjlots/9Vq0RPnahqLwEuKv6NLrNde53G1UeWihZus133qSka4sOiVFWbKJP1DxHKUvuj0W8iA49QXBzVeSqHZzJl5vw3hqJx317PTQkyoONGMJdGq+DC1HBnVReI9rNSjy3BcsabfI0AUZv/xDshYmFdVmvE6V0qAMllcC0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130041; c=relaxed/simple; bh=tT5h8GDADbe7S7URz5YCHmr4PCkBZykN39avR2JCIfM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=JeZoYFNypKwhhYJowWQDna7cYJieE/mUyG2n6ErLAkdSgzrhHeDu2/IDIlRGiTjGs1tNvOH3FTS16DHnyJ/cM9uOcZBeUvU8AJBLcgSzgYjPNZMS8lJLazx6LsvP4P6GvmjAfDZQ39KZ+SXm6urwW5ytu/z8vhzfi6l/vnsmK+E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com; spf=pass smtp.mailfrom=ttaylorr.com; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b=H/LKO0B/; arc=none smtp.client-ip=209.85.219.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b="H/LKO0B/" Received: by mail-yb1-f180.google.com with SMTP id 3f1490d57ef6-e388e1fd582so31568276.2 for ; Wed, 20 Nov 2024 11:13:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ttaylorr-com.20230601.gappssmtp.com; s=20230601; t=1732130038; x=1732734838; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=oV4d4QYvOcPmjHsN5KRpjrkaxyQj0TY3391mCEyLgJs=; b=H/LKO0B/urm0HgLZZLYKcrQ1P5EddNiXdU2phYi5KP8dvPN/tHu182zdlNC9eaqADP SHE5fKw19cmotYOvYpmwoNImgVsdKRDIFnnQB34v6/h/NHHqYKSn0cPchhV7vTGz6OO4 hFE1xWjGBMHCmudo5d+YG5RRjjyajabyn9bs/HXZq0jqVvZGDpUiizL1p2XW8oiYZO/K GWoD5TEjUxc3nkHzJ+AThDEomcYe74wP5jWiVPjo+4YaITroBtk92goVzu23Xgc5acMv i/YgXSZ//3jUgc8F9sW6EeQ6KSz+TpneewlRgBjEHeZ2w4B2QxtWXzgauJXhvYQ7vcfr QdQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732130038; x=1732734838; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=oV4d4QYvOcPmjHsN5KRpjrkaxyQj0TY3391mCEyLgJs=; b=AtIWVERxtqhzMsOAzZvBMfrF8Wcanvt6SA5swDUnhbYTNqTWAd4LUfAMVJ4HX7knNL SD+gtigDH+jC4WoXbX0Ry3P8axluIQa2T3e4pu6g7Es2L+agL0hm/jJeUKr8PntRY5tj derCiP1cM1UWfbMtHioKhiltsaRtEKp4M0t6Kw8ijqXAzA1dR8aKtIFlF/Pgx3+WO1wi Q+amxw++3E0+3vx/u0JpkVaZaxFvtET4BBzXVlwjOA9nGzTbu/4vj3FzgCR0+cuiQq5C YvnZjJ+5OlQ1oJ8T8So79MmoGvi9OFtQ0yOLxtiCUfnZjO2dYs7GqLXyLHIeVyB7KNl7 mm/g== X-Gm-Message-State: AOJu0Yz/BzqHhnLq3aGDm8/6p7mXoCs5VJnZ0a2NZ2eh8AOFFnBukqV2 gqF5+HpiHHCTm0a5nQMOHA7TpwytdBqnYSMBtksbU/3SOUjyHqTAl+fPu/UReHrP+5NSXSZkni3 z X-Google-Smtp-Source: AGHT+IHiFacgyjhtg099gDWqK1T2yji+ezDPIpAId1Pyhp7jTPpkLSPw4NZoqI0FNytQ+QFt+eLQ/w== X-Received: by 2002:a05:6902:278a:b0:e38:fbd:48db with SMTP id 3f1490d57ef6-e38cb58d48emr3377369276.29.1732130038208; Wed, 20 Nov 2024 11:13:58 -0800 (PST) Received: from localhost (104-178-186-189.lightspeed.milwwi.sbcglobal.net. [104.178.186.189]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e387e753e11sm3307443276.23.2024.11.20.11.13.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 11:13:57 -0800 (PST) Date: Wed, 20 Nov 2024 14:13:56 -0500 From: Taylor Blau To: git@vger.kernel.org Cc: Elijah Newren , Jeff King , Junio C Hamano , "brian m. carlson" Subject: [PATCH 5/6] t/helper/test-hash.c: use unsafe_hash_algo() 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: Remove a series of conditionals within the shared cmd_hash_impl() helper that powers the 'sha1' and 'sha1-unsafe' helpers. Instead, replace them with a single conditional that transforms the specified hash algorithm into its unsafe variant. Then all subsequent calls can directly use whatever function it wants to call without having to decide between the safe and unsafe variants. Signed-off-by: Taylor Blau --- t/helper/test-hash.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/t/helper/test-hash.c b/t/helper/test-hash.c index d0ee668df95..aa82638c621 100644 --- a/t/helper/test-hash.c +++ b/t/helper/test-hash.c @@ -9,6 +9,8 @@ int cmd_hash_impl(int ac, const char **av, int algo, int unsafe) int binary = 0; char *buffer; const struct git_hash_algo *algop = &hash_algos[algo]; + if (unsafe) + algop = unsafe_hash_algo(algop); if (ac == 2) { if (!strcmp(av[1], "-b")) @@ -27,10 +29,7 @@ int cmd_hash_impl(int ac, const char **av, int algo, int unsafe) die("OOPS"); } - if (unsafe) - algop->unsafe_init_fn(&ctx); - else - algop->init_fn(&ctx); + algop->init_fn(&ctx); while (1) { ssize_t sz, this_sz; @@ -49,15 +48,9 @@ int cmd_hash_impl(int ac, const char **av, int algo, int unsafe) } if (this_sz == 0) break; - if (unsafe) - algop->unsafe_update_fn(&ctx, buffer, this_sz); - else - algop->update_fn(&ctx, buffer, this_sz); + algop->update_fn(&ctx, buffer, this_sz); } - if (unsafe) - algop->unsafe_final_fn(hash, &ctx); - else - algop->final_fn(hash, &ctx); + algop->final_fn(hash, &ctx); if (binary) fwrite(hash, 1, algop->rawsz, stdout); From patchwork Wed Nov 20 19:13:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taylor Blau X-Patchwork-Id: 13881413 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 621B81C578C for ; Wed, 20 Nov 2024 19:14:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130043; cv=none; b=X+XhCTO5BNugQfwhtCWUr5YCRDJCOz/I9nGYHXFh0X1CYDa5Ybv3wYXPMT1ZBf7kp5mbCP6gLnia+Vogh745bHRenLa0kgNvOwcr/8w/L21fColznkCEBTEun46jjIoasnmHdO2Ts6/z3LR++6WCw7OCPKD25XE+BepoLxvTMtE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732130043; c=relaxed/simple; bh=WDA0yRZ4FlU5a5TVe4Sr3gbbAhBLDPgxg9I4EDcQ2eY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=lbAdmDVN7dftx2nNwzU+z+nW+vB84W308AN/rQbYQ8iIWt2czjdJiXSiEIHmuNEEe1BXRY+D2Z6YG3cv05XshtX6jHcBqtig4H64ophjvz0oc2o9K9kGgXGboz8yVW+IMcWDSuC/sNAANBSacdVCPNH0UaWeH02iGR86b9wo+wU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com; spf=pass smtp.mailfrom=ttaylorr.com; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b=NMxnHn0o; arc=none smtp.client-ip=209.85.128.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ttaylorr.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ttaylorr-com.20230601.gappssmtp.com header.i=@ttaylorr-com.20230601.gappssmtp.com header.b="NMxnHn0o" Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-6eebc318242so9647b3.3 for ; Wed, 20 Nov 2024 11:14:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ttaylorr-com.20230601.gappssmtp.com; s=20230601; t=1732130041; x=1732734841; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=s67AtFawONSh7Btfa6Xj/BnkGLf6gX3xPXdnVJFThdA=; b=NMxnHn0oeJr+JMnrv64xca6HNWt1Z4T768DBAx0Qas5OFwL4IXD9niu7/MYl35ND9R xuVMpXaK3jZxAbWOj4IZIH1UVA83uxtVUP7pnJh2awc7gMipPpGpQebLGT1+QC5oGHtD zOE7f9ObTeWOF0yRXl2NghV/E9LHpJdqn7fRcmsF2QWOtY/IFqQAu3LlJrEbZVF+3tzb vtAHqgERhO5Nm/ZVoIJNfMjsGnUuvugg9XMtfOqsx+k0BsdRVE/IvSZYtP9L3bJvuZqN Slt0ksQLkng//XgGEKbG8GOSh7NjcAMghMi8FHnvl9lE04RyT5ajQxkOcJk4W1ZhFTRL AlEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732130041; x=1732734841; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=s67AtFawONSh7Btfa6Xj/BnkGLf6gX3xPXdnVJFThdA=; b=EIMFjKVuw5N29hen9sONhw3QCESTtsPH74CkmvDsKQ3aWdj7pqZYsTxnXk3boumzMr P+MCvZ1ELC4020ZH8xq/gg4DuUHITe1MFqls+ffV2rT38XnZQSOqvhqnhn19WGfTZhVL K+Gt05ckVcdScqdM7jFTsEgMVogHmUD1akTsV7u0V6rzJq7ai+kOkiJkhwx8WTZC93I2 YxUeUz2xQAnHED3tePMg5tvAnyozsC2lQAdCAIuFHLtc+LmhJ2xtWXrCRelCLjWiU4+q LF9c0NPx/Z1UPldS2z+lgdhQg8nOWLI1+paTOfCg3EYAoPnq4mr0x3LR6YtKu3JZKXKK sNjg== X-Gm-Message-State: AOJu0YzKo3ryFOMivNW2f1n/RO5CYedje95z1JkU6VWt7kSzZ5RXtXgR Oz/nk1KUg9jLczhIj36iUMtsNuC/LMwhdYHFU6GYf8wEX40aMTY22KU73DQcMp9+7qVw4ZIYUMC P X-Google-Smtp-Source: AGHT+IF3K/Xv+rlc76gtCJC6zQk1qaIP1nfQ9r1w79mFdN8i7OkeBVYQUsDC7DJ9Uir16EFUeN9BNQ== X-Received: by 2002:a05:690c:74c6:b0:6e7:e76e:5852 with SMTP id 00721157ae682-6eebd2ae557mr46862577b3.32.1732130041194; Wed, 20 Nov 2024 11:14:01 -0800 (PST) Received: from localhost (104-178-186-189.lightspeed.milwwi.sbcglobal.net. [104.178.186.189]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6ee71278665sm23606997b3.22.2024.11.20.11.14.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 11:14:00 -0800 (PST) Date: Wed, 20 Nov 2024 14:13:59 -0500 From: Taylor Blau To: git@vger.kernel.org Cc: Elijah Newren , Jeff King , Junio C Hamano , "brian m. carlson" Subject: [PATCH 6/6] hash.h: drop unsafe_ function variants Message-ID: <4081ad08549895a1b05c45fbc6f229683fec3963.1732130001.git.me@ttaylorr.com> 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: Now that all callers have been converted from: the_hash_algo->unsafe_init_fn(); to unsafe_hash_algo(the_hash_algo)->unsafe_init_fn(); and similar, we can remove the scaffolding for the unsafe_ function variants and force callers to use the new unsafe_hash_algo() mechanic instead. Signed-off-by: Taylor Blau --- hash.h | 15 --------------- object-file.c | 15 --------------- 2 files changed, 30 deletions(-) diff --git a/hash.h b/hash.h index 23cf6876e50..6dcbf6ab835 100644 --- a/hash.h +++ b/hash.h @@ -282,21 +282,6 @@ struct git_hash_algo { /* The hash finalization function for object IDs. */ git_hash_final_oid_fn final_oid_fn; - /* The non-cryptographic hash initialization function. */ - git_hash_init_fn unsafe_init_fn; - - /* The non-cryptographic hash context cloning function. */ - git_hash_clone_fn unsafe_clone_fn; - - /* The non-cryptographic hash update function. */ - git_hash_update_fn unsafe_update_fn; - - /* The non-cryptographic hash finalization function. */ - git_hash_final_fn unsafe_final_fn; - - /* The non-cryptographic hash finalization function. */ - git_hash_final_oid_fn unsafe_final_oid_fn; - /* The OID of the empty tree. */ const struct object_id *empty_tree; diff --git a/object-file.c b/object-file.c index fddcdbe9ba6..1040a5408f2 100644 --- a/object-file.c +++ b/object-file.c @@ -232,11 +232,6 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = { .update_fn = git_hash_unknown_update, .final_fn = git_hash_unknown_final, .final_oid_fn = git_hash_unknown_final_oid, - .unsafe_init_fn = git_hash_unknown_init, - .unsafe_clone_fn = git_hash_unknown_clone, - .unsafe_update_fn = git_hash_unknown_update, - .unsafe_final_fn = git_hash_unknown_final, - .unsafe_final_oid_fn = git_hash_unknown_final_oid, .empty_tree = NULL, .empty_blob = NULL, .null_oid = NULL, @@ -252,11 +247,6 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = { .update_fn = git_hash_sha1_update, .final_fn = git_hash_sha1_final, .final_oid_fn = git_hash_sha1_final_oid, - .unsafe_init_fn = git_hash_sha1_init_unsafe, - .unsafe_clone_fn = git_hash_sha1_clone_unsafe, - .unsafe_update_fn = git_hash_sha1_update_unsafe, - .unsafe_final_fn = git_hash_sha1_final_unsafe, - .unsafe_final_oid_fn = git_hash_sha1_final_oid_unsafe, .unsafe = &sha1_unsafe_algo, .empty_tree = &empty_tree_oid, .empty_blob = &empty_blob_oid, @@ -273,11 +263,6 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = { .update_fn = git_hash_sha256_update, .final_fn = git_hash_sha256_final, .final_oid_fn = git_hash_sha256_final_oid, - .unsafe_init_fn = git_hash_sha256_init, - .unsafe_clone_fn = git_hash_sha256_clone, - .unsafe_update_fn = git_hash_sha256_update, - .unsafe_final_fn = git_hash_sha256_final, - .unsafe_final_oid_fn = git_hash_sha256_final_oid, .empty_tree = &empty_tree_oid_sha256, .empty_blob = &empty_blob_oid_sha256, .null_oid = &null_oid_sha256,