GCC Bugzilla has been upgraded from version 4.4.9 to 5.0rc3. If you see any problem, please report it to bug 64968.
Bug 33086 - warn for read-only uninitialized variables passed as arguments
Summary: warn for read-only uninitialized variables passed as arguments
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.3.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: 10138
  Show dependency treegraph
 
Reported: 2007-08-16 10:49 UTC by Manuel López-Ibáñez
Modified: 2007-08-20 14:47 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Manuel López-Ibáñez 2007-08-16 10:49:59 UTC
void use(const int *);

void foo(void)
{
  int i;
  use(&i);
}

At least for languages where 'const' is actually enforced, we should warn for this. For languages where the 'const' can be cast away and 'i' can be initialized by 'use' the situation is less clear (although personally I think we should warn anyway). This is one part of PR10138.

"the question whether an argument is actually used or not is secondary, the fact that we pass an uninitialized variable to which only read access is possible 
is definitely worth a warning." http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10138#c8
Comment 1 Andrew Pinski 2007-08-16 10:59:21 UTC
> At least for languages where 'const' is actually enforced

There is none, unless you are taking about fortran "in" arguments.  So we need to mark such argument as special.

Now if you have the full program (or at least the containts of use function), and you can prove it never writes to the incoming pointer argument, then you can warn but only then.

In C and C++ you can never tell without the body of use.
Comment 2 Manuel López-Ibáñez 2007-08-16 11:19:49 UTC
(In reply to comment #1)
> > At least for languages where 'const' is actually enforced
> 
> There is none, 

void use(const int *a)
{
    a[0] = 5;
}
void foo(void)
{
  int i;
  use(&i);
}

new.c:3: error: assignment of read-only location

Either I am misunderstanding you or your argument about overwriting the pointer argument is equivalent to just don't using the value of 'i'. Of course, we don't know whether the value is used or not within use() but the fact is that 'i' cannot be initialized within use().
Comment 3 Andrew Pinski 2007-08-16 14:58:22 UTC

void use(const int *a)
{
  int *b = (int*)a;
    b[0] = 5;
}
void foo(void)
{
  int i;
  use(&i);
}
Comment 4 Manuel López-Ibáñez 2007-08-17 10:15:27 UTC
(In reply to comment #3)
> 
> void use(const int *a)
> {
>   int *b = (int*)a;

Andrew, you are right. I tend to forget how fragile is 'const', even in C++. So, then this is invalid and thus it is PR10138.
Comment 5 Manuel López-Ibáñez 2007-08-20 14:47:12 UTC
Andrew, what about functions marked with attribute "pure" ?

int atoi(const char *) __attribute__ ((pure));