diff mbox series

[11/15] midx: use 64-bit multiplication for chunk sizes

Message ID c6a433efba3214e83a7265e53c24cb4001345f14.1607012215.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series Refactor chunk-format into an API | expand

Commit Message

Derrick Stolee Dec. 3, 2020, 4:16 p.m. UTC
From: Derrick Stolee <dstolee@microsoft.com>

When calculating the sizes of certain chunks, we should use 64-bit
multiplication always. This allows us to properly predict the chunk
sizes without risk of overflow.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 midx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Junio C Hamano Dec. 3, 2020, 10 p.m. UTC | #1
"Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Derrick Stolee <dstolee@microsoft.com>
>
> When calculating the sizes of certain chunks, we should use 64-bit
> multiplication always. This allows us to properly predict the chunk
> sizes without risk of overflow.

That's quite an obvious fix, applicable even before this entire
series.  I think we saw quite similar bugfixes in different parts of
the codebase recently.

So far, everything looks good.


> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> ---
>  midx.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/midx.c b/midx.c
> index 0548266bea..47f5f60fcd 100644
> --- a/midx.c
> +++ b/midx.c
> @@ -946,12 +946,12 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
>  	chunks[2].write_fn = write_midx_oid_lookup;
>  
>  	chunks[3].id = MIDX_CHUNKID_OBJECTOFFSETS;
> -	chunks[3].size = ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH;
> +	chunks[3].size = (uint64_t)ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH;
>  	chunks[3].write_fn = write_midx_object_offsets;
>  
>  	if (ctx.large_offsets_needed) {
>  		chunks[4].id = MIDX_CHUNKID_LARGEOFFSETS;
> -		chunks[4].size = ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
> +		chunks[4].size = (uint64_t)ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
>  		chunks[4].write_fn = write_midx_large_offsets;
>  	}
Taylor Blau Dec. 8, 2020, 6:43 p.m. UTC | #2
On Thu, Dec 03, 2020 at 02:00:50PM -0800, Junio C Hamano wrote:
> "Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > From: Derrick Stolee <dstolee@microsoft.com>
> >
> > When calculating the sizes of certain chunks, we should use 64-bit
> > multiplication always. This allows us to properly predict the chunk
> > sizes without risk of overflow.
>
> That's quite an obvious fix, applicable even before this entire
> series.  I think we saw quite similar bugfixes in different parts of
> the codebase recently.

:-). Indeed, Stolee and I were probably looking at output from the same
static analysis tool, which seems to be eager to catch these sorts of
multiplication-widening errors.

I'd be happy to see this patch get applied down regardless of what
happens with the rest of the series.

Thanks,
Taylor
diff mbox series

Patch

diff --git a/midx.c b/midx.c
index 0548266bea..47f5f60fcd 100644
--- a/midx.c
+++ b/midx.c
@@ -946,12 +946,12 @@  static int write_midx_internal(const char *object_dir, struct multi_pack_index *
 	chunks[2].write_fn = write_midx_oid_lookup;
 
 	chunks[3].id = MIDX_CHUNKID_OBJECTOFFSETS;
-	chunks[3].size = ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH;
+	chunks[3].size = (uint64_t)ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH;
 	chunks[3].write_fn = write_midx_object_offsets;
 
 	if (ctx.large_offsets_needed) {
 		chunks[4].id = MIDX_CHUNKID_LARGEOFFSETS;
-		chunks[4].size = ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
+		chunks[4].size = (uint64_t)ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
 		chunks[4].write_fn = write_midx_large_offsets;
 	}