{"thread":{"id":"44445","subject":"[PATCH] sequencer: silence -Wtautological-constant-out-of-range-compare","startedAt":"2016-11-09T03:57:34Z","lastAt":"2016-11-09T13:55:08Z","messageCount":2,"participants":["Jeff King","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"305611","messageId":"20161109035728.v2mqvtj4ep4dj74j@sigill.intra.peff.net","threadId":"44445","inReplyTo":null,"subject":"[PATCH] sequencer: silence -Wtautological-constant-out-of-range-compare","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2016-11-09T03:57:28Z","receivedAt":"2016-11-09T03:57:34Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"When clang compiles sequencer.c, it complains:\n\n  sequencer.c:632:14: warning: comparison of constant 2 with\n    expression of type 'const enum todo_command' is always\n    true [-Wtautological-constant-out-of-range-compare]\n          if (command < ARRAY_SIZE(todo_command_strings))\n\nThis is because \"command\" is an enum that may only have two\nvalues (0 and 1) and the array in question has two elements.\n\nAs it turns out, clang is actually wrong here, at least\naccording to its own bug tracker:\n\n  https://llvm.org/bugs/show_bug.cgi?id=16154\n\nBut it's still worth working around this, as the warning is\npresent with -Wall, meaning we fail compilation with \"make\nDEVELOPER=1\".\n\nCasting the enum to size_t sufficiently unconfuses clang. As\na bonus, it also catches any possible out-of-bounds access\nif the enum takes on a negative value (which shouldn't\nhappen either, but again, this is a defensive check).\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nI know that a different fix is coming in a follow-on series, but I think\nit's worth doing this to un-break clang on master (and v2.11) in the\nmeantime.\n\n sequencer.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/sequencer.c b/sequencer.c\nindex 5fd75f30d..6f0ff9e41 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -629,7 +629,7 @@ static const char *todo_command_strings[] = {\n \n static const char *command_to_string(const enum todo_command command)\n {\n-\tif (command < ARRAY_SIZE(todo_command_strings))\n+\tif ((size_t)command < ARRAY_SIZE(todo_command_strings))\n \t\treturn todo_command_strings[command];\n \tdie(\"Unknown command: %d\", command);\n }\n-- \n2.11.0.rc0.263.g6f44bc3\n"},{"id":"305631","messageId":"alpine.DEB.2.20.1611091453580.72596@virtualbox","threadId":"44445","inReplyTo":"20161109035728.v2mqvtj4ep4dj74j@sigill.intra.peff.net","subject":"Re: [PATCH] sequencer: silence -Wtautological-constant-out-of-range-compare","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2016-11-09T13:54:58Z","receivedAt":"2016-11-09T13:55:08Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Peff,\n\nOn Tue, 8 Nov 2016, Jeff King wrote:\n\n> diff --git a/sequencer.c b/sequencer.c\n> index 5fd75f30d..6f0ff9e41 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -629,7 +629,7 @@ static const char *todo_command_strings[] = {\n>  \n>  static const char *command_to_string(const enum todo_command command)\n>  {\n> -\tif (command < ARRAY_SIZE(todo_command_strings))\n> +\tif ((size_t)command < ARRAY_SIZE(todo_command_strings))\n>  \t\treturn todo_command_strings[command];\n>  \tdie(\"Unknown command: %d\", command);\n\nI have come to prefer a slightly different approach. Will send it out in a\nmoment.\n\nCiao,\nDscho\n"}]}