Re: C compiler warning messages?

trt@duke.cs.duke.edu (Thomas R. Truscott)
7 Apr 1997 15:07:19 -0400

          From comp.compilers

Related articles
C compiler warning messages? johnr@ims.com (1997-04-06)
Re: C compiler warning messages? trt@duke.cs.duke.edu (1997-04-07)
Proactive Compiler Error Messages cdg@nullstone.com (Christopher Glaeser) (1997-04-08)
Re: C compiler warning messages? morris@CAM.ORG (1997-04-08)
Re: C compiler warning messages? chase@naturalbridge.com (David Chase) (1997-04-11)
Re: C compiler warning messages? joshua@intrinsa.com (1997-04-16)
Re: C compiler warning messages? morris@CAM.ORG (Morris Bernstein) (1997-04-16)
Re: C compiler warning messages? oz@ds9.rnd.border.com (1997-04-18)
| List of all articles for this month |
From: trt@duke.cs.duke.edu (Thomas R. Truscott)
Newsgroups: comp.compilers
Date: 7 Apr 1997 15:07:19 -0400
Organization: SAS Institute Inc.
References: 97-04-042
Keywords: C, errors, practice

Some years ago I sent a compiler vendor the following program, which
their compiler compiled without complaint, and suggested that they
emit some helpful warning messages (they were already emitting plenty
of useless annoying ones). I see that they now issue a warning about
unterminated comments, which is a big improvement :-)


gcc has by far the most helpful warning messages.


Tom Truscott


===================================================
#include <stdio.h>


int main()
{
        char c, *p;
        double x;


        printf("Type ^D, or press return\n");


        /* "comparison of unsigned with negative constant" */
        if ((c = getchar()) != EOF)
printf("You did NOT type ^D\n");
        else
printf("Thank you for typing ^D\n");


        /* note: we included <stdio.h> */
        /* "printf: expected double argument but got integer" */
        /* "printf: argument count mismatch" */
        printf("And remember, %f + %d == %d\n", 2, 2);


        p = "3.14159";
        /* This next warning is particularly debatable. Oh well. */
        /* "include <math.h> in files that invoke the standard C library atof()" */
        x = atof(p);
        printf("Also note that atof(\"%s\") is %g\n", p, x);
        exit(0);


        /* Please DO NOT warn that "c set but not used" */
}


char *subr1(int *countp, int j)
{
        int i;
        char m[100];


        /* "null effect" */
        *countp++;


        /* "evaluation order undefined" */
        j = j++;


        /* "evaluation order undefined" */
        i = j++ * j++;


        /* "evaluation order undefined" */
        printf("%d %d\n", j, j++);


        /* "referencing past end of array" */
        for (i = 1; i <= 100; i++)
m[i] = '\0';


        /* "possible precedence error, parenthesize!" */
        m[1] = 1<<j + 3;


        /* "null effect" */
        m[2] == j;


        /* "null effect" */
        printf("%d\n", sizeof(j++));


        /* "assignment in conditional" (need to come up with a better warning) */
        if (i = 1)
                /* "returning pointer to deallocated storage" */
                return(m);


        /* (need warning about confusion between &/&& and |/||) */
        if (i != 1 & j != 2)
printf("i out of range\n");


        /* (need warning that this (non-trivial) conditional is a constant) */
        if (i == 1 && i == 2)
printf("i is in range\n");


      /* "function has return(e) and return" */
}




int subr2(unsigned int i)
{
        int j;


        /* "comparison of unsigned with negative constant" */
        if (i == -1)
return(0);


        /* "degenerate unsigned comparison" / "comparison always false" */
        if (i < 0)
exit(0);


        /* "degenerate unsigned comparison" */
        if (i <= 0)
/* "null effect" */
exit;


        /*
          * "nested comments not supported"
          * (see note below)
          *
        if (i == 0)
return(0);


        /* this comment terminates the unclosed comment above */


        /* i++; /* Pleease DO NOT warn about one-liners, a common hack */


        /* This next warning is particularly debatable. Oh well. */
        /* "Use "(i = 3) != 0" if assignment is indeed intended" */
        if (i = 3)
i++;


        /* "value used before set" */
        return(j);
}




#include <malloc.h>


struct foo {
        int foo_bar[100];
        struct foo *foo_next;
}




/* "subr3 is declared to return a structure, but none is returned" */
subr3(struct foo a[10])
{
        struct foo *foop;


        /* "sizeof(a) returns sizeof pointer, not sizeof the array" */
        foop = (struct foo *)malloc(sizeof(a));


        /* "There is no check for possible malloc failure" */
        bzero((char *)foop, sizeof(*foop));


        /* "dubious size argument to bzero" */
        bzero((char *)foop, sizeof(foop));


        /* "dubious size argument to memset" */
        bzero((char *)foop, sizeof(*foop), '\0');


        /* "attempting read of size 104 into location of size 4" */
        if (read(0, &foop, sizeof(struct foo)) != sizeof(struct foo))
        {
perror("read error");
exit(1);
        }
        free(foop);
        /* "dereferencing a freed variable" */
        foop = foop->foo_next;
}


--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.