develooper Front page | perl.perl5.porters | Postings from November 2016

[perl #130169] Fix const correctness for header files

Thread Next
From:
Petr Pisar
Date:
November 24, 2016 18:11
Subject:
[perl #130169] Fix const correctness for header files
Message ID:
rt-4.0.24-31563-1480011059-888.130169-75-0@perl.org
# New Ticket Created by  Petr Pisar 
# Please include the string:  [perl #130169]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=130169 >


Hello,

when building a code that includes

#include "EXTERN.h
#include "perl.h"

with GCC's -Wcast-qual option, I get many warnings about discarding const
qualifiers like this:

$ printf '#include "EXTERN.h"\n#include "perl.h"\n' | gcc -Wcast-qual -I/usr/lib64/perl5/CORE -c -x c -
In file included from /usr/lib64/perl5/CORE/hv.h:629:0,
                 from /usr/lib64/perl5/CORE/perl.h:3740,
                 from <stdin>:2:
/usr/lib64/perl5/CORE/hv_func.h: In function ‘S_perl_hash_siphash_2_4’:
/usr/lib64/perl5/CORE/hv_func.h:213:17: warning: cast discards ‘const’ qualifier from pointer target type [-Wcast-qual]
   U64TYPE k0 = ((U64TYPE*)seed)[0];
                 ^

This makes difficult to spot issues in non-perl code and people complain
(https://bugzilla.redhat.com/show_bug.cgi?id=1242980).

Series of three patches is attached that tries to fix this issue:

First patch fixes hash functions in hv_func.h. Because their input is already
const and their output does not rely pointers to input data, this patch seems
simple and non-controversial.

Second patch touches utf8.h and utfebcdic.h. It changes dereferencing string
bytes in is_UTF8-* check macros. While these are macros without argument types,
I think its reasonable to assume the input string does not change during the
check, so the change is desired. The only unusual thing is the changed code
was originally generated by a script, then hand-editted, and then comes my
patch. I did not try to locate the disfunctional (or deleted?) generator in
regen/regcharclass.pl and ammend it.

The third and last patch touches utf8_hop* functions. This is the most
controversal change as it changes their prototype. I cannot see a way how to
get rid of the warnings there without changing prototype. I looked into glibc
sources how char *strchr(const char *, int) is solved. It isn't. It also
produces many warnings. Here I doubt about helpfullness of the -Wcast-qual
warnings.

-- Petr

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About