This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


On Tue, 2 Oct 2018 at 11:26, Richard Earnshaw (lists)
<Richard.Earnshaw@arm.com> 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 <christophe.lyon@linaro.org>
> > Date:   Mon Oct 1 18:53:37 2018 +0000
> >
> >     Cast "const char *" pointers to "char *" to avoid compiler warnings.
> >
> >     2018-10-01  Christophe Lyon  <christophe.lyon@linaro.org>
> >
> >       * 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
> >
> >
>

Attachment: newlib-6.txt
Description: Text document

Attachment: newlib-7.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]