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

Re: [PATCH 02/13] msvc: avoid using minus operator on unsigned types

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Sep 30, 2019, 09:50 UTC
Message-ID
<nycvar.QRO.7.76.6.1909301149070.46@tvgsbejvaqbjf.bet>
In-Reply-To
<20190926235753.GA94990@generichostname>
Hi Denton,
On Thu, 26 Sep 2019, Denton Liu wrote:
Show 71 quoted lines
> On Thu, Sep 26, 2019 at 11:01:32PM +0200, Johannes Schindelin wrote:
> >
> > On Thu, 26 Sep 2019, Denton Liu wrote:
> >
> > > Hi Dscho,
> > >
> > > On Thu, Sep 26, 2019 at 01:30:10AM -0700, Johannes Schindelin via GitGitGadget wrote:
> > > > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> > > >
> > > > MSVC complains about this with `-Wall`, which can be taken as a sign
> > > > that this is indeed a real bug. The symptom is:
> > > >
> > > > 	C4146: unary minus operator applied to unsigned type, result
> > > > 	still unsigned
> > > >
> > > > Let's avoid this warning in the minimal way, e.g. writing `-1 -
> > > > <unsigned value>` instead of `-<unsigned value> - 1`.
> > >
> > > [...]
> > >
> > > > ---
> > > >  read-cache.c  | 4 ++--
> > > >  sha1-lookup.c | 2 +-
> > > >  2 files changed, 3 insertions(+), 3 deletions(-)
> > > >
> > > > diff --git a/read-cache.c b/read-cache.c
> > > > index c701f7f8b8..11f3357216 100644
> > > > --- a/read-cache.c
> > > > +++ b/read-cache.c
> > > > @@ -1276,7 +1276,7 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
> > > >  	 */
> > > >  	if (istate->cache_nr > 0 &&
> > > >  		strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0)
> > > > -		pos = -istate->cache_nr - 1;
> > > > +		pos = -1 - istate->cache_nr;
> > >
> > > I've been thinking about this and I'm still not certain that this 100%
> > > correct from a language-lawyer perspective.
> > >
> > > If we do `-1 - istate->cache_nr`, then the unsignedness of
> > > istate->cache_nr takes over and the whole expression is a very large
> > > unsigned number.
> > >
> > > Then, when we assign to `int pos`, we are converting an unsigned number
> > > which is out of the range of the signed number. According to a
> > > StackOverflow post citing the C99 standard[1]:
> > >
> > > 	Otherwise, the new type is signed and the value cannot be
> > > 	represented in it; either the result is implementation-defined
> > > 	or an implementation-defined signal is raised.
> > >
> > > I'm sure that most platforms that we support will handle it sanely but
> > > could we write this as
> > >
> > > 	pos = -1 - (int) istate->cache_nr;
> > >
> > > to be doubly sure that no funny business will happen?
> >
> > I guess we should use `signed_add_overflows()` to make extra certain
> > that it does what we want it to do, kind of like `st_add()`. Or just do
> > the check explicitly, like so:
> >
> > 	if (istate->cache_nr > INT_MAX)
> > 		die("overflow: -1 - %u", istate->cache_nr);
> > 	pos = -1 - istate->cache_nr;
>
> Could we change this to
>
>  	pos = -1 - (int) istate->cache_nr;
>
> so that we alleviate the problem I was talking about above?
Thank you, I had missed that.
> Other than that, it looks good. Well, it might break on one's complement
> systems but I don't think we support UNIVACs anyway. ;)
Let's hope that nobody tries to run Git on such a system, indeed.

Thanks, Dscho

Show 56 quoted lines
>
> > }
> > >
> > > >  	else
> > > >  		pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
> > > >
> > > > @@ -1894,7 +1894,7 @@ static size_t estimate_cache_size(size_t ondisk_size, unsigned int entries)
> > > >  	/*
> > > >  	 * Account for potential alignment differences.
> > > >  	 */
> > > > -	per_entry += align_padding_size(sizeof(struct cache_entry), -sizeof(struct ondisk_cache_entry));
> > > > +	per_entry += align_padding_size(per_entry, 0);
> > > >  	return ondisk_size + entries * per_entry;
> > > >  }
> > > >
> > > > diff --git a/sha1-lookup.c b/sha1-lookup.c
> > > > index 796ab68da8..c819687730 100644
> > > > --- a/sha1-lookup.c
> > > > +++ b/sha1-lookup.c
> > > > @@ -97,7 +97,7 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
> > > >  			lo = mi + 1;
> > > >  		mi = lo + (hi - lo) / 2;
> > > >  	} while (lo < hi);
> > > > -	return -lo-1;
> > > > +	return -1 - lo;
> > >
> > > Same thing here.
> >
> > This is even more critical, as `lo` has the type `size_t`:
> >
> > 	if (lo > INT_MAX)
> > 		die("overflow: -1 - %"PRIuMAX, (uintmax_t)lo);
> > 	return -1 - lo;
>
> Also, could we change this to
>
>  	return -1 - (int) lo;
>
> Thanks,
>
> Denton
>
> > >
> > What do you think?
> > Dscho
> >
> > > [1]: https://stackoverflow.com/questions/50605/signed-to-unsigned-conversion-in-c-is-it-always-safe
> > >
> > > >  }
> > > >
> > > >  int bsearch_hash(const unsigned char *sha1, const uint32_t *fanout_nbo,
> > > > --
> > > > gitgitgadget
> > > >
> > >
>
Previous: Denton LiuNext: Johannes Schindelin via GitGitGadget
Message 7 of 72 in “ci: include a Visual Studio build & test in our Azure Pipeline”
  1. 00/13 ci: include a Visual Studio build & test in our Azure PipelineJohannes Schindelin via GitGitGadget, Sep 26, 2019
  2. 01/13 push: do not pretend to return `int` from `die_push_simple()`Johannes Schindelin via GitGitGadget, Sep 26, 2019
  3. 02/13 msvc: avoid using minus operator on unsigned typesJohannes Schindelin via GitGitGadget, Sep 26, 2019
  4. Denton LiuSep 26, 2019
  5. Johannes SchindelinSep 26, 2019
  6. Denton LiuSep 26, 2019
  7. Johannes SchindelinSep 30, 2019
  8. 03/13 winansi: use FLEX_ARRAY to avoid compiler warningJohannes Schindelin via GitGitGadget, Sep 26, 2019
  9. 04/13 compat/win32/path-utils.h: add #include guardsJohannes Schindelin via GitGitGadget, Sep 26, 2019
  10. 05/13 msvc: ignore some libraries when linkingJohannes Schindelin via GitGitGadget, Sep 26, 2019
  11. 06/13 msvc: handle DEVELOPER=1Johannes Schindelin via GitGitGadget, Sep 26, 2019
  12. 07/13 msvc: work around a bug in GetEnvironmentVariable()Johannes Schindelin via GitGitGadget, Sep 26, 2019
  13. 08/13 vcxproj: only copy `git-remote-http.exe` once it was builtJohannes Schindelin via GitGitGadget, Sep 26, 2019
  14. 10/13 test-tool run-command: learn to run (parts of) the testsuiteJohannes Schindelin via GitGitGadget, Sep 26, 2019
  15. 09/13 vcxproj: include more generated filesJohannes Schindelin via GitGitGadget, Sep 26, 2019
  16. 11/13 tests: let --immediate and --write-junit-xml play well togetherJohannes Schindelin via GitGitGadget, Sep 26, 2019
  17. Junio C HamanoSep 28, 2019
  18. Johannes SchindelinSep 30, 2019
  19. 12/13 ci: really use shallow clones on Azure PipelinesJohannes Schindelin via GitGitGadget, Sep 26, 2019
  20. 13/13 ci: also build and test with MS Visual Studio on Azure PipelinesJohannes Schindelin via GitGitGadget, Sep 26, 2019
  21. 00/13 ci: include a Visual Studio build & test in our Azure PipelineJohannes Schindelin via GitGitGadget, Sep 30, 2019
  22. 01/13 push: do not pretend to return `int` from `die_push_simple()`Johannes Schindelin via GitGitGadget, Sep 30, 2019
  23. Junio C HamanoOct 3, 2019
  24. Johannes SchindelinOct 4, 2019
  25. 03/13 winansi: use FLEX_ARRAY to avoid compiler warningJohannes Schindelin via GitGitGadget, Sep 30, 2019
  26. 02/13 msvc: avoid using minus operator on unsigned typesJohannes Schindelin via GitGitGadget, Sep 30, 2019
  27. Junio C HamanoOct 3, 2019
  28. Johannes SchindelinOct 4, 2019
  29. Johannes SixtOct 4, 2019
  30. Johannes SchindelinOct 4, 2019
  31. Junio C HamanoOct 6, 2019
  32. Johannes SixtOct 6, 2019
  33. Johannes SchindelinOct 8, 2019
  34. Johannes SixtOct 8, 2019
  35. 05/13 msvc: ignore some libraries when linkingJohannes Schindelin via GitGitGadget, Sep 30, 2019
  36. 06/13 msvc: handle DEVELOPER=1Johannes Schindelin via GitGitGadget, Sep 30, 2019
  37. 04/13 compat/win32/path-utils.h: add #include guardsJohannes Schindelin via GitGitGadget, Sep 30, 2019
  38. 07/13 msvc: work around a bug in GetEnvironmentVariable()Johannes Schindelin via GitGitGadget, Sep 30, 2019
  39. 11/13 tests: let --immediate and --write-junit-xml play well togetherJohannes Schindelin via GitGitGadget, Sep 30, 2019
  40. 13/13 ci: also build and test with MS Visual Studio on Azure PipelinesJohannes Schindelin via GitGitGadget, Sep 30, 2019
  41. 10/13 test-tool run-command: learn to run (parts of) the testsuiteJohannes Schindelin via GitGitGadget, Sep 30, 2019
  42. 08/13 vcxproj: only copy `git-remote-http.exe` once it was builtJohannes Schindelin via GitGitGadget, Sep 30, 2019
  43. 09/13 vcxproj: include more generated filesJohannes Schindelin via GitGitGadget, Sep 30, 2019
  44. 12/13 ci: really use shallow clones on Azure PipelinesJohannes Schindelin via GitGitGadget, Sep 30, 2019
  45. 00/13 ci: include a Visual Studio build & test in our Azure PipelineJohannes Schindelin via GitGitGadget, Oct 4, 2019
  46. 01/13 push: do not pretend to return `int` from `die_push_simple()`Johannes Schindelin via GitGitGadget, Oct 4, 2019
  47. 03/13 winansi: use FLEX_ARRAY to avoid compiler warningJohannes Schindelin via GitGitGadget, Oct 4, 2019
  48. Alban GruinOct 7, 2019
  49. 05/13 msvc: ignore some libraries when linkingJohannes Schindelin via GitGitGadget, Oct 4, 2019
  50. 02/13 msvc: avoid using minus operator on unsigned typesJohannes Schindelin via GitGitGadget, Oct 4, 2019
  51. 08/13 vcxproj: only copy `git-remote-http.exe` once it was builtJohannes Schindelin via GitGitGadget, Oct 4, 2019
  52. 07/13 msvc: work around a bug in GetEnvironmentVariable()Johannes Schindelin via GitGitGadget, Oct 4, 2019
  53. 09/13 vcxproj: include more generated filesJohannes Schindelin via GitGitGadget, Oct 4, 2019
  54. 04/13 compat/win32/path-utils.h: add #include guardsJohannes Schindelin via GitGitGadget, Oct 4, 2019
  55. 12/13 ci: really use shallow clones on Azure PipelinesJohannes Schindelin via GitGitGadget, Oct 4, 2019
  56. 06/13 msvc: handle DEVELOPER=1Johannes Schindelin via GitGitGadget, Oct 4, 2019
  57. 11/13 tests: let --immediate and --write-junit-xml play well togetherJohannes Schindelin via GitGitGadget, Oct 4, 2019
  58. 10/13 test-tool run-command: learn to run (parts of) the testsuiteJohannes Schindelin via GitGitGadget, Oct 4, 2019
  59. 13/13 ci: also build and test with MS Visual Studio on Azure PipelinesJohannes Schindelin via GitGitGadget, Oct 4, 2019
  60. Junio C HamanoOct 6, 2019
  61. Johannes SchindelinOct 6, 2019
  62. Johannes SchindelinOct 6, 2019
  63. Junio C HamanoOct 7, 2019
  64. Johannes SchindelinOct 7, 2019
  65. Junio C HamanoOct 8, 2019
  66. Johannes SchindelinOct 8, 2019
  67. Philip OakleyOct 9, 2019
  68. Johannes SchindelinOct 10, 2019
  69. Philip OakleyOct 10, 2019
  70. Junio C HamanoOct 7, 2019
  71. Thomas GummererOct 7, 2019
  72. Johannes SchindelinOct 11, 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.