Here are a few simplest and most interesting bugs found by the PVS-Studio analyzer in the RT-Thread project. PVS-Studio is a tool for detecting errors and potential vulnerabilities in the source code of applications written in С, C++, and C#. The analyzer supports Windows and Linux (in addition, a macOS version will be released soon). https://www.viva64.com/en/pvs-studio/ RT-Thread is an open source IoT operating system from China, which has strong scalability: from a tiny kernel running on a tiny core, for example ARM Cortex-M0, or Cortex-M3/4/7, to a rich feature system running on MIPS32, ARM Cortex-A8, ARM Cortex-A9 DualCore etc. https://github.com/RT-Thread/rt-thread ---------------------------------------------------------------- PVS-Studio diagnostic messages: V560 CWE-571 A part of conditional expression is always true: 0xFFFF0000. peci.c 372 V560 CWE-571 A part of 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); } The & operator should be used instead of &&. ---------------------------------------------------------------- PVS-Studio diagnostic message: V767 Suspicious access to element of '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; } .... } The value 0 is written into one and the same array element. The major part of the array will remain uninitialized. The correct version looks like this: ctxInternal->blk.w[i] = 0u; ---------------------------------------------------------------- PVS-Studio diagnostic messages: V602 CWE-480 Consider inspecting the '(1U < 1)' expression. '<' possibly should be replaced with '<<'. fsl_aipstz.h 69 V602 CWE-480 Consider inspecting the '(1U < 2)' expression. '<' possibly should be replaced with '<<'. fsl_aipstz.h 70 V602 CWE-480 Consider inspecting the '(1U < 2)' expression. '<' possibly should 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; The << operator should be used instead of <. The constants would then equal to different powers of 2. ----------------------------------------------------------------