1st point was to use PUWSTR_C instead of wchar_t * for the cast, still
stands.
2nd point was to »to avoid further warnings« and i would like to – once
again – vote to avoid union casts.

As far as i know there is no reason to believe that -Wcast-qual (as well as
-Wsystem-headers) will be a default warning anytime soon. And even then it
would be just a warning but still standards-conforming code (which is also
a reason for it not to become a default warning).

A C-style cast is perfectly appropriate here, no union needed.

FTR, it’s not only Clang which doesn’t accept the pre-patch code, GCC
errors too.

Best, Norbert.

On Sun, Jun 4, 2017 at 12:18 PM Mateusz Mikuła <mati...@gmail.com> wrote:

> Anything to improve?
>
> From 05bc4cbc93f5f9942fc28a578dc1afa68d69daa2 Mon Sep 17 00:00:00 2001
> From: Mateusz Mikula <mati...@gmail.com>
> Date: Sun, 4 Jun 2017 11:33:22 +0200
> Subject: [PATCH] cast ua_wcschr and ua_wcsrchr returns when WSTR_ALIGNED is
>  true
>
> Clang doesn't allow implicit conversion form "const wchar_t *" to
> "PUWSTR_C" (aka wchar_t *)
>
> Signed-off-by: Mateusz Mikula <mati...@gmail.com>
> ---
>  mingw-w64-headers/include/stralign.h | 15 +++++++++++++--
>  1 file changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/mingw-w64-headers/include/stralign.h
> b/mingw-w64-headers/include/stralign.h
> index 9b5637d6..4b157c27 100644
> --- a/mingw-w64-headers/include/stralign.h
> +++ b/mingw-w64-headers/include/stralign.h
> @@ -117,12 +117,23 @@ extern "C" {
>    size_t ua_wcslen(PCUWSTR String);
>
>  #ifndef __CRT__NO_INLINE
> +    union {
> +      wchar_t *wcharPointer;
> +      const wchar_t *constWcharPointer;
> +    } cast;
> +
>    __CRT_INLINE PUWSTR_C ua_wcschr(PCUWSTR String,WCHAR Character) {
> -    if(WSTR_ALIGNED(String)) return wcschr((PCWSTR)String,Character);
> +    if(WSTR_ALIGNED(String)) {
> +      cast.constWcharPointer = wcschr((PCWSTR)String,Character);
> +      return cast.wcharPointer;
> +    }
>      return (PUWSTR_C)uaw_wcschr(String,Character);
>    }
>    __CRT_INLINE PUWSTR_C ua_wcsrchr(PCUWSTR String,WCHAR Character) {
> -    if(WSTR_ALIGNED(String)) return wcsrchr((PCWSTR)String,Character);
> +    if(WSTR_ALIGNED(String)) {
> +      cast.constWcharPointer = wcsrchr((PCWSTR)String,Character);
> +      return cast.wcharPointer;
> +    }
>      return (PUWSTR_C)uaw_wcsrchr(String,Character);
>    }
>  #if defined(__cplusplus) && defined(_WConst_Return)
> --
> 2.12.1
>
>
>
> ------ Original Message ------
> Subject: Re: [Mingw-w64-public] [PATCH] stralign: cast ua_wcschr and
> ua_wcsrchr returns to wchar_t *
> Date: Thu, 6 Apr 2017 11:48:24 +0200
> To: Mingw-w64-public
> From: Kai Tietz
> > A cast via union looks like this:
> > type1 *foo(const type1 *my_const_ptr)
> > {
> > union {
> >   type1 *t1;
> >   const type1 *ct1;
> > } v;
> > v.ct1 = my_const_ptr;
> > return v.t1;
> > }
> >
> > The advantage of such a pattern is that no type conversion
> > errors/warnings are shown.  So for casting from const to none-const,
> > this variant is to be preferred.  (and it works for C, and C++!!!)
> >
> > Cheers,
> > Kai
> >
> > 2017-04-05 15:51 GMT+02:00 Mateusz Mikuła <mati...@gmail.com>:
> >>> Hmm, using here "wchar_t *" as cast looks wrong.  Actually we should
> >>> use anyway PUWSTR_C instead.
> >> I noticed it a bit too late and sent another patch casting to PUWSTR_C.
> >>> Nevertheless we can have here a const/none-const conversion (means
> >>> const specifiers for C-runtime function isn't regarded right?).  I
> >>> would suggest to introduce a union-cast instead to avoid further
> >>> warnings instead.
> >> Conversion from const pointer to normal pointer is definitely unsafe but
> >> that's probably what GCC just did.
> >> I'm unsure what you mean by "union-cast" but you can commit your fix.
> >>
> >>
> >>
> ------------------------------------------------------------------------------
> >> Check out the vibrant tech community on one of the world's most
> >> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> >> _______________________________________________
> >> Mingw-w64-public mailing list
> >> Mingw-w64-public@lists.sourceforge.net
> >> https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
> >>
> >
> ------------------------------------------------------------------------------
> > Check out the vibrant tech community on one of the world's most
> > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> > _______________________________________________
> > Mingw-w64-public mailing list
> > Mingw-w64-public@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Mingw-w64-public mailing list
> Mingw-w64-public@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to