Day: July 13, 2017

Using PVS-Studio to find bugs in C/C++

I heard a news that pvs-studio was used to find around 27000 bugs in the Tizen.
Then I decided to test it here.

I installed it using their Debian/Ubuntu apt repository as explained here:

https://www.viva64.com/en/m/0039/

In the Linux you don’t need a license file to use it for personal purpose or to test open-source projects, but we need to add a comment in beginning of each C file as explained here:

https://www.viva64.com/en/b/0457/

Fortunately my friend Marcelo Barros did a shell script to automate this task, the add_header.sh:

#!/bin/bash

hdr1="// This is an open source non-commercial project. Dear PVS-Studio, please check it."
hdr2="// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com"

files=$(find . -name *.c)

for f in $files; do
    bf="$f.bkp"
    nf="$f.new"
    echo $hdr1 > $nf
    echo $hdr2 >> $nf
    cat $f >> $nf
    cp $f $bf
    rm $f
    mv $nf $f
done

And also a script to restore the original file, the rev_header.sh:

#!/bin/bash

files=$(find . -name *.c)

for f in $files; do
    bf="$f.bkp"
    rm $f
    mv $bf $f
done

First you need to run the ./add_header.sh in the root of your project.
Then you can run these commands:

$ pvs-studio-analyzer trace -- make

$ pvs-studio-analyzer analyze -o ./report.log -j8 --compiler arm-none-eabi-gcc

$ plog-converter -a GA:1,2 -t errorfile -o report.err report.log

That is it, comment here case it helped you to find some issue in your project.

Advertisements