Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid casting away const in a couple of silly places #22300

Merged
merged 2 commits into from
Jun 18, 2024

Conversation

tonycoz
Copy link
Contributor

@tonycoz tonycoz commented Jun 17, 2024

See the commit messages for how to reproduce the cast-qual warnings.

Noticed this while reviewing Perl#22271.

If you build with:

  ./Configure -des -Dusedevel -Ud_strnlen -Accflags=-Wcast-qual -Dcc=g++

you will see amongst many other warnings:

inline.h: In function ‘size_t Perl_my_strnlen(const char*, size_t)’:
inline.h:4220:23: warning: cast from type ‘const void*’ to type ‘char*’ casts away qualifiers [-Wcast-qual]
 4220 |     const char *end = (char *) memchr(str, '\0', maxlen);
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The cast itself is only needed on C++ to cast from "const void *", C
doesn't require any cast here.
Seen with:

  ./Configure -des -Dusedevel -Accflags=-Wcast-qual

mathoms.c: In function ‘Perl_utf8_to_uvchr’:
utf8.h:804:53: warning: cast discards ‘const’ qualifier from pointer target type
 [-Wcast-qual]
  804 |            (UNLIKELY(s[0] == '\0') ? 1 : my_strnlen((char *) (s), UTF8SK
IP(s)))
      |                                                     ^
embed.h:792:87: note: in definition of macro ‘utf8_to_uvchr_buf_helper’
  792 | # define utf8_to_uvchr_buf_helper(a,b,c)        Perl_utf8_to_uvchr_buf_h
elper(aTHX_ a,b,c)
      |
              ^
mathoms.c:970:12: note: in expansion of macro ‘utf8_to_uvchr_buf’
  970 |     return utf8_to_uvchr_buf(s, s + UTF8_CHK_SKIP(s), retlen);
      |            ^~~~~~~~~~~~~~~~~
mathoms.c:970:37: note: in expansion of macro ‘UTF8_CHK_SKIP’
  970 |     return utf8_to_uvchr_buf(s, s + UTF8_CHK_SKIP(s), retlen);
      |                                     ^~~~~~~~~~~~~

mathoms.c seems to be the only user of this macro.

This came up when reviewing Perl#22271
@tonycoz tonycoz merged commit ca81be3 into Perl:blead Jun 18, 2024
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants