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

Re: unary minus operator applied to unsigned type, result still unsigned?

From
Junio C Hamano <gitster@pobox.com>
Date
May 4, 2016, 23:19 UTC
Message-ID
<xmqqoa8lcswz.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<74B59A4DF45C45AF9CDCF6662DDAEF1E@PhilipOakley>
"Philip Oakley" <philipoakley@iee.org> writes:
Show 7 quoted lines
> I'm working on building Git on Visual Studio as part of the Git for Windows
> capability.
>
> The MSVC compiler is reporting:
>
> 1>..\sha1-lookup.c(100) : warning C4146: unary minus operator applied to
> unsigned type, result still unsigned
Show 15 quoted lines
> 1>..\sha1-lookup.c(316) : warning C4146: unary minus operator applied to
> unsigned type, result still unsigned
>
> the two lines of code are the same, and the message suggests a bad
> return value:
>
> #100: return -lo-1;
>
> #316: return -lo-1;
>
>
> Should these be protected by an appropriate brackets/calculation
> (-1-lo) ? Or does
> gcc use an alternate assumption about unary minus conversion for functions
> returning int?

I think -lo-1 (which is carried out as unsigned) is further casted back to signed int as part of the return from the function whose type is signed int. This may be portable in practice, but changing type of lo to signed int may be prudent.

sha1_pos() uses size_t, but returns "int", which is inconsistent (size_t could be much larger). Either using int for hi/lo/mi, or using ssize_t and returning ssize_t would make sense.

Previous: Philip OakleyNext: Philip Oakley
Message 2 of 3 in “unary minus operator applied to unsigned type, result still unsigned?”
  1. Philip OakleyMay 4, 2016
  2. Junio C HamanoMay 4, 2016
  3. Philip OakleyMay 5, 2016

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.