gcov produce incorrect numbers for while loops and for loops with no increment part

Bug #837303 reported by Kasper Dupont
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gcc-defaults (Ubuntu)
Won't Fix
Undecided
Unassigned

Bug Description

gcov produce correct execution count for a typical for loop such as:
for (i=0;i<10;++i)

However if the increment part is removed from the for statement and put at the end of the loop instead, the program is equivalent, but gcov in that case will double count the line with the for statement once per time the for loop is reached. That is if the loop was not entered at all, the line where the for loop starts is counted twice.

The same incorrect behaviour is seen with any while loop.

Here is the gcov output from four variations of a program demonstrating incorrect execution counts in three of the four cases.

        -: 1:#include <stdio.h>
        -: 2:
        1: 3:int main()
        -: 4:{
        -: 5: int i;
       11: 6: for (i=0;i<10;++i) {
       10: 7: printf("%d\n",i);
        -: 8: }
        1: 9: return 0;
        -: 10:}

        -: 1:#include <stdio.h>
        -: 2:
        1: 3:int main()
        -: 4:{
        -: 5: int i;
       12: 6: for (i=0;i<10;) {
       10: 7: printf("%d\n",i);
       10: 8: ++i;
        -: 9: }
        1: 10: return 0;
        -: 11:}

        -: 1:#include <stdio.h>
        -: 2:
        1: 3:int main()
        -: 4:{
        1: 5: int i=0;
       12: 6: while (i<10) {
       10: 7: printf("%d\n",i);
       10: 8: ++i;
        -: 9: }
        1: 10: return 0;
        -: 11:}

        -: 1:#include <stdio.h>
        -: 2:
        1: 3:int main()
        -: 4:{
        1: 5: int i=20;
        2: 6: while (i<10) {
    #####: 7: printf("%d\n",i);
    #####: 8: ++i;
        -: 9: }
        1: 10: return 0;
        -: 11:}

ProblemType: Bug
DistroRelease: Ubuntu 10.04
Package: gcc 4:4.4.3-1ubuntu1
ProcVersionSignature: Ubuntu 2.6.32-33.72-generic 2.6.32.41+drm33.18
Uname: Linux 2.6.32-33-generic i686
Architecture: i386
Date: Tue Aug 30 12:47:01 2011
EcryptfsInUse: Yes
InstallationMedia: Ubuntu 10.04.3 LTS "Lucid Lynx" - Release i386 (20110720.1)
ProcEnviron:
 PATH=(custom, user)
 LANG=en_DK.utf8
 SHELL=/bin/bash
SourcePackage: gcc-defaults

Revision history for this message
Kasper Dupont (ubuntu-launchpad-feb) wrote :
Revision history for this message
dino99 (9d9) wrote :

This version has expired long times ago, and so will never get support

Changed in gcc-defaults (Ubuntu):
status: New → Invalid
status: Invalid → New
tags: removed: lucid
Revision history for this message
Matthias Klose (doko) wrote :

closing, feel free to re-open, when this is seen with newer versions.

Changed in gcc-defaults (Ubuntu):
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.