5 March

PVS-Studio Integration in PlatformIO

PVS-Studio corporate blogC++CProgramming microcontrollersDeveloping for Arduino
Picture 5

Recently, the PlatformIO development environment of embedded systems has supported PVS-Studio. In this article, you'll find out how to check your code with the static analyzer on the example of an open project.

What is PlatformIO?


PlatformIO is a cross-platform tool for microcontroller programming. The core of PlatformIO is a command-line tool, however it is recommended to use it as a plugin for Visual Studio Code. It supports a large number of modern microchips, and boards based on them. It can automatically download suitable build systems. The site has a large collection of libraries for managing plug-in electronic components. There is support for several static code analyzers, including PVS-Studio.

Project import


To demonstrate, let's take the controlling program for ArduPod — a hexapod on the Arduino Mega board.


Let's create a new project for the appropriate board and copy the source code:

Picture 2

In the /arduino/AP_Utils/examples/ directory, there are several examples of programs for configuring and running the hexapod, we'll use servo_test.ino. Basically, the program for Arduino is created as sketches in the INO format, which in this case is not quite suitable. In order to make the correct .cpp file from it, it is usually enough to change the file extension, add the #include <Arduino.h> header at the beginning, and make sure that functions and global variables are declared before accessing them.

Picture 3

During the build process, some errors might occur indicating the lack of required third-party libraries. However, PlatformIO will help you find them in its repository.

In file included from src\servo_test.cpp:20:0:
src/AP_Utils.h:10:37: fatal error: Adafruit_PWMServoDriver.h:
No such file or directory
*******************************************************************************
* Looking for Adafruit_PWMServoDriver.h dependency? Check our library registry!
*
* CLI> platformio lib search "header:Adafruit_PWMServoDriver.h"
* Web> https://platformio.org/lib/search?query=header:Adafruit_PWMServoDriver.h
*
*******************************************************************************
compilation terminated.

Suitable options are available by the link. The needed dependency is installed by the following command in the terminal:

pio lib install "Adafruit PWM Servo Driver Library"

Configuring analyzers and starting a check


To configure analyzers, you need to edit the platformio.ini configuration file like this:

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
check_tool = pvs-studio
check_flags =
  pvs-studio:
    --analysis-mode=4 ; General analysis mode. Set to 32 for MISRA
    --exclude-path=/.pio/libdeps ; Ignore dependency libraries

The check_tool parameter indicates which code analyzers to apply. They can be configured in the check_flags parameter. More detailed instructions are available in the documentation on the official website: https://docs.platformio.org/en/latest/plus/check-tools/pvs-studio.html

Finally, you can run a project check with a command in the terminal. Before the first check, the environment itself will download the current analyzer distribution.

pio check

The result of checking the hexapod program


This time, the purpose of this article is to demonstrate the PVS-Studio integration with PlatformIO, not the analyzer's diagnostic abilities. Nevertheless, once the project is checked, let's take a quick look at a couple of errors to make sure the project's check was successful.

V519 There are identical sub-expressions to the left and to the right of the '-' operator: pow(t, 2) — pow(t, 2). AP_Utils.cpp 176

pointLeg* AP_Utils::traceLeg(uint8_t leg, float phi, float z,
  int resolution, uint8_t shape) {
  ....
  if(shape == ELLIPTIC) {
    ....
    float v = sqrt(pow(phi - legs[leg].phi, 2) + pow(z - legs[leg].z, 2));
    float u = sqrt(pow(phi - phi0, 2) + pow(z - z0, 2));
    float t = sqrt(pow(phi0 - legs[leg].phi, 2) + pow(z0 - legs[leg].z, 2));
    theta = acos((pow(t, 2) - pow(t, 2) - pow(v, 2))/(-2.0*t*u));
    ....
  }
  ....
}

Two identical expressions are subtracted from one another. It is not clear what the mathematical sense of this difference is. Perhaps, the programmer simply didn't simplify the expression. Or maybe there is a typo — another argument has to be there instead of one of these ts.

V550 An odd precise comparison: value != — 1. It's probably better to use a comparison with defined precision: fabs(A — B) > Epsilon. AP_Utils.cpp 574

float AP_Utils::sr04_average(uint8_t trig, uint8_t echo,
  int unit, int samples, int time) {
  ....
  float average, pause, value;
  ....
  for(int i=0; i<samples; i++) {
    value = sr04(trig, echo, unit);
    if(value != -1) { // <=
      total += value;
      delay(pause);
    } else {
      i--;
    }
  }
  average = total/samples;
  ....
  return average;
}


The warning indicates the sloppy comparison of floating point numbers. As it's impossible to accurately represent real numbers with a finite number of bits, it is safer to establish the equality of fractional numbers by comparing their difference with an epsilon. For example, as follows:

bool is_equal(double x, double y) {
  return std::fabs(x - y) < 0.001f;
}

The only safe option to directly compare non-integer numbers is to assign constants to the variables, and then compare their values with these constants. In this case, value variable is not assigned -1 anywhere. This is how the AP_Utils::sr04 callee method operates. It returns the checked value.

float AP_Utils::sr04(uint8_t trig, uint8_t echo, int unit) {
  ....
  float duration, distance;
  ....
  duration = pulseIn(echo, HIGH);
  distance = (346.3*duration*0.000001*unit)/2; // <=
  
  if((distance >= 0.02*unit) && (distance <= 4*unit)) {
    ....
    return(distance);
  } else {
    ....
    return 0;
  }
}

As you can see, the result of some calculations will be written in value. Assignment of -1 is nowhere to be seen, whereas AP_Utils::sr04 can return 0. This implies that the wrong result is compared.

Conclusion


In this article, we've touched upon the process of checking projects on microcontrollers by the static code analyzer in the PlatformIO environment for programming embedded systems. Let me quickly remind you that everyone interested in trying PVS-Studio can avail of the trial mode and for open source projects it is possible to get a free license.

For those who want to learn more about PVS-Studio features in more detail, I recommend checking out the following articles:

Tags:c++pvs-studioembeddedmicrocontrollerPlatformIOc
Hubs: PVS-Studio corporate blog C++ C Programming microcontrollers Developing for Arduino
+5
1.1k 0
Leave a comment