git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 31/32] commit-graph.c: fix code that could convert the result of an integer multiplication to a larger type

From
Danh Doan <congdanhqx@gmail.com>
Date
Nov 7, 2019, 04:06 UTC
Message-ID
<20191107040605.GA8096@danh.dev>
In-Reply-To
<xmqqtv7gs6g3.fsf@gitster-ct.c.googlers.com>
On 2019-11-07 12:37:00 +0900, Junio C Hamano wrote:
Show 22 quoted lines
> Danh Doan <congdanhqx@gmail.com> writes:
> 
> > On 2019-11-06 11:23:00 +0900, Junio C Hamano wrote:
> >> > @@ -1454,7 +1454,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
> >> >  			    num_chunks);
> >> >  		ctx->progress = start_delayed_progress(
> >> >  			progress_title.buf,
> >> > -			num_chunks * ctx->commits.nr);
> >> > +			(uint64_t)num_chunks * ctx->commits.nr);
> >> 
> >> Hmph, do we need this?  I understand that the second parameter to
> >> the callee is u64, so the caller needs to come up with u64 without
> >> overflow, but doesn't that automatically get promoted?
> >
> > Neither num_chunks nor ctx->commits.nr is promoted because both of
> > them are int. The result of `num_chunks * ctx->commits.nr' will be int
> > and will be promoted to u64 to pass to caller.
> 
> Ah, yes.  Thanks.
> 
> The commit title is about "integer multiplication", but can the same
> issue arise with addition and subtraction as well, by the way?

Yes, the same issue will arise with all binary (and ternary) arithmetic operators (+, -, *, /, %, ^, &, |, <<, >> and ?:).

IIRC, gcc doesn't have any warning for this kind of issue.

Microsoft Visual Studio (2017+) has C26451 for this. https://docs.microsoft.com/en-us/visualstudio/code-quality/c26451?view=vs-2017 If our friends at Microsoft could help, we can check the remaining one in our codebase.

-- 
Danh
Previous: Junio C HamanoNext: Johannes Schindelin
Message 6 of 15 in “ident.c: fix LGTM warning on the possible abuse of the '=' operator”
  1. 30/32 ident.c: fix LGTM warning on the possible abuse of the '=' operatorElia Pinto, Nov 4, 2019
  2. 31/32 commit-graph.c: fix code that could convert the result of an integer multiplication to a larger typeElia Pinto, Nov 4, 2019
  3. Junio C HamanoNov 6, 2019
  4. Danh DoanNov 7, 2019
  5. Junio C HamanoNov 7, 2019
  6. Danh DoanNov 7, 2019
  7. Johannes SchindelinNov 7, 2019
  8. Johannes SchindelinNov 7, 2019
  9. 32/32 date.c: fix code that may overflow 'int' before it is converted to 'time_t'Elia Pinto, Nov 4, 2019
  10. SZEDER GáborNov 4, 2019
  11. Elia PintoNov 4, 2019
  12. Phillip WoodNov 4, 2019
  13. Elia PintoNov 4, 2019
  14. Junio C HamanoNov 6, 2019
  15. Johannes SchindelinNov 6, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.