Static code analysis

Programming, for all ages and all languages.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Static code analysis

Post by Love4Boobies »

Synon wrote:
Love4Boobies wrote:
Synon wrote:I compile with -Wall -Wextra -pedantic -ansi -std=C99 when I'm adding a feature, but I add -Werror and change -pedantic to -pedantic-errors when I get it working.
-ansi is C89, -std=C99 is C99. It's either one or the other.
I put both, I thought -ansi meant it would only use standard features. I'll take the -ansi out.
No, that's already covered by -std=c99. If you want to use the GNU dialect, you have to use -std=gnu99 instead.
Synon wrote:@Combuster,
Lol, surely -Wall and -Wextra account for all of those? I mean, -Wall is "enable all warnings" so I would've thought it would, you know, enable all warnings...
They account for some of them. See the simplified list I provided in my last reply.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Static code analysis

Post by Owen »

Love4Boobies wrote:
Synon wrote:
Love4Boobies wrote:-ansi is C89, -std=C99 is C99. It's either one or the other.
I put both, I thought -ansi meant it would only use standard features. I'll take the -ansi out.
No, that's already covered by -std=c99. If you want to use the GNU dialect, you have to use -std=gnu99 instead.
Note that GCC-specific features remain available - they just only get made available in reserved namespaces. IMO, defaulting to a non-strict mode is GCC's biggest bug.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Static code analysis

Post by Solar »

Synon wrote:Lol, surely -Wall and -Wextra account for all of those? I mean, -Wall is "enable all warnings" so I would've thought it would, you know, enable all warnings...
Do. Not. Assume.

The GCC docs say about -Wall, emphasis mine:
Note that some warning flags are not implied by -Wall. Some of them warn about constructions that users generally do not consider questionable, but which occasionally you might wish to check for; others warn about constructions that are necessary or hard to avoid in some cases, and there is no simple way to modify the code to suppress the warning. Some of them are enabled by -Wextra but many of them must be enabled individually.
Every good solution is obvious once you've found it.
Andrey2012
Posts: 2
Joined: Thu Mar 22, 2012 6:52 am

Re: Static code analysis

Post by Andrey2012 »

Hello. I would invite all who are interested in static code analysis, try our tool PVS-Studio.
PVS-Studio is a static analyzer that detects errors in source code of C/C++/C++11 applications (Visual Studio 2005/2008/2010).
Examples of use PVS-Studio: 100 bugs in Open Source C/C++ projects.
Fully functional trial: download.
User avatar
Yoda
Member
Member
Posts: 255
Joined: Tue Mar 09, 2010 8:57 am
Location: Moscow, Russia

Re: Static code analysis

Post by Yoda »

Andrey2012 wrote:Examples of use PVS-Studio: 100 bugs in Open Source C/C++ projects.
Wow! Looks great!
Yet Other Developer of Architecture.
OS Boot Tools.
Russian national OSDev forum.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Static code analysis

Post by Combuster »

It stinks. It requires a paid version of M$VS.

(Would've expected something like that from a bot)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Andrey2012
Posts: 2
Joined: Thu Mar 22, 2012 6:52 am

Re: Static code analysis

Post by Andrey2012 »

Combuster wrote:It stinks. It requires a paid version of M$VS.
Why doesn't PVS-Studio support Visual Studio Express Edition?

This is not the restriction of PVS-Studio. None of the Visual Studio Express Edition versions supports plugins - this is the restriction of this very Visual Studio edition; we cannot integrate our tool into Visual Studio Express Edition.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Static code analysis

Post by Combuster »

That's your design choice to write an implementation with that requirement, not Microsoft's.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Static code analysis

Post by Brynet-Inc »

The amount of people on this forum using Visual Studio for OSDev is negligible, the are other free static analysis tools. Please peddle your product elsewhere.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
brain
Member
Member
Posts: 234
Joined: Thu Nov 05, 2009 5:04 pm
Location: UK
Contact:

Re: Static code analysis

Post by brain »

Hmm, the last tool I used similar to this was RATS... showing how long ago that was eh...
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: Static code analysis

Post by Rudster816 »

Combuster wrote:That's your design choice to write an implementation with that requirement, not Microsoft's.
According to the product page, you can launch it via the command line as well. Either way, its a commercial product, I don't think a software company\team is going to be using an express edition of Visual Studio. I will say that posting the trail here is pretty useless because anyone looking into OSDev is almost certainly doing it on his own time, and isn't going to be able to afford or want a commercial product like that.


On a related note, I suddenly find that my code could have benefited from a product like that.

I was using this bit of code...

Code: Select all

...
#define CHUNK_POOL_INDEX_PUSH(x) chunk_pool_stack--; *chunk_pool_stack = x
unsigned short* chunk_pool_stack;
...
	int i;
...
	for (i = 65535; i >= 0; i--)
		CHUNK_POOL_INDEX_PUSH(i);
Which to my surprise, would decrement the stack pointer all the way down, but only set the last element, not to 0 though, but to 65535. I'll let you try and figure it out without spoiling the fun.

Answer:

Only the first statement in the macro was run through the loop, since without braces only the statement immediately following the for loop will be executed. So it expanded to this

for (i = 65535; i >= 0; i--)
chunk_pool_stack--;
*chunk_pool_stack = i;

since i is now -1, the last element is set to 65535, not 0 as you would expect.

Correct code:

for (i = 65535; i >= 0; i--)
{
CHUNK_POOL_INDEX_PUSH(i);
}

Even better would be not to use multiline macros that look like single statements. Luckily it only took me a minute or two to catch this after testing it.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Static code analysis

Post by bluemoon »

You may also enclose it within the macro:

Code: Select all

#define CHUNK_POOL_INDEX_PUSH(x) { chunk_pool_stack--; *chunk_pool_stack = x }
Synon
Member
Member
Posts: 169
Joined: Sun Sep 06, 2009 3:54 am
Location: Brighton, United Kingdom

Re: Static code analysis

Post by Synon »

@Rudster816
I would wrap the macro in

Code: Select all

do {
        ...
} while (0)
so you can't make that mistake again. I do that with all macros that are more than one statement (I got the idea from Linux).
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Static code analysis

Post by qw »

Another but non-standard (GCC specific) solution is to wrap the curly brackets between parentheses:

Code: Select all

#define FOO ({ ... })
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Static code analysis

Post by Solar »

Hobbes wrote:Another but non-standard (GCC specific) solution...
I wonder, why bring up a "non-standard, compiler-specific" solution at all when the canon, general solution has already been presented?
Every good solution is obvious once you've found it.
Post Reply