Topic: Defect Report: Jump past initialization of local static variable


Author: Kerch Holt <kerch@cup.hp.com>
Date: Thu, 1 Apr 2004 16:05:27 +0000 (UTC)
Raw View
[ Note: Forwarded to C++ Committee. -sdc ]

When jumping past initialization of a local static variable the
value of the static becomes indeterminate. Seems like this
behavior should be illegal just as it is for local variables
with automatic linkage.

Here is an example:

struct X {
    X(int i) : x(i) {}
    int x;
};
int f(int c) {
    if (c)
        goto ly;    // error here for jumping past next stmt.
    static X a = 1;
ly:
    return a.x;  // either 1 or 0 depending on implementation.
}

6.7 stmt.decl P3 should be changed to:
"A program that jumps 77)  from a point where local variable with automatic
or static storage duration is not in scope to a point where it is in
scope is
ill-formed unless the variable has POD type (3.9) and is declared without
an initiailizer  (8.5)."
Note the addition of "or static storage duration".
This would imply "static X a = 1;" should be flagged as an error.
Note that this behavior a may be a "quality of implemenation issue"
which may be covered in 6.7 P4.  Paragraph 4 seems to make the
choice of static/dynamic initialization indeterminate.
Making this an error and thus determinate seems the correct thing
to do since that is what is already required of automatic variables.

--
Kerch Holt                 kerch@cup.hp.com
HP Java, Compilers, and Tools Lab (JCTL)
11000 Wolfe Rd. MS 4023
Cupertino, CA    95014     408-447-0421



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: eric_backus@alum.mit.edu ("Eric Backus")
Date: Fri, 2 Apr 2004 00:25:47 +0000 (UTC)
Raw View
"Kerch Holt" <kerch@cup.hp.com> wrote in message
news:406B755F.6070309@cup.hp.com...
> [ Note: Forwarded to C++ Committee. -sdc ]
>
> When jumping past initialization of a local static variable the
> value of the static becomes indeterminate. Seems like this
> behavior should be illegal just as it is for local variables
> with automatic linkage.
>
> Here is an example:
>
> struct X {
>     X(int i) : x(i) {}
>     int x;
> };
> int f(int c) {
>     if (c)
>         goto ly;    // error here for jumping past next stmt.
>     static X a = 1;
> ly:
>     return a.x;  // either 1 or 0 depending on implementation.
> }
>
> 6.7 stmt.decl P3 should be changed to:
> "A program that jumps 77)  from a point where local variable with
automatic
> or static storage duration is not in scope to a point where it is in
> scope is
> ill-formed unless the variable has POD type (3.9) and is declared without
> an initiailizer  (8.5)."
> Note the addition of "or static storage duration".
> This would imply "static X a = 1;" should be flagged as an error.
> Note that this behavior a may be a "quality of implemenation issue"
> which may be covered in 6.7 P4.  Paragraph 4 seems to make the
> choice of static/dynamic initialization indeterminate.
> Making this an error and thus determinate seems the correct thing
> to do since that is what is already required of automatic variables.

Should it be an error even if the variable is not used after the jump?  Your
proposal says the following is invalid, even though I believe it otherwise
works fine:

int f(int c)
{
    switch (c)
    {
    case 0:
        static int a = 1;
        return a;
    case 1:
        static int b = 2;
        return b;
    }
    return 0;
}

Certainly a compiler should warn about this potentially questionable code,
but that's a quality of implementation thing.

--
Eric Backus

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: thp@cs.ucr.edu
Date: Sun, 4 Apr 2004 18:24:47 +0000 (UTC)
Raw View
Kerch Holt <kerch@cup.hp.com> wrote:
+
+ [ Note: Forwarded to C++ Committee. -sdc ]
+
+ When jumping past initialization of a local static variable the
+ value of the static becomes indeterminate. Seems like this
+ behavior should be illegal just as it is for local variables
+ with automatic linkage.
+
+ Here is an example:
+
+ struct X {
+    X(int i) : x(i) {}
+    int x;
+ };
+ int f(int c) {
+    if (c)
+        goto ly;    // error here for jumping past next stmt.
+    static X a = 1;
+ ly:
+    return a.x;  // either 1 or 0 depending on implementation.
+ }
+
+ 6.7 stmt.decl P3 should be changed to:
+ "A program that jumps 77)  from a point where local variable with automatic
+ or static storage duration is not in scope to a point where it is in
+ scope is
+ ill-formed unless the variable has POD type (3.9) and is declared without
+ an initiailizer  (8.5)."
+ Note the addition of "or static storage duration".
+ This would imply "static X a = 1;" should be flagged as an error.
+ Note that this behavior a may be a "quality of implemenation issue"
+ which may be covered in 6.7 P4.  Paragraph 4 seems to make the
+ choice of static/dynamic initialization indeterminate.
+ Making this an error and thus determinate seems the correct thing
+ to do since that is what is already required of automatic variables.

Both the suggestion and the quoted passage from 6.7 raise the question
of how it can be known at compile time whether or not such a jump will
occur at run time.

Tom Payne

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]