No, I can’t see an issue it would stop on.
And it doesn’t even find the internal divide-by-zero (cross function analysis):
printf("i divided is %u\n", i / ReturnZero());
And I would like it to find that the external function also returns zero leading to a divide-by-zero (cross file analysis):
printf("i divived is %u\n", i / ExtReturnZero());
My file:
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "extfunc.h"
#define BUFFERSIZE 300
uint8_t buffer[BUFFERSIZE] = { 0 };
uint16_t wrIndex = 0;
void f()
{
char *p;
*p = 0;
}
uint8_t ReturnZero(void)
{
return 0;
}
void PutInBuffer(uint8_t byte)
{
buffer[wrIndex++] = byte;
}
int intValue = 42;
void readstruct(void *dst)
{
memcpy(dst, &intValue, sizeof(intValue));
}
void oobwrite(char *c)
{
readstruct(c);
}
void main(void)
{
for (uint16_t i = 0; i < 500; i++)
{
PutInBuffer(i);
if (i == 300)
{
printf("i divived is %u\n", i / ExtReturnZero());
}
if (i == 400)
{
printf("i divided is %u\n", i / ReturnZero());
}
}
char charValue = 0;
oobwrite(&charValue);
}
SonarLint output:
[{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S988",
"severity": 4,
"message": "Remove this include of \"stdio.h\".",
"source": "sonarlint",
"startLineNumber": 2,
"startColumn": 1,
"endLineNumber": 2,
"endColumn": 19
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:M23_388",
"severity": 4,
"message": "Make this global constant with static initialization or remove it by passing the value as a function argument.",
"source": "sonarlint",
"startLineNumber": 8,
"startColumn": 1,
"endLineNumber": 8,
"endColumn": 38
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:M23_388",
"severity": 4,
"message": "Make this global constant with static initialization or remove it by passing the value as a function argument.",
"source": "sonarlint",
"startLineNumber": 9,
"startColumn": 1,
"endLineNumber": 9,
"endColumn": 34
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S929",
"severity": 4,
"message": "Set this function's parameter list to \"void\".",
"source": "sonarlint",
"startLineNumber": 11,
"startColumn": 6,
"endLineNumber": 11,
"endColumn": 7
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:M23_321",
"severity": 4,
"message": "Local variables should be initialized immediately.",
"source": "sonarlint",
"startLineNumber": 13,
"startColumn": 11,
"endLineNumber": 13,
"endColumn": 12
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S836",
"severity": 4,
"message": "Dereference of undefined pointer value (loaded from variable 'p') [+2 locations]",
"source": "sonarlint",
"startLineNumber": 14,
"startColumn": 8,
"endLineNumber": 14,
"endColumn": 9
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:M23_152",
"severity": 4,
"message": "Dereference of undefined pointer value (loaded from variable 'p') [+2 locations]",
"source": "sonarlint",
"startLineNumber": 14,
"startColumn": 8,
"endLineNumber": 14,
"endColumn": 9
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S100",
"severity": 4,
"message": "Rename this function to match the regular expression: ^[a-z][a-zA-Z0-9]*$",
"source": "sonarlint",
"startLineNumber": 18,
"startColumn": 9,
"endLineNumber": 18,
"endColumn": 19
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S100",
"severity": 4,
"message": "Rename this function to match the regular expression: ^[a-z][a-zA-Z0-9]*$",
"source": "sonarlint",
"startLineNumber": 24,
"startColumn": 6,
"endLineNumber": 24,
"endColumn": 17
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:M23_058",
"severity": 4,
"message": "Replace this builtin type with an alias that makes the type size explicit",
"source": "sonarlint",
"startLineNumber": 29,
"startColumn": 1,
"endLineNumber": 29,
"endColumn": 4
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S813",
"severity": 4,
"message": "Replace this use of \"int\" with a typedef.",
"source": "sonarlint",
"startLineNumber": 29,
"startColumn": 1,
"endLineNumber": 29,
"endColumn": 4
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:M23_388",
"severity": 4,
"message": "Make this global constant with static initialization or remove it by passing the value as a function argument.",
"source": "sonarlint",
"startLineNumber": 29,
"startColumn": 1,
"endLineNumber": 29,
"endColumn": 18
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:M23_007",
"severity": 4,
"message": "Use the value returned from \"memcpy\".",
"source": "sonarlint",
"startLineNumber": 33,
"startColumn": 3,
"endLineNumber": 33,
"endColumn": 9
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S109",
"severity": 4,
"message": "Assign this magic number 500 to a well-named variable, and use the variable instead.",
"source": "sonarlint",
"startLineNumber": 44,
"startColumn": 28,
"endLineNumber": 44,
"endColumn": 31
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S1705",
"severity": 4,
"message": "Use prefix increment.",
"source": "sonarlint",
"startLineNumber": 44,
"startColumn": 34,
"endLineNumber": 44,
"endColumn": 36
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S5276",
"severity": 4,
"message": "implicit conversion loses integer precision: 'uint16_t' (aka 'unsigned short') to 'uint8_t' (aka 'unsigned char')",
"source": "sonarlint",
"startLineNumber": 46,
"startColumn": 17,
"endLineNumber": 46,
"endColumn": 18
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S1772",
"severity": 4,
"message": "Flip this test to put the right operand on the left-hand side of \"==\".",
"source": "sonarlint",
"startLineNumber": 47,
"startColumn": 9,
"endLineNumber": 47,
"endColumn": 17
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S109",
"severity": 4,
"message": "Assign this magic number 300 to a well-named variable, and use the variable instead.",
"source": "sonarlint",
"startLineNumber": 47,
"startColumn": 14,
"endLineNumber": 47,
"endColumn": 17
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:M23_007",
"severity": 4,
"message": "Use the value returned from \"printf\".",
"source": "sonarlint",
"startLineNumber": 49,
"startColumn": 7,
"endLineNumber": 49,
"endColumn": 13
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S1772",
"severity": 4,
"message": "Flip this test to put the right operand on the left-hand side of \"==\".",
"source": "sonarlint",
"startLineNumber": 51,
"startColumn": 9,
"endLineNumber": 51,
"endColumn": 17
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:S109",
"severity": 4,
"message": "Assign this magic number 400 to a well-named variable, and use the variable instead.",
"source": "sonarlint",
"startLineNumber": 51,
"startColumn": 14,
"endLineNumber": 51,
"endColumn": 17
},{
"resource": "/home/tfo/work/static_code_test/src/main.c",
"owner": "sonarlint",
"code": "c:M23_007",
"severity": 4,
"message": "Use the value returned from \"printf\".",
"source": "sonarlint",
"startLineNumber": 53,
"startColumn": 7,
"endLineNumber": 53,
"endColumn": 13
}]