📜 ⬆️ ⬇️

Errors in robots: expectation and reality

Robo errors

I think the robots riot is not close. I started writing an article about the fact that our team has begun to adapt the PVS-Studio code analyzer for the needs of embedded developers. For starters, we supported KEIL and IAR. Looking at errors in some projects for embedded devices, I am full of feelings that I want to share. The easiest way to do this is to show a couple of pictures and a couple of examples of errors.

So, what do we associate bugs with robots? The imagination of the man in the street draws a picture like this:

Expectation

However, when I see such errors in the code of the RT-Thread operating system, I think that everything is different.
Some of the most simple and funny errors that I found using the PVS-Studio code analyzer in the RT-Thread project.

RT-Thread is an open source IoT operating system from China, which has strong scalability: for example ARM Cortex-M0, or Cortex-M3 / 4/7, for a rich feature system running on MIPS32, ARM Cortex-A8, ARM Cortex-A9 DualCore etc. github.com/RT-Thread/rt-thread
')
PVS-Studio warnings:

V560 CWE-571 A part of the conditional expression is always true: 0xFFFF0000. peci.c 372
V560 CWE-571 A part of the conditional expression is always true: 0x0000FFFF. peci.c 373

#define PECI_M0D0C_HITHR_M 0xFFFF0000 // High Threshold #define PECI_M0D0C_LOTHR_M 0x0000FFFF // Low Threshold void PECIDomainConfigGet(....) { unsigned long ulTemp; .... ulTemp = HWREG(ulBase + PECI_O_M0D0C + (ulDomain * 4)); *pulHigh = ((ulTemp && PECI_M0D0C_HITHR_M) >> PECI_M0D0C_HITHR_S); *pulLow = ((ulTemp && PECI_M0D0C_LOTHR_M) >> PECI_M0D0C_LOTHR_S); } 

Instead of && you should write &.

PVS-Studio warning:
V767 Suspicious access to the element of the 'w' array by a constant index inside a loop. fsl_dcp.c 946

 typedef union _dcp_hash_block { uint32_t w[DCP_HASH_BLOCK_SIZE / 4]; uint8_t b[DCP_HASH_BLOCK_SIZE]; } dcp_hash_block_t; typedef struct _dcp_hash_ctx_internal { dcp_hash_block_t blk; .... } dcp_hash_ctx_internal_t; status_t DCP_HASH_Init(DCP_Type *base, dcp_handle_t *handle, dcp_hash_ctx_t *ctx, dcp_hash_algo_t algo) { .... dcp_hash_ctx_internal_t *ctxInternal; .... for (i = 0; i < sizeof(ctxInternal->blk.w) / sizeof(ctxInternal->blk.w[0]); i++) { ctxInternal->blk.w[0] = 0u; } .... } 

Write 0 to the same cell in the array. Most of the array will remain uninitialized. It should be written:

ctxInternal-> blk.w [i] = 0u;

PVS-Studio warnings:

V602 CWE-480 Consider inspecting the '(1U <1)' expression. '<' possibly should not be replaced with '<<'. fsl_aipstz.h 69
V602 CWE-480 Consider inspecting the '(1U <2)' expression. '<' possibly should not be replaced with '<<'. fsl_aipstz.h 70
V602 CWE-480 Consider inspecting the '(1U <2)' expression. '<' possibly should not be replaced with '<<'. fsl_aipstz.h 71

 typedef enum _aipstz_peripheral_access_control { kAIPSTZ_PeripheralAllowUntrustedMaster = 1U, kAIPSTZ_PeripheralWriteProtected = (1U < 1), kAIPSTZ_PeripheralRequireSupervisor = (1U < 2), kAIPSTZ_PeripheralAllowBufferedWrite = (1U < 2) } aipstz_peripheral_access_control_t; 

Instead of <should use <<. Then the constants will be equal to different powers of 2.

When Terminator arrives at the police station to kill Sarah Connor, it will be like this:

Reality

So while you can sleep well.

...

Wait, I'm going to write an article where I’ll tell you that PVS-Studio will find such errors ... Oops!

Source: https://habr.com/ru/post/349590/


All Articles