{"thread":{"id":"38681","subject":"[PATCH] Use unsigned char to squash compiler warnings","startedAt":"2015-03-02T19:22:31Z","lastAt":"2015-03-04T22:34:23Z","messageCount":4,"participants":["Ben Walton","Junio C Hamano","Randall S. Becker"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"256858","messageId":"1425324151-5480-1-git-send-email-bdwalton@gmail.com","threadId":"38681","inReplyTo":null,"subject":"[PATCH] Use unsigned char to squash compiler warnings","fromName":"Ben Walton","fromEmail":"bdwalton@gmail.com","sentAt":"2015-03-02T19:22:31Z","receivedAt":"2015-03-02T19:22:31Z","isPatch":true,"sender":{"key":"bdwalton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/396061?v=4"},"body":"Sun Studio on Solaris issues warnings about improper initialization\nvalues being used when defining tolower_trans_tbl in\nctype.c. tolower_trans_tbl is defined as char[], which studio's\ncompiler defaults to signed char[] due to the Solaris ABI. To resolve\nthis, instead of supplying -xchar or another option at build time,\ndeclare tolower_trans_tbl as unsigned char.  Update all appropriate\nreferences to the new type.\n\nSigned-off-by: Ben Walton <bdwalton@gmail.com>\n---\n ctype.c           | 2 +-\n git-compat-util.h | 2 +-\n kwset.c           | 8 ++++----\n kwset.h           | 2 +-\n 4 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/ctype.c b/ctype.c\nindex 0bfebb4..fc0225c 100644\n--- a/ctype.c\n+++ b/ctype.c\n@@ -30,7 +30,7 @@ const unsigned char sane_ctype[256] = {\n };\n \n /* For case-insensitive kwset */\n-const char tolower_trans_tbl[256] = {\n+const unsigned char tolower_trans_tbl[256] = {\n \t0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,\n \t0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,\n \t0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex 3455c5e..5eae2b2 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -694,7 +694,7 @@ static inline size_t xsize_t(off_t len)\n }\n \n /* in ctype.c, for kwset users */\n-extern const char tolower_trans_tbl[256];\n+extern const unsigned char tolower_trans_tbl[256];\n \n /* Sane ctype - no locale, and works with signed chars */\n #undef isascii\ndiff --git a/kwset.c b/kwset.c\nindex a0f49b3..e6236a0 100644\n--- a/kwset.c\n+++ b/kwset.c\n@@ -80,13 +80,13 @@ struct kwset\n   struct trie *next[NCHAR];\t/* Table of children of the root. */\n   char *target;\t\t\t/* Target string if there's only one. */\n   int mind2;\t\t\t/* Used in Boyer-Moore search for one string. */\n-  char const *trans;\t\t/* Character translation table. */\n+  unsigned char const *trans;  /* Character translation table. */\n };\n \n /* Allocate and initialize a keyword set object, returning an opaque\n    pointer to it.  Return NULL if memory is not available. */\n kwset_t\n-kwsalloc (char const *trans)\n+kwsalloc (unsigned char const *trans)\n {\n   struct kwset *kwset;\n \n@@ -381,7 +381,7 @@ kwsprep (kwset_t kws)\n   register struct kwset *kwset;\n   register int i;\n   register struct trie *curr;\n-  register char const *trans;\n+  register unsigned char const *trans;\n   unsigned char delta[NCHAR];\n \n   kwset = (struct kwset *) kws;\n@@ -590,7 +590,7 @@ cwexec (kwset_t kws, char const *text, size_t len, struct kwsmatch *kwsmatch)\n   register int d;\n   register char const *end, *qlim;\n   register struct tree const *tree;\n-  register char const *trans;\n+  register unsigned char const *trans;\n \n   accept = NULL;\n \ndiff --git a/kwset.h b/kwset.h\nindex a21b2ea..61a134f 100644\n--- a/kwset.h\n+++ b/kwset.h\n@@ -39,7 +39,7 @@ typedef struct kwset_t* kwset_t;\n    if enough memory cannot be obtained.  The argument if non-NULL\n    specifies a table of character translations to be applied to all\n    pattern and search text. */\n-extern kwset_t kwsalloc(char const *);\n+extern kwset_t kwsalloc(unsigned char const *);\n \n /* Incrementally extend the keyword set to include the given string.\n    Return NULL for success, or an error message.  Remember an index\n-- \n1.9.1\n"},{"id":"256865","messageId":"xmqqoaob9m82.fsf@gitster.dls.corp.google.com","threadId":"38681","inReplyTo":"1425324151-5480-1-git-send-email-bdwalton@gmail.com","subject":"Re: [PATCH] Use unsigned char to squash compiler warnings","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-03-02T20:29:49Z","receivedAt":"2015-03-02T20:29:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ben Walton <bdwalton@gmail.com> writes:\n\n> Sun Studio on Solaris issues warnings about improper initialization\n> values being used when defining tolower_trans_tbl in\n> ctype.c. tolower_trans_tbl is defined as char[], which studio's\n> compiler defaults to signed char[] due to the Solaris ABI. To resolve\n> this, instead of supplying -xchar or another option at build time,\n> declare tolower_trans_tbl as unsigned char.  Update all appropriate\n> references to the new type.\n>\n> Signed-off-by: Ben Walton <bdwalton@gmail.com>\n> ---\n>  ctype.c           | 2 +-\n>  git-compat-util.h | 2 +-\n>  kwset.c           | 8 ++++----\n>  kwset.h           | 2 +-\n>  4 files changed, 7 insertions(+), 7 deletions(-)\n>\n> diff --git a/ctype.c b/ctype.c\n> index 0bfebb4..fc0225c 100644\n> --- a/ctype.c\n> +++ b/ctype.c\n> @@ -30,7 +30,7 @@ const unsigned char sane_ctype[256] = {\n>  };\n>  \n>  /* For case-insensitive kwset */\n> -const char tolower_trans_tbl[256] = {\n> +const unsigned char tolower_trans_tbl[256] = {\n>  \t0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,\n>  \t0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,\n>  \t0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,\n\nIt is not obvious from the context but later elements in this array\nhave values above 0x7f.  So you are saying your compiler complains\nwhen you write:\n\n\tsigned char ch = 0xff;\n\nwhich sort of makes sense (because you actually are storing -1 not\n255 to the variable).  Throughout our codebase (and kwset is a\nborrowed code that does not count as \"our\" codebase ;-) we do use\nunsigned when we mean we want 255 and not -1, and this patch fixes\nthat borrowed code to be in line with the rest.\n\nThe conversion looked good from a cursory view; I didn't check it\nvery carefully though.\n\nThanks.\n"},{"id":"257032","messageId":"xmqqd24o76rh.fsf@gitster.dls.corp.google.com","threadId":"38681","inReplyTo":"CAP30j1684A=QsA0o+zLMP70V09mFTS_MtiPZB2TrRywYK2NWWw@mail.gmail.com","subject":"Re: [PATCH] Use unsigned char to squash compiler warnings","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-03-04T22:11:14Z","receivedAt":"2015-03-04T22:11:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ben Walton <bdwalton@gmail.com> writes:\n\n> On Mon, Mar 2, 2015 at 8:30 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n>> The conversion looked good from a cursory view; I didn't check it\n>> very carefully though.\n>>\n> Yes, because of the Solaris ABI, the Studio compiler defaults char to\n> signed char.\n\nDoesn't our beloved GCC also uses signed char when you write char?\nYou keep saying that \"defaults to signed char is the problem\", but\nthat does not explain why those in the rest of the world outside the\nSolaris land do not encounter this problem.\n\n\t$ cat >x.c <<\\EOF\n        #include <stdio.h>\n        int main (void) {\n                SIGNED char ch = 0xff;\n                printf(\"%d\\n\", ch);\n                return 0;\n        }\n\tEOF\n        $ gcc -Wall -DSIGNED= x.c && ./a.out\n        -1\n        $ gcc -Wall -DSIGNED=signed x.c && ./a.out\n\t-1\n\nI think th problem is not Solaris uses signed char for char like\neverybody else does ;-) but it gives a fairly useless warning to\nannoy people.\n\nIn any case, here is what I queued, FYI, on bw/kwset-use-unsigned\ntopic.\n\nThanks.\n\ncommit 189c860c9ec5deb95845c056ca5c15b58970158e\nAuthor: Ben Walton <bdwalton@gmail.com>\nDate:   Mon Mar 2 19:22:31 2015 +0000\n\n    kwset: use unsigned char to store values with high-bit set\n    \n    Sun Studio on Solaris issues warnings about improper initialization\n    values being used when defining tolower_trans_tbl[] in ctype.c.  The\n    array wants to store values with high-bit set and treat them as\n    values between 128 to 255.  Unlike the rest of the Git codebase\n    where we explicitly specify 'unsigned char' for such variables and\n    arrays, however, kwset code we borrowed from elsewhere uses 'char'\n    for this and other variables.\n    \n    Fix the declarations to explicitly use 'unsigned char' where\n    necessary to bring it in line with the rest of the Git.\n    \n    Signed-off-by: Ben Walton <bdwalton@gmail.com>\n    Signed-off-by: Junio C Hamano <gitster@pobox.com>\n"},{"id":"257033","messageId":"002301d056cb$5d86f5a0$1894e0e0$@nexbridge.com","threadId":"38681","inReplyTo":"xmqqd24o76rh.fsf@gitster.dls.corp.google.com","subject":"RE: [PATCH] Use unsigned char to squash compiler warnings","fromName":"Randall S. Becker","fromEmail":"rsbecker@nexbridge.com","sentAt":"2015-03-04T22:34:23Z","receivedAt":"2015-03-04T22:34:23Z","isPatch":true,"sender":{"key":"randall.becker@nexbridge.ca","avatar":"https://avatars.githubusercontent.com/u/28956764?v=4"},"body":">On 4 Mar 2015, Junio C Hamano Wrote:\n> Sent: March 4, 2015 5:11 PM\n> To: Ben Walton\n> Cc: git@vger.kernel.org\n> Subject: Re: [PATCH] Use unsigned char to squash compiler warnings\n> \n> Ben Walton <bdwalton@gmail.com> writes:\n> \n> > On Mon, Mar 2, 2015 at 8:30 PM Junio C Hamano <gitster@pobox.com>\n> wrote:\n> >\n> >> The conversion looked good from a cursory view; I didn't check it\n> >> very carefully though.\n> >>\n> > Yes, because of the Solaris ABI, the Studio compiler defaults char to\n> > signed char.\n> \n> Doesn't our beloved GCC also uses signed char when you write char?\n> You keep saying that \"defaults to signed char is the problem\", but that\ndoes not\n> explain why those in the rest of the world outside the Solaris land do not\n> encounter this problem.\n> \n> \t$ cat >x.c <<\\EOF\n>         #include <stdio.h>\n>         int main (void) {\n>                 SIGNED char ch = 0xff;\n>                 printf(\"%d\\n\", ch);\n>                 return 0;\n>         }\n> \tEOF\n>         $ gcc -Wall -DSIGNED= x.c && ./a.out\n>         -1\n>         $ gcc -Wall -DSIGNED=signed x.c && ./a.out\n> \t-1\n> \n> I think th problem is not Solaris uses signed char for char like everybody\nelse\n> does ;-) but it gives a fairly useless warning to annoy people.\n> \n> In any case, here is what I queued, FYI, on bw/kwset-use-unsigned topic.\n\nEven the NonStop c99 compiler does not report a warning - and it is usually\nvery noisy. The default is unsigned char for c99 on this platform, and the\nvalue interpretation is significant.\n\n#include <stdio.h>\n\nint main (void) {\n        char ch0 = 0xff;\n        signed char ch1 = 0xff;\n        unsigned char ch = 0xff;\n        printf(\"%d, %d, %d, %d, %d\\n\", ch0, ch, ch1, ch==ch0, ch==ch1);\n        return 0;\n}\n255, 255, -1, 1, 0\n"}]}