public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/38335]  New: Code warning
@ 2008-11-30 18:52 adam dot c dot scott at gmail dot com
  2008-11-30 19:51 ` [Bug c++/38335] " rguenth at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: adam dot c dot scott at gmail dot com @ 2008-11-30 18:52 UTC (permalink / raw)
  To: gcc-bugs

Add warning about modifying an index in a for loop.

Without this warning the kind of errors introduced in code are likely to be
very difficult to debug (core dump).

Example code to reproduce below.  Current commandline used to compile: -ansi
-pedantic -Wall -O.

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
    int loopndx;
    int indexes[10];

    for( loopndx=0 ; loopndx <=10 ; loopndx++) {
        if (loopndx==5) {
            loopndx=666666;
        }
        cout << indexes[loopndx];
    }
    return (EXIT_SUCCESS);
}


-- 
           Summary: Code warning
           Product: gcc
           Version: 3.4.4
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: adam dot c dot scott at gmail dot com
 GCC build triplet: dmd
  GCC host triplet: cyg
GCC target triplet: gdc


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38335


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/38335] Code warning
  2008-11-30 18:52 [Bug c++/38335] New: Code warning adam dot c dot scott at gmail dot com
@ 2008-11-30 19:51 ` rguenth at gcc dot gnu dot org
  2008-12-01  6:32 ` adam dot c dot scott at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-11-30 19:51 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 660 bytes --]



------- Comment #1 from rguenth at gcc dot gnu dot org  2008-11-30 19:50 -------
You mean like

g++ -S -O2 t.C -Wall
t.C: In function ‘int main(int, char**)’:
t.C:12: warning: array subscript is above array bounds

?  Seriously, there is too many code around modifying the induction variable
in a valid way.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38335


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/38335] Code warning
  2008-11-30 18:52 [Bug c++/38335] New: Code warning adam dot c dot scott at gmail dot com
  2008-11-30 19:51 ` [Bug c++/38335] " rguenth at gcc dot gnu dot org
@ 2008-12-01  6:32 ` adam dot c dot scott at gmail dot com
  2008-12-01  6:48 ` adam dot c dot scott at gmail dot com
  2008-12-24  1:31 ` [Bug middle-end/38335] " pinskia at gcc dot gnu dot org
  3 siblings, 0 replies; 5+ messages in thread
From: adam dot c dot scott at gmail dot com @ 2008-12-01  6:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from adam dot c dot scott at gmail dot com  2008-12-01 06:30 -------
(In reply to comment #0)

> Add warning about modifying an index in a for loop.
> 
> Without this warning the kind of errors introduced in code are likely to be
> very difficult to debug (core dump).
> 
> Example code to reproduce below.  Current commandline used to compile: -ansi
> -pedantic -Wall -O.
> 
> #include <iostream>
> using namespace std;
> 
> int main(int argc, char** argv) {
>     int loopndx;
>     int indexes[10];
> 
>     for( loopndx=0 ; loopndx <=10 ; loopndx++) {
>         if (loopndx==5) {
>             loopndx=666666;
>         }
>         cout << indexes[loopndx];
>     }
>     return (EXIT_SUCCESS);
> }
> 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38335


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/38335] Code warning
  2008-11-30 18:52 [Bug c++/38335] New: Code warning adam dot c dot scott at gmail dot com
  2008-11-30 19:51 ` [Bug c++/38335] " rguenth at gcc dot gnu dot org
  2008-12-01  6:32 ` adam dot c dot scott at gmail dot com
@ 2008-12-01  6:48 ` adam dot c dot scott at gmail dot com
  2008-12-24  1:31 ` [Bug middle-end/38335] " pinskia at gcc dot gnu dot org
  3 siblings, 0 replies; 5+ messages in thread
From: adam dot c dot scott at gmail dot com @ 2008-12-01  6:48 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1864 bytes --]



------- Comment #3 from adam dot c dot scott at gmail dot com  2008-12-01 06:47 -------
With my version of g++ I didn't get your example warning about subscript.  This
would be great.

In response to your objection...  If any line of code modified the index of a
for loop then why use a for loop?  It would make more sense to use a while
loop.

In structured system design, modifying the index of a for loop is "tight" data
coupling and lacks logical cohesion.  Doing this is on par of a "goto".

Mathematically a for loop implies a series or sequence; interrupting that by
modifying an index violates the semantic of a series.

>From a marketing point of view, if you want new adopters, easier to use
software that gets the job done can never be wrong if you want broad appeal.

The counter is that we want to "haze" developers using the product, making them
stronger, limiting the talent pool, thereby creating Conan programmers :)


Anyone doing this should at least be warned at a verbose warning level.  If
they want to modify the index, they are better off with a while loop.

Really this philosophical viewpoint may need elevation to a product level (what
about a --novice --student --worker --expert --elite warning levels?)





(In reply to comment #1)
> You mean like
> 
> g++ -S -O2 t.C -Wall
> t.C: In function ‘int main(int, char**)’:
> t.C:12: warning: array subscript is above array bounds
> 
> ?  Seriously, there is too many code around modifying the induction variable
> in a valid way.
> 


-- 

adam dot c dot scott at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38335


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/38335] Code warning
  2008-11-30 18:52 [Bug c++/38335] New: Code warning adam dot c dot scott at gmail dot com
                   ` (2 preceding siblings ...)
  2008-12-01  6:48 ` adam dot c dot scott at gmail dot com
@ 2008-12-24  1:31 ` pinskia at gcc dot gnu dot org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-12-24  1:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from pinskia at gcc dot gnu dot org  2008-12-24 01:27 -------
Fixed in 4.3.0 and above which emits at -O2 -Wall -W:
t.cc: In function 'int main(int, char**)':
t.cc:12: warning: array subscript is above array bounds


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
          Component|c++                         |middle-end
         Resolution|                            |FIXED
   Target Milestone|---                         |4.3.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38335


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-12-24  1:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-30 18:52 [Bug c++/38335] New: Code warning adam dot c dot scott at gmail dot com
2008-11-30 19:51 ` [Bug c++/38335] " rguenth at gcc dot gnu dot org
2008-12-01  6:32 ` adam dot c dot scott at gmail dot com
2008-12-01  6:48 ` adam dot c dot scott at gmail dot com
2008-12-24  1:31 ` [Bug middle-end/38335] " pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).