git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Use unsigned char to squash compiler warnings
@ 2015-03-02 19:22 Ben Walton
  2015-03-02 20:29 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Ben Walton @ 2015-03-02 19:22 UTC (permalink / raw)
  To: gitster; +Cc: git, Ben Walton

Sun Studio on Solaris issues warnings about improper initialization
values being used when defining tolower_trans_tbl in
ctype.c. tolower_trans_tbl is defined as char[], which studio's
compiler defaults to signed char[] due to the Solaris ABI. To resolve
this, instead of supplying -xchar or another option at build time,
declare tolower_trans_tbl as unsigned char.  Update all appropriate
references to the new type.

Signed-off-by: Ben Walton <bdwalton@gmail.com>
---
 ctype.c           | 2 +-
 git-compat-util.h | 2 +-
 kwset.c           | 8 ++++----
 kwset.h           | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/ctype.c b/ctype.c
index 0bfebb4..fc0225c 100644
--- a/ctype.c
+++ b/ctype.c
@@ -30,7 +30,7 @@ const unsigned char sane_ctype[256] = {
 };
 
 /* For case-insensitive kwset */
-const char tolower_trans_tbl[256] = {
+const unsigned char tolower_trans_tbl[256] = {
 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
diff --git a/git-compat-util.h b/git-compat-util.h
index 3455c5e..5eae2b2 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -694,7 +694,7 @@ static inline size_t xsize_t(off_t len)
 }
 
 /* in ctype.c, for kwset users */
-extern const char tolower_trans_tbl[256];
+extern const unsigned char tolower_trans_tbl[256];
 
 /* Sane ctype - no locale, and works with signed chars */
 #undef isascii
diff --git a/kwset.c b/kwset.c
index a0f49b3..e6236a0 100644
--- a/kwset.c
+++ b/kwset.c
@@ -80,13 +80,13 @@ struct kwset
   struct trie *next[NCHAR];	/* Table of children of the root. */
   char *target;			/* Target string if there's only one. */
   int mind2;			/* Used in Boyer-Moore search for one string. */
-  char const *trans;		/* Character translation table. */
+  unsigned char const *trans;  /* Character translation table. */
 };
 
 /* Allocate and initialize a keyword set object, returning an opaque
    pointer to it.  Return NULL if memory is not available. */
 kwset_t
-kwsalloc (char const *trans)
+kwsalloc (unsigned char const *trans)
 {
   struct kwset *kwset;
 
@@ -381,7 +381,7 @@ kwsprep (kwset_t kws)
   register struct kwset *kwset;
   register int i;
   register struct trie *curr;
-  register char const *trans;
+  register unsigned char const *trans;
   unsigned char delta[NCHAR];
 
   kwset = (struct kwset *) kws;
@@ -590,7 +590,7 @@ cwexec (kwset_t kws, char const *text, size_t len, struct kwsmatch *kwsmatch)
   register int d;
   register char const *end, *qlim;
   register struct tree const *tree;
-  register char const *trans;
+  register unsigned char const *trans;
 
   accept = NULL;
 
diff --git a/kwset.h b/kwset.h
index a21b2ea..61a134f 100644
--- a/kwset.h
+++ b/kwset.h
@@ -39,7 +39,7 @@ typedef struct kwset_t* kwset_t;
    if enough memory cannot be obtained.  The argument if non-NULL
    specifies a table of character translations to be applied to all
    pattern and search text. */
-extern kwset_t kwsalloc(char const *);
+extern kwset_t kwsalloc(unsigned char const *);
 
 /* Incrementally extend the keyword set to include the given string.
    Return NULL for success, or an error message.  Remember an index
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Use unsigned char to squash compiler warnings
  2015-03-02 19:22 [PATCH] Use unsigned char to squash compiler warnings Ben Walton
@ 2015-03-02 20:29 ` Junio C Hamano
       [not found]   ` <CAP30j1684A=QsA0o+zLMP70V09mFTS_MtiPZB2TrRywYK2NWWw@mail.gmail.com>
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2015-03-02 20:29 UTC (permalink / raw)
  To: Ben Walton; +Cc: git

Ben Walton <bdwalton@gmail.com> writes:

> Sun Studio on Solaris issues warnings about improper initialization
> values being used when defining tolower_trans_tbl in
> ctype.c. tolower_trans_tbl is defined as char[], which studio's
> compiler defaults to signed char[] due to the Solaris ABI. To resolve
> this, instead of supplying -xchar or another option at build time,
> declare tolower_trans_tbl as unsigned char.  Update all appropriate
> references to the new type.
>
> Signed-off-by: Ben Walton <bdwalton@gmail.com>
> ---
>  ctype.c           | 2 +-
>  git-compat-util.h | 2 +-
>  kwset.c           | 8 ++++----
>  kwset.h           | 2 +-
>  4 files changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/ctype.c b/ctype.c
> index 0bfebb4..fc0225c 100644
> --- a/ctype.c
> +++ b/ctype.c
> @@ -30,7 +30,7 @@ const unsigned char sane_ctype[256] = {
>  };
>  
>  /* For case-insensitive kwset */
> -const char tolower_trans_tbl[256] = {
> +const unsigned char tolower_trans_tbl[256] = {
>  	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
>  	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
>  	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,

It is not obvious from the context but later elements in this array
have values above 0x7f.  So you are saying your compiler complains
when you write:

	signed char ch = 0xff;

which sort of makes sense (because you actually are storing -1 not
255 to the variable).  Throughout our codebase (and kwset is a
borrowed code that does not count as "our" codebase ;-) we do use
unsigned when we mean we want 255 and not -1, and this patch fixes
that borrowed code to be in line with the rest.

The conversion looked good from a cursory view; I didn't check it
very carefully though.

Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Use unsigned char to squash compiler warnings
       [not found]   ` <CAP30j1684A=QsA0o+zLMP70V09mFTS_MtiPZB2TrRywYK2NWWw@mail.gmail.com>
@ 2015-03-04 22:11     ` Junio C Hamano
  2015-03-04 22:34       ` Randall S. Becker
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2015-03-04 22:11 UTC (permalink / raw)
  To: Ben Walton; +Cc: git

Ben Walton <bdwalton@gmail.com> writes:

> On Mon, Mar 2, 2015 at 8:30 PM Junio C Hamano <gitster@pobox.com> wrote:
>
>> The conversion looked good from a cursory view; I didn't check it
>> very carefully though.
>>
> Yes, because of the Solaris ABI, the Studio compiler defaults char to
> signed char.

Doesn't our beloved GCC also uses signed char when you write char?
You keep saying that "defaults to signed char is the problem", but
that does not explain why those in the rest of the world outside the
Solaris land do not encounter this problem.

	$ cat >x.c <<\EOF
        #include <stdio.h>
        int main (void) {
                SIGNED char ch = 0xff;
                printf("%d\n", ch);
                return 0;
        }
	EOF
        $ gcc -Wall -DSIGNED= x.c && ./a.out
        -1
        $ gcc -Wall -DSIGNED=signed x.c && ./a.out
	-1

I think th problem is not Solaris uses signed char for char like
everybody else does ;-) but it gives a fairly useless warning to
annoy people.

In any case, here is what I queued, FYI, on bw/kwset-use-unsigned
topic.

Thanks.

commit 189c860c9ec5deb95845c056ca5c15b58970158e
Author: Ben Walton <bdwalton@gmail.com>
Date:   Mon Mar 2 19:22:31 2015 +0000

    kwset: use unsigned char to store values with high-bit set
    
    Sun Studio on Solaris issues warnings about improper initialization
    values being used when defining tolower_trans_tbl[] in ctype.c.  The
    array wants to store values with high-bit set and treat them as
    values between 128 to 255.  Unlike the rest of the Git codebase
    where we explicitly specify 'unsigned char' for such variables and
    arrays, however, kwset code we borrowed from elsewhere uses 'char'
    for this and other variables.
    
    Fix the declarations to explicitly use 'unsigned char' where
    necessary to bring it in line with the rest of the Git.
    
    Signed-off-by: Ben Walton <bdwalton@gmail.com>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH] Use unsigned char to squash compiler warnings
  2015-03-04 22:11     ` Junio C Hamano
@ 2015-03-04 22:34       ` Randall S. Becker
  0 siblings, 0 replies; 4+ messages in thread
From: Randall S. Becker @ 2015-03-04 22:34 UTC (permalink / raw)
  To: 'Junio C Hamano', 'Ben Walton'; +Cc: git

>On 4 Mar 2015, Junio C Hamano Wrote:
> Sent: March 4, 2015 5:11 PM
> To: Ben Walton
> Cc: git@vger.kernel.org
> Subject: Re: [PATCH] Use unsigned char to squash compiler warnings
> 
> Ben Walton <bdwalton@gmail.com> writes:
> 
> > On Mon, Mar 2, 2015 at 8:30 PM Junio C Hamano <gitster@pobox.com>
> wrote:
> >
> >> The conversion looked good from a cursory view; I didn't check it
> >> very carefully though.
> >>
> > Yes, because of the Solaris ABI, the Studio compiler defaults char to
> > signed char.
> 
> Doesn't our beloved GCC also uses signed char when you write char?
> You keep saying that "defaults to signed char is the problem", but that
does not
> explain why those in the rest of the world outside the Solaris land do not
> encounter this problem.
> 
> 	$ cat >x.c <<\EOF
>         #include <stdio.h>
>         int main (void) {
>                 SIGNED char ch = 0xff;
>                 printf("%d\n", ch);
>                 return 0;
>         }
> 	EOF
>         $ gcc -Wall -DSIGNED= x.c && ./a.out
>         -1
>         $ gcc -Wall -DSIGNED=signed x.c && ./a.out
> 	-1
> 
> I think th problem is not Solaris uses signed char for char like everybody
else
> does ;-) but it gives a fairly useless warning to annoy people.
> 
> In any case, here is what I queued, FYI, on bw/kwset-use-unsigned topic.

Even the NonStop c99 compiler does not report a warning - and it is usually
very noisy. The default is unsigned char for c99 on this platform, and the
value interpretation is significant.

#include <stdio.h>

int main (void) {
        char ch0 = 0xff;
        signed char ch1 = 0xff;
        unsigned char ch = 0xff;
        printf("%d, %d, %d, %d, %d\n", ch0, ch, ch1, ch==ch0, ch==ch1);
        return 0;
}
255, 255, -1, 1, 0

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-03-04 22:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-02 19:22 [PATCH] Use unsigned char to squash compiler warnings Ben Walton
2015-03-02 20:29 ` Junio C Hamano
     [not found]   ` <CAP30j1684A=QsA0o+zLMP70V09mFTS_MtiPZB2TrRywYK2NWWw@mail.gmail.com>
2015-03-04 22:11     ` Junio C Hamano
2015-03-04 22:34       ` Randall S. Becker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).