This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/59870] New: Gcc should warn conversion from "const char*" to "char*"
- From: "chengniansun at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 18 Jan 2014 07:11:00 +0000
- Subject: [Bug c/59870] New: Gcc should warn conversion from "const char*" to "char*"
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59870
Bug ID: 59870
Summary: Gcc should warn conversion from "const char*" to
"char*"
Product: gcc
Version: 4.9.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: chengniansun at gmail dot com
I think Gcc should warn the conversion from a const char* to a char* (i.e.,
assigning a string literal to a pointer of type "char *"). The intent of such a
warning is to notify the developers to be careful about any modification made
to the string, which will trigger a segmentation fault.
The following shows an example ("unwarned-const-qualifier-removal.c"). Gcc
silently compiles the program while the trunk version of Clang warns the
conversion. It will be pretty useful for developers, especially for beginners
and students learning C as the triggered segmentation fault may be hard for
them to debug and comprehend.
$: cat unwarned-const-qualifier-removal.c
#include <stdio.h>
int main() {
char* s = "test";
s[0] = 'a';
printf("%s\n", s);
return 0;
}
$: gcc-trunk -Wall -Wextra -std=c99 -pedantic
unwarned-const-qualifier-removal.c
$: ./a.out
Segmentation fault (core dumped)
$: gcc-trunk -v
Using built-in specs.
COLLECT_GCC=gcc-trunk
COLLECT_LTO_WRAPPER=/home/chengniansun/tools/gcc-trunk-binaries/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-trunk/configure --enable-languages=c,c++
--disable-multilib --prefix=/home/chengniansun/tools/gcc-trunk-binaries
Thread model: posix
gcc version 4.9.0 20140109 (experimental) (GCC)
$: clang-trunk -Weverything -std=c99 -pedantic
unwarned-const-qualifier-removal.c
unwarned-const-qualifier-removal.c:4:8: warning: initializing 'char *' with an
expression of type 'const char [5]' discards
qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
char* s = "test";
^ ~~~~~~
1 warning generated.