This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug middle-end/10138] warn for uninitialized arrays passed as const* arguments
- From: "bangerth at dealii dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 20 Aug 2007 16:21:52 -0000
- Subject: [Bug middle-end/10138] warn for uninitialized arrays passed as const* arguments
- References: <bug-10138-1038@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Comment #16 from bangerth at dealii dot org 2007-08-20 16:21 -------
(In reply to comment #15)
> Of course, the output is '5' and not '0'. So yes, atoi() seems perfectly able
> to initialize buf. (or perhaps, I am still confused).
I think you are. This program here segfaults:
---------------------
#include <cstdio>
const int i=0;
void use(const int *a)
{
int * b = (int *)a;
b[0] = 5;
}
int main(void)
{
use(&i);
printf("%d\n", i);
return 0;
}
----------------------------
Since use() (like atoi) can't know whether its argument is a local automatic
or a global variable, there is no way for it to reliably initialize its
argument. Casting away constness invokes undefined behavior if the static
type of the object is const.
W.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10138