{"thread":{"id":"42221","subject":"unary minus operator applied to unsigned type, result still unsigned?","startedAt":"2016-05-04T22:59:23Z","lastAt":"2016-05-05T21:39:23Z","messageCount":3,"participants":["Philip Oakley","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"285541","messageId":"74B59A4DF45C45AF9CDCF6662DDAEF1E@PhilipOakley","threadId":"42221","inReplyTo":null,"subject":"unary minus operator applied to unsigned type, result still unsigned?","fromName":"Philip Oakley","fromEmail":"philipoakley@iee.org","sentAt":"2016-05-04T22:59:23Z","receivedAt":"2016-05-04T22:59:23Z","isPatch":false,"sender":{"key":"philipoakley@iee.email","avatar":"https://avatars.githubusercontent.com/u/914343?v=4"},"body":"I'm working on building Git on Visual Studio as part of the Git for Windows\ncapability.\n\nThe MSVC compiler is reporting:\n\n1>..\\sha1-lookup.c(100) : warning C4146: unary minus operator applied to\nunsigned type, result still unsigned\n\n1>..\\sha1-lookup.c(316) : warning C4146: unary minus operator applied to\nunsigned type, result still unsigned\n\nthe two lines of code are the same, and the message suggests a bad return \nvalue:\n\n#100: return -lo-1;\n\n#316: return -lo-1;\n\n\nShould these be protected by an appropriate brackets/calculation (-1-lo) ? \nOr does\ngcc use an alternate assumption about unary minus conversion for functions\nreturning int?\n\n--\n\nPhilip\n"},{"id":"285544","messageId":"xmqqoa8lcswz.fsf@gitster.mtv.corp.google.com","threadId":"42221","inReplyTo":"74B59A4DF45C45AF9CDCF6662DDAEF1E@PhilipOakley","subject":"Re: unary minus operator applied to unsigned type, result still unsigned?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-05-04T23:19:56Z","receivedAt":"2016-05-04T23:19:56Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Philip Oakley\" <philipoakley@iee.org> writes:\n\n> I'm working on building Git on Visual Studio as part of the Git for Windows\n> capability.\n>\n> The MSVC compiler is reporting:\n>\n> 1>..\\sha1-lookup.c(100) : warning C4146: unary minus operator applied to\n> unsigned type, result still unsigned\n\n> 1>..\\sha1-lookup.c(316) : warning C4146: unary minus operator applied to\n> unsigned type, result still unsigned\n>\n> the two lines of code are the same, and the message suggests a bad\n> return value:\n>\n> #100: return -lo-1;\n>\n> #316: return -lo-1;\n>\n>\n> Should these be protected by an appropriate brackets/calculation\n> (-1-lo) ? Or does\n> gcc use an alternate assumption about unary minus conversion for functions\n> returning int?\n\nI think -lo-1 (which is carried out as unsigned) is further casted\nback to signed int as part of the return from the function whose\ntype is signed int.  This may be portable in practice, but changing\ntype of lo to signed int may be prudent.\n\nsha1_pos() uses size_t, but returns \"int\", which is inconsistent\n(size_t could be much larger).  Either using int for hi/lo/mi, or\nusing ssize_t and returning ssize_t would make sense.\n"},{"id":"285606","messageId":"D165B5E4FA8947DD8A5F18B7A15B02BC@PhilipOakley","threadId":"42221","inReplyTo":"xmqqoa8lcswz.fsf@gitster.mtv.corp.google.com","subject":"Re: unary minus operator applied to unsigned type, result still unsigned?","fromName":"Philip Oakley","fromEmail":"philipoakley@iee.org","sentAt":"2016-05-05T21:39:23Z","receivedAt":"2016-05-05T21:39:23Z","isPatch":false,"sender":{"key":"philipoakley@iee.email","avatar":"https://avatars.githubusercontent.com/u/914343?v=4"},"body":"From: \"Junio C Hamano\" <gitster@pobox.com>\n> \"Philip Oakley\" <philipoakley@iee.org> writes:\n>\n>> I'm working on building Git on Visual Studio as part of the Git for \n>> Windows\n>> capability.\n>>\n>> The MSVC compiler is reporting:\n>>\n>> 1>..\\sha1-lookup.c(100) : warning C4146: unary minus operator applied to\n>> unsigned type, result still unsigned\n>\n>> 1>..\\sha1-lookup.c(316) : warning C4146: unary minus operator applied to\n>> unsigned type, result still unsigned\n>>\n>> the two lines of code are the same, and the message suggests a bad\n>> return value:\n>>\n>> #100: return -lo-1;\n>>\n>> #316: return -lo-1;\n>>\n>>\n>> Should these be protected by an appropriate brackets/calculation\n>> (-1-lo) ? Or does\n>> gcc use an alternate assumption about unary minus conversion for \n>> functions\n>> returning int?\n>\n> I think -lo-1 (which is carried out as unsigned) is further casted\n> back to signed int as part of the return from the function whose\n> type is signed int.  This may be portable in practice, but changing\n> type of lo to signed int may be prudent.\n>\n> sha1_pos() uses size_t, but returns \"int\", which is inconsistent\n> (size_t could be much larger).  Either using int for hi/lo/mi, or\n> using ssize_t and returning ssize_t would make sense.\n>\n\nI'm going to hold off on patching this at the moment, as there are a few \nmore signed/unsigned mismatches that are reported but I've been ignoring \n(mainly comparisons), so I'll try and catch them all at the same time, \nespecially as I now understand the standards issues.\n\nThis thread has a summary of the standards \nhttp://stackoverflow.com/questions/8026694/c-unary-minus-operator-behavior-with-unsigned-operands \n(includes C++ >> C) while \nhttps://msdn.microsoft.com/en-us/library/4kh09110(v=vs.90).aspx explains \nwhat is being reported.\n\ni.e. unary minus on unsigned ints produces the 2's complement + 1 value, \nexcept for 0x8000h, where that would be out of range. It's an oddball of \npartially defined behaviours.\n--\nPhilip\n"}]}