"tweak" <> wrote in message news

JpHc.787$rr.721@fed1read02...
> Barry Schwarz wrote:
> > On Thu, 08 Jul 2004 18:22:16 -0700, tweak <>
> > wrote:
[..]
> I tried different sizes of the buffer array, and I was able to generate
> a segmentation fault. Since the compiler tells me everything is okay,
> and since the software appears to work, are there any audit tools to
> check for problems like this one, where the compiler doesn't warn of
> a potential problem (beyond just the malloc() ones listed in the FAQ)?
>
lint may be helpful. See below.
> I'm sure you grep or use an equivalent tool in the many different
> OS's we have. But are there any portable tools?
>
> Of course, writing ISO C99 code that is portable is preferred, but you
> can miss stuff as the number of lines of code and number of files increase.
>
> Brian
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <errno.h>
>
> int
> main(void)
> {
>
> char *ptr = NULL;
> char buffer[256]; /* intentionally did not initialize */
> int y = 0;
>
> if ( ( ptr = malloc(1) ) == 0 ) {
> perror("malloc() problem");
> exit(1);
> }
>
> for ( int i; i < 255; i++ )
i is uninitialized. UB.
> buffer[i] = 'Z';
>
> while ( (ptr[y] = buffer[y]) != '\0' )
> y++;
> (void)printf("ptr is: %s\n", ptr);
> free(ptr);
>
> return 0;
> }
F:\>splint overflow.c
Splint 3.0.1.6 --- 11 Feb 2002
overflow2.c: (in function main)
overflow2.c(15,1

: Unrecognized identifier: NULL
Identifier used in code has not been declared. (Use -unrecog to inhibit
warning)
overflow2.c(19,19): Unrecognized identifier: malloc
overflow2.c(20,10): Unrecognized identifier: perror
overflow2.c(21,10): Unrecognized identifier: exit
overflow2.c(27,15): Index of possibly null pointer ptr: ptr
A possibly null pointer is dereferenced. Value is either the result of a
function which may return null (in which case, code should check it is not
null), or a global, parameter or structure field declared with the null
qualifier. (Use -nullderef to inhibit warning)
overflow2.c(19,19): Storage ptr may become null
overflow2.c(27,24): Value buffer[] used before definition
An rvalue is used that may not be initialized to a value on some execution
path. (Use -usedef to inhibit warning)
overflow2.c(29,12): Unrecognized identifier: printf
overflow2.c(30,6): Unrecognized identifier: free
Finished checking --- 8 code warnings