Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.programmer > #1085

Re: C Code "sanity checking" tools

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.unix.programmer
Subject Re: C Code "sanity checking" tools
Date 2011-08-09 10:01 +1200
Message-ID <9ab4m6Fv2pU1@mid.individual.net> (permalink)
References <4e3fc1c6$0$940$c3e8da3$9f400e27@news.astraweb.com> <9a9ukcFc3uU1@mid.individual.net> <0.4f4825ba1afbfe0d8edd.20110808153502BST.87mxfkt1ih.fsf@bsb.me.uk> <9aatodF52gU1@mid.individual.net> <0.d395cec53d92aacc3c94.20110808225103BST.87bovztvw8.fsf@bsb.me.uk>

Show all headers | View raw


On 08/ 9/11 09:51 AM, Ben Bacarisse wrote:
> Ian Collins<ian-news@hotmail.com>  writes:
>
>> On 08/ 9/11 02:35 AM, Ben Bacarisse wrote:
>>> Ian Collins<ian-news@hotmail.com>   writes:
>>>
>>>> On 08/ 8/11 11:00 PM, Martin Crouch wrote:
>>>
>>>>> If I write some code like this:
>>>>>
>>>>> #include<stdio.h>
>>>>> int main(void)
>>>>> {
>>>>>      char mystring[5];
>>>>>
>>>>>      printf("String:%s\n",mystring);
>>>>>
>>>>>      return 0;
>>>>> }
>>>>>
>>>>> It compiles okay (no warnings),  but when runs it has undefined results
>>>>> because I've not put anything into mystring before attempting to print
>>>>> it, so it gets whatever garbage is left in memory.
>>>>>
>>>>> Are there any "pre-compiler" code analysis tools out there that would
>>>>> pick up this sort of problem?  The platform is sparc Solaris 10.
>>>>
>>>> Unit tests (written first).
>>>
>>> I don't think unit tests should be the first line of defence here.  It's
>>> too common to find a forgiving environment where, say, an uninitialised
>>> string happens to be a null string, or where a buffer overrun is silent.
>>
>> My point was if the code was written to pass a test, the bug probably
>> wouldn't have been introduced in the first place.
>
> Yes, I got that that was your point!  Did you get that I was disagreeing
> with it? :-)

I do and I agree tests aren't a panacea.  I reorder my tests fairly 
often to help with detecting code that accidentally relies on 
uninitialised memory.  It is easy to test for reading uninitialised 
dynamic memory, but much harder with automatic variables.

> We may have to leave it there since we are both making statistical
> arguments: you say that units test will "probably" prevent the bug
> appearing, and I am saying that cases where it might not are all "too
> common".  I have no data to back up my statistics except anecdotes to
> the effect that it's happened more than once.
>
>> As the answers here show, this kind of problem is a difficult one for
>> compilers and lint to pick up.  None that I know will spot this.
>
> Unit tests + valgrind would be my proposal:
>
>    $ gcc -g -o printf -std=c99 -pedantic -Wall -Wextra printf.c
>    $ valgrind ./printf
>    ==16796== Memcheck, a memory error detector
>    ==16796== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
>    ==16796== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
>    ==16796== Command: ./printf
>    ==16796==
>    ==16796== Conditional jump or move depends on uninitialised value(s)
>    ==16796==    at 0x4E7635E: vfprintf (vfprintf.c:1614)
>    ==16796==    by 0x4E7D539: printf (printf.c:35)
>    ==16796==    by 0x400544: main (printf.c:6)
>    ==16796==
>    ... etc...

I certainly agree with that, I do much the same by running tests under 
dbx with memory checking enabled.

But which is the first line of defence, the tests or valgrind?

:)

-- 
Ian Collins

Back to comp.unix.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

C Code "sanity checking" tools Martin Crouch <spamspamspam@spaml.com> - 2011-08-08 12:00 +0100
  Re: C Code "sanity checking" tools Richard Kettlewell <rjk@greenend.org.uk> - 2011-08-08 12:09 +0100
    Re: C Code "sanity checking" tools Fritz Wuehler <fritz@spamexpire-201108.rodent.frell.theremailer.net> - 2011-08-09 08:50 +0200
      Re: C Code "sanity checking" tools Måns Rullgård <mans@mansr.com> - 2011-08-09 10:55 +0100
        Re: C Code "sanity checking" tools Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-08-09 14:15 +0100
        Re: C Code "sanity checking" tools Nomen Nescio <nobody@dizum.com> - 2011-08-10 01:58 +0200
        Re: C Code "sanity checking" tools Nomen Nescio <nobody@dizum.com> - 2011-08-10 12:29 +0200
      Re: C Code "sanity checking" tools Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-09 14:17 +0100
        Re: C Code "sanity checking" tools Keith Thompson <kst-u@mib.org> - 2011-08-09 09:17 -0700
          Re: C Code "sanity checking" tools Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-09 17:36 +0100
            Re: C Code "sanity checking" tools Keith Thompson <kst-u@mib.org> - 2011-08-09 12:19 -0700
              Re: C Code "sanity checking" tools Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-09 20:36 +0100
                Re: C Code "sanity checking" tools Michael Press <rubrum@pacbell.net> - 2011-08-09 19:24 -0700
                Re: C Code "sanity checking" tools Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-10 10:27 +0100
                Re: C Code "sanity checking" tools Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-10 14:01 +0100
                Re: C Code "sanity checking" tools "io_x" <a@b.c.invalid> - 2011-08-13 09:42 +0200
                Re: C Code "sanity checking" tools Keith Thompson <kst-u@mib.org> - 2011-08-13 16:04 -0700
                Re: C Code "sanity checking" tools Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-08-14 19:46 +0100
  Re: C Code "sanity checking" tools Ian Collins <ian-news@hotmail.com> - 2011-08-08 23:12 +1200
    Re: C Code "sanity checking" tools Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-08-08 15:35 +0100
      Re: C Code "sanity checking" tools Martin Crouch <spamspamspam@spaml.com> - 2011-08-08 16:05 +0100
        Re: C Code "sanity checking" tools Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-08-08 22:57 +0100
          Re: C Code "sanity checking" tools Måns Rullgård <mans@mansr.com> - 2011-08-08 23:32 +0100
      Re: C Code "sanity checking" tools Ian Collins <ian-news@hotmail.com> - 2011-08-09 08:03 +1200
        Re: C Code "sanity checking" tools Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-08-08 22:51 +0100
          Re: C Code "sanity checking" tools Ian Collins <ian-news@hotmail.com> - 2011-08-09 10:01 +1200
    Re: C Code "sanity checking" tools jt@toerring.de (Jens Thoms Toerring) - 2011-08-08 22:06 +0000
      Re: C Code "sanity checking" tools Ian Collins <ian-news@hotmail.com> - 2011-08-09 10:18 +1200
        Re: C Code "sanity checking" tools jt@toerring.de (Jens Thoms Toerring) - 2011-08-08 23:25 +0000
  Re: C Code "sanity checking" tools Nobody <nobody@nowhere.com> - 2011-08-08 14:01 +0100
    Re: C Code "sanity checking" tools Måns Rullgård <mans@mansr.com> - 2011-08-08 14:47 +0100
      Re: C Code "sanity checking" tools Martin Crouch <spamspamspam@spaml.com> - 2011-08-08 16:08 +0100
        Re: C Code "sanity checking" tools Marc <marc.glisse@gmail.com> - 2011-08-08 15:39 +0000
      Re: C Code "sanity checking" tools William Ahern <william@wilbur.25thandClement.com> - 2011-08-08 10:20 -0700
      Re: C Code "sanity checking" tools Nobody <nobody@nowhere.com> - 2011-08-09 00:59 +0100
  Re: C Code "sanity checking" tools mathog <dmathog@gmail.com> - 2011-08-08 08:48 -0700

csiph-web