Static code analyzer for Torque bitstreams
by Konrad Kiss · 04/17/2012 (10:13 am) · 6 comments
Ever missed a readFlag() in your network stream to later see a bunch of "disagree" messages? Spent days if not weeks until you found the culprit? This resource is a php script that will help you by finding such possible issues.
Like all static code analyzers, this is not 100% exact, but to my defense, it will almost surely find it if bit counts mismatch in ie. a packUpdate and an unpackUpdate. The downside is that it reports a few false positives. I had to leave these in to not decrease the chance of it finding a real problem.
The script goes through all .cpp files in your source directory and checks whether they have pack/unpack, packData/unpackData, packUpdate/unpackUpdate and readPacketData/writePacketData methods, and if they do, it will compare the stream i/o commands used. It tries to find the number of bits used in each operation where possible, or assign some realistic value.
The downside if that it does not currently compare the order of these commands, but being able to find an issue where you write one bit more than you read is a pretty good thing in itself if I may say so.
False positives are usually cases, where the program structures to write to and read bits from the bitstream are different. (Ie. using a writeFlag(true) and a writeFlag(false) in two branches of a condition which is read by a readFlag()) There are currently about 6 such reports in vanilla Torque 3D 1.2 and 6+1 in AFX for Torque 3D 1.2. So if you get mismatches over these values, be very suspicious! :)
If you intend to use this tool to check on your code from time to time, you might want to rewrite the parts where false positives are reported, so that they don't mislead you the next time.
Alright, let me add some shameless self-promotion here - I've already posted the code over in my blog at konradkiss.com.
Khm.. where I don't have to break it up into pieces anyway.. :)
Any comments either here or over at my blog are very much appreciated. Hope this helps you catch some insects! :)
@konradkiss
KonradKiss @ GitHub
konradkiss.com
Like all static code analyzers, this is not 100% exact, but to my defense, it will almost surely find it if bit counts mismatch in ie. a packUpdate and an unpackUpdate. The downside is that it reports a few false positives. I had to leave these in to not decrease the chance of it finding a real problem.
The script goes through all .cpp files in your source directory and checks whether they have pack/unpack, packData/unpackData, packUpdate/unpackUpdate and readPacketData/writePacketData methods, and if they do, it will compare the stream i/o commands used. It tries to find the number of bits used in each operation where possible, or assign some realistic value.
The downside if that it does not currently compare the order of these commands, but being able to find an issue where you write one bit more than you read is a pretty good thing in itself if I may say so.
False positives are usually cases, where the program structures to write to and read bits from the bitstream are different. (Ie. using a writeFlag(true) and a writeFlag(false) in two branches of a condition which is read by a readFlag()) There are currently about 6 such reports in vanilla Torque 3D 1.2 and 6+1 in AFX for Torque 3D 1.2. So if you get mismatches over these values, be very suspicious! :)
If you intend to use this tool to check on your code from time to time, you might want to rewrite the parts where false positives are reported, so that they don't mislead you the next time.
Alright, let me add some shameless self-promotion here - I've already posted the code over in my blog at konradkiss.com.
Khm.. where I don't have to break it up into pieces anyway.. :)
Any comments either here or over at my blog are very much appreciated. Hope this helps you catch some insects! :)
@konradkiss
KonradKiss @ GitHub
konradkiss.com
About the author
http://about.me/konrad.kiss
#2
This is going in my toolbox. Thumbs up and a pint (or three) in your honor :)
04/17/2012 (11:38 am)
You have no idea how this very thing was troubling me a couple weeks ago! This is going in my toolbox. Thumbs up and a pint (or three) in your honor :)
#3
@Michael: Awesome! :) Let me know if you encounter any issues. I've tested it for a rather long day, but still..
04/17/2012 (12:08 pm)
@Kevin: The general tools I use are CPPCheck and the trial version of PVS-Studio. PVS-Studio is a very solid tool, but it's way out of my budget at EUR 3500. CPPCheck is surprisingly good for being open-source, but it's also a bit harder to manage.@Michael: Awesome! :) Let me know if you encounter any issues. I've tested it for a rather long day, but still..
#4
04/18/2012 (12:59 am)
Great idea. Will definitely give this a go next time I work on classes that aren't server-side only! ;P
#5
04/19/2012 (5:36 am)
Very interesting!, i should have need of this script very soon!
#6
Thanks
04/19/2012 (7:43 pm)
This is cool. I remember a while back trying to figure out how to do networking for a new object. It was frustrating to see an object look okay when first person, but be out of sync in 3rd person. Anything to help with that stuff getting out of sync is awesome.Thanks

Torque Owner Kevin Mitchell
12 CatBlack Studios