[cfe-dev] CLang 2.1 doesn't catch assert within if

Ted Kremenek kremenek at apple.com
Fri Oct 7 22:11:45 PDT 2011


The analyzer doesn't find this, but it would be an interesting check to write.  Conditional code for branches are just asking for trouble.

On Oct 7, 2011, at 10:02 PM, Don Quixote de la Mancha wrote:

> I've been looking around for an analysis I could add to clang, but I
> used Xcode 4.1's clang to test this, which only has CLang 2.1.  Could
> someone please check whether the current SVN catches this?  I would
> check myself but I am having some trouble getting the current SVN to
> build.  It's not the source code but some arcane problems with the
> configure shell scripts.  If I can't figure out my configure problem
> soon I will ask about it here.
> 
> If the current SVN does not catch this, I'd like to do the work to add
> the analysis myself.  I've been following the list for a couple weeks
> and understand now what would be expected of my code for it to be
> accepted.  I will open a Bugzilla bug to track my progress.
> 
> You NEVER EVER want to do the following:
> 
> if ( condition )
>  assert( must_be_true );
> 
> following_line();
> 
> If NDEBUG is defined as for a release or profile build, you'll get the
> following in your preprocessed code, which will be very hard to debug:
> 
> if ( condition )
>   following_line();
> 
> If you really must insist on putting an assert as the only line within
> an if statement, you MUST place it in a Basic Block:
> 
> if ( condition ){
>  assert( must_be_true );
> }
> 
> Even better is to make the if statement part of the assert:
> 
> assert( condition && must_be_true );
> 
> Back in the day I wrote a new library to replace my client's old one.
> To test that my new library got the same results as his old one, he
> asked me to link his executable with both his old and my new library,
> and to compare the results of all the calls to each library.  I don't
> recall why anymore but I actually made the mistake, then spent all day
> long tracking down my bug, as the executables and both libraries were
> very complex.
> 
> I'm running Mac OS X Snow Leopard 10.6.8 on a MacBook Pro, Model
> Identifier MacBookPro1,1 (32-bit Core Duo, NOT 64-bit Core 2 Duo).
> 
> $ clang --version
> Apple clang version 2.1 (tags/Apple/clang-163.7.1) (based on LLVM 3.0svn)
> Target: i386-apple-darwin10.8.0
> Thread model: posix
> 
> $ gcc --version
> i686-apple-darwin10-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc.
> build 5658) (LLVM build 2335.15.00)
> 
> $ gcc --pedantic -Wall -c assert_within_if.c
> 
> This compile succeeds without any warnings.
> 
> $ clang --analyze assert_within_if.c
> 
> CLang doesn't print anything to the terminal.  assert_within_if.plist
> doesn't contain any diagnostics.
> 
> ====== Cut Here ======
> /* assert_within_if.c */
> #include <stdlib.h>
> #include <assert.h>
> 
> int assert_within_if( int flag, int  *pointer );
> 
> int assert_within_if( int flag, int  *pointer )
> {
> 	int result = 0;
> 
> 	if ( flag )
> 		assert( NULL != pointer );
> 
> 	result = *pointer;
> 
> 	return result;
> }
> 
> ====== Cut Here =====
> 
> Ever Faithful,
> 
> Don Quixote
> -- 
> Don Quixote de la Mancha
> Dulcinea Technologies Corporation
> Software of Elegance and Beauty
> http://www.dulcineatech.com
> quixote at dulcineatech.com
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list