Bug 33483 - New warning suggestion (for -Wall): sizeof() with non-lvalue has side effects that will not execute at runtime
Summary: New warning suggestion (for -Wall): sizeof() with non-lvalue has side effects...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.2
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2007-09-18 22:50 UTC by Yuri
Modified: 2023-08-25 00:15 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-09-19 03:13:48


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yuri 2007-09-18 22:50:09 UTC
#include <iostream>
using namespace std;
Here is a very dangerous situation that compiler can catch illustrated by the following example: when sizeof has non-lvalue argument.

--- example ---

int f() {
  cout << "I am f" << endl;
  return 5;
}

int main() {
  cout << "sizeof=" << sizeof(f()) << endl; // HERE THE WARNING SHOULD BE ISSUED
  return (0);
}
Comment 1 Andrew Pinski 2007-09-18 22:53:08 UTC
Well when VLA's get added, sometimes sizeof will have side effects.
An example:
sizeof(int[n++])
Comment 2 Yuri 2007-09-18 22:58:30 UTC
Warning should be issued in this case as well. Since this isn't an expected behavior to ignore "n++".
Comment 3 Wolfgang Bangerth 2007-09-19 03:13:48 UTC
Confirmed. Using sizeof with an argument that appears to have side effects
but isn't evaluated seems like an error waiting to happen.
W.
Comment 4 Martin Sebor 2015-10-11 23:08:10 UTC
It might makes sense to provide the functionality under the -Wunevaluated-expression option for compatibility with clang:

$ cat u.c && clang -Wall -c u.c
int foo (int i)
{
    int a [i];
    return sizeof (a [i++]);
}
u.c:4:19: warning: expression with side effects has no effect in an unevaluated
      context [-Wunevaluated-expression]
    return sizeof (a [i++]);
                  ^
1 warning generated.