Re: Cast "const char *" pointers to "char *" to avoid compiler warnings.

Christophe Lyon <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <CAKdteOYYF=VhYBL=wEguO8q+207Kx--BZjr4i874se0VCpeuFQ@mail.gmail.com>
On Tue, 2 Oct 2018 at 11:26, Richard Earnshaw (lists)
<[email protected]> wrote:
>
> On 01/10/18 22:33, Christophe Lyon wrote:
> > Hi,
> >
> > GCC complains that some assignments loose the const-ness of several
> > data. This small patch adds explicit (char *) casts, but I'm not
> > familiar enough with what newlib does with these to be sure that they
> > are not modified. Maybe the proper fix would be to declare the
> > destinations as "const"?
> >
> > Christophe
> >
> >
> > newlib-4.txt
> >
> >
> > commit 349a08c43cd4baf0d93f28ea8ca7351bf9606d50
> > Author: Christophe Lyon <[email protected]>
> > Date:   Mon Oct 1 18:53:37 2018 +0000
> >
> >     Cast "const char *" pointers to "char *" to avoid compiler warnings.
> >
> >     2018-10-01  Christophe Lyon  <[email protected]>
> >
> >       * newlib/libc/ctype/ctype_.c (__set_ctype): Cast "_ctype_" to "char *".
> >       * newlib/libc/ctype/jp2uc.c (_jp2uc_l, _uc2jp_l): Cast output of
> >       "__locale_charset()" and "__current_locale_charset()" to "char *".
> >       * newlib/libc/locale/locale.c (__loadlocale): Cast "new_locale" to
> >       "char *".
> >
> > diff --git a/newlib/libc/ctype/ctype_.c b/newlib/libc/ctype/ctype_.c
> > index 28727e8..851fc06 100644
> > --- a/newlib/libc/ctype/ctype_.c
> > +++ b/newlib/libc/ctype/ctype_.c
> > @@ -176,7 +176,7 @@ __set_ctype (struct __locale_t *loc, const char *charset)
> >  #  if defined(ALLOW_NEGATIVE_CTYPE_INDEX)
> >       ctype_ptr = _ctype_b;
> >  #  else
> > -     ctype_ptr = _ctype_;
> > +     ctype_ptr = (char *) _ctype_;
> >  #  endif
> >      }
> >  #  if defined(ALLOW_NEGATIVE_CTYPE_INDEX)
> > diff --git a/newlib/libc/ctype/jp2uc.c b/newlib/libc/ctype/jp2uc.c
> > index b89b5ea..00272eb 100644
> > --- a/newlib/libc/ctype/jp2uc.c
> > +++ b/newlib/libc/ctype/jp2uc.c
> > @@ -166,7 +166,7 @@ __uc2jp (wint_t c, int type)
> >  wint_t
> >  _jp2uc_l (wint_t c, struct __locale_t * l)
> >  {
> > -  char * cs = l ? __locale_charset(l) : __current_locale_charset();
> > +  char * cs = l ? (char *) __locale_charset(l) : (char *) __current_locale_charset();
>
> Why not change cs to const char *?  It's only used to call strcmp.
>
> Probably most other casts should be similarly considered.
>
> Generally, this patch feels wrong, IMO.
>

OK, I've split the patch in two parts, then:
- one for jp2uc.c following Richard's suggestion
- one for the other two files.

In locale.c, I add a (char *) cast for new_locale just like what is
already done a few lines above for the Cygwin case.
In ctype_.c, it's similar in that it adds the cast to effectively
behave like in the Cygwin case.
I've noticed the comment saying that with Cygwin the values maybe
overwritten hence the absence of 'const', but
I don't know what happens for non-Cygwin environments: maybe the
proper fix would be to remove 'const' when
declaring _ctype_ and new_locale ?

> R.
>
> >    if (0 == strcmp (cs, "JIS"))
> >      c = __jp2uc (c, JP_JIS);
> >    else if (0 == strcmp (cs, "SJIS"))
> > @@ -186,7 +186,7 @@ _jp2uc (wint_t c)
> >  wint_t
> >  _uc2jp_l (wint_t c, struct __locale_t * l)
> >  {
> > -  char * cs = l ? __locale_charset(l) : __current_locale_charset();
> > +  char * cs = l ? (char *) __locale_charset(l) : (char *) __current_locale_charset();
> >    if (0 == strcmp (cs, "JIS"))
> >      c = __uc2jp (c, JP_JIS);
> >    else if (0 == strcmp (cs, "SJIS"))
> > diff --git a/newlib/libc/locale/locale.c b/newlib/libc/locale/locale.c
> > index 791a775..79da35f 100644
> > --- a/newlib/libc/locale/locale.c
> > +++ b/newlib/libc/locale/locale.c
> > @@ -515,7 +515,7 @@ restart:
> >      }
> >  # define FAIL        goto restart
> >  #else
> > -  locale = new_locale;
> > +  locale = (char *) new_locale;
> >  # define FAIL        return NULL
> >  #endif
> >
> >
>
newlib-6.txt (text/plain, 1.3 KB)
commit 2f4f47d593d6bdbd62510afe4c3c2298615d5762
Author: Christophe Lyon <[email protected]>
Date:   Fri Oct 5 09:11:05 2018 +0000

    Declare "cs" variable as "const char *"
    
    Instead of "char *" to avoid compiler warnings.
    This is OK because "cs" is only used as input of strcmp.
    
    2018-10-01  Christophe Lyon  <[email protected]>
    
    	* newlib/libc/ctype/jp2uc.c (_jp2uc_l, _uc2jp_l): Declare "cs" as
    	const.

diff --git a/newlib/libc/ctype/jp2uc.c b/newlib/libc/ctype/jp2uc.c
index b89b5ea..5e30f09 100644
--- a/newlib/libc/ctype/jp2uc.c
+++ b/newlib/libc/ctype/jp2uc.c
@@ -166,7 +166,7 @@ __uc2jp (wint_t c, int type)
 wint_t
 _jp2uc_l (wint_t c, struct __locale_t * l)
 {
-  char * cs = l ? __locale_charset(l) : __current_locale_charset();
+  const char * cs = l ? __locale_charset(l) : __current_locale_charset();
   if (0 == strcmp (cs, "JIS"))
     c = __jp2uc (c, JP_JIS);
   else if (0 == strcmp (cs, "SJIS"))
@@ -186,7 +186,7 @@ _jp2uc (wint_t c)
 wint_t
 _uc2jp_l (wint_t c, struct __locale_t * l)
 {
-  char * cs = l ? __locale_charset(l) : __current_locale_charset();
+  const char * cs = l ? __locale_charset(l) : __current_locale_charset();
   if (0 == strcmp (cs, "JIS"))
     c = __uc2jp (c, JP_JIS);
   else if (0 == strcmp (cs, "SJIS"))
newlib-7.txt (text/plain, 1.2 KB)
commit c6af2b7be0e94935c589307c14eeb9f53901a811
Author: Christophe Lyon <[email protected]>
Date:   Mon Oct 1 18:53:37 2018 +0000

    Cast "const char *" pointers to "char *" to avoid compiler warnings.
    
    2018-10-01  Christophe Lyon  <[email protected]>
    
    	* newlib/libc/ctype/ctype_.c (__set_ctype): Cast "_ctype_" to "char *".
    	* newlib/libc/locale/locale.c (__loadlocale): Cast "new_locale" to
    	"char *".

diff --git a/newlib/libc/ctype/ctype_.c b/newlib/libc/ctype/ctype_.c
index 28727e8..851fc06 100644
--- a/newlib/libc/ctype/ctype_.c
+++ b/newlib/libc/ctype/ctype_.c
@@ -176,7 +176,7 @@ __set_ctype (struct __locale_t *loc, const char *charset)
 #  if defined(ALLOW_NEGATIVE_CTYPE_INDEX)
      ctype_ptr = _ctype_b;
 #  else
-     ctype_ptr = _ctype_;
+     ctype_ptr = (char *) _ctype_;
 #  endif
     }
 #  if defined(ALLOW_NEGATIVE_CTYPE_INDEX)
diff --git a/newlib/libc/locale/locale.c b/newlib/libc/locale/locale.c
index 791a775..79da35f 100644
--- a/newlib/libc/locale/locale.c
+++ b/newlib/libc/locale/locale.c
@@ -515,7 +515,7 @@ restart:
     }
 # define FAIL	goto restart
 #else
-  locale = new_locale;
+  locale = (char *) new_locale;
 # define FAIL	return NULL
 #endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.