VSCode SonarLint detecting errors

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

VSCode SonarLint detecting errors

Postby zazas321 » Tue Dec 19, 2023 12:29 pm

I an running a simple sample project on VSCode (esp-idf v5.0.3)

Code: Select all

/*
 * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"

void app_main(void)
{
    printf("Hello world!\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    uint32_t flash_size;
    esp_chip_info(&chip_info);
    printf("This is %s chip with %d CPU core(s), WiFi%s%s, ",
           CONFIG_IDF_TARGET,
           chip_info.cores,
           (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
           (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");

    unsigned major_rev = chip_info.revision / 100;
    unsigned minor_rev = chip_info.revision % 100;
    printf("silicon revision v%d.%d, ", major_rev, minor_rev);
    if (esp_flash_get_size(NULL, &flash_size) != ESP_OK)
    {
        printf("Get flash size failed");
        return;
    }

    printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
           (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());

    printf("esp_get_free_heap_size: %" PRIu32 " bytes\n", esp_get_free_heap_size());

    size_t size_to_allocate_1kb = 1000;
    uint8_t *ptr1;
    uint8_t *ptr2;

    ptr1 = malloc(size_to_allocate_1kb * sizeof(*ptr1));
    *(ptr1 + 5) = 555;
    printf("Minimum free heap size after allocating: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
    printf("esp_get_free_heap_size after allocating: %" PRIu32 " bytes\n", esp_get_free_heap_size());

    ptr2 = malloc(size_to_allocate_1kb * sizeof(*ptr2));
    printf("Minimum free heap size after allocating2: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
    printf("esp_get_free_heap_size after allocating2: %" PRIu32 " bytes\n", esp_get_free_heap_size());
}




We are using SonarLint extension for VSCode to give us some guidelines for writing a decent code. I have noticed that even with the
most basic sample, I am getting 10 Problems:
lint_issues.png
lint_issues.png (42.12 KiB) Viewed 17242 times

Code: Select all

[{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "C/C++: IntelliSense",
	"code": "1696",
	"severity": 8,
	"message": "#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (C:\\Users\\petrikas.lu\\Desktop\\WORK\\gitchangelog-test\\my_project\\main\\hello_world_main.c).",
	"source": "C/C++",
	"startLineNumber": 10,
	"startColumn": 1,
	"endLineNumber": 10,
	"endColumn": 31
},{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "C/C++: IntelliSense",
	"code": "1696",
	"severity": 8,
	"message": "cannot open source file \"freertos/FreeRTOS.h\"",
	"source": "C/C++",
	"startLineNumber": 10,
	"startColumn": 1,
	"endLineNumber": 10,
	"endColumn": 31
},{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "C/C++: IntelliSense",
	"code": "1696",
	"severity": 8,
	"message": "cannot open source file \"freertos/task.h\"",
	"source": "C/C++",
	"startLineNumber": 11,
	"startColumn": 1,
	"endLineNumber": 11,
	"endColumn": 27
},{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "C/C++: IntelliSense",
	"code": "1696",
	"severity": 8,
	"message": "cannot open source file \"esp_chip_info.h\"",
	"source": "C/C++",
	"startLineNumber": 12,
	"startColumn": 1,
	"endLineNumber": 12,
	"endColumn": 27
},{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "C/C++: IntelliSense",
	"code": "1696",
	"severity": 8,
	"message": "cannot open source file \"esp_flash.h\"",
	"source": "C/C++",
	"startLineNumber": 13,
	"startColumn": 1,
	"endLineNumber": 13,
	"endColumn": 23
},{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "sonarlint",
	"code": "c:S5350",
	"severity": 4,
	"message": "Make the type of this variable a pointer-to-const. The current type of \"ptr2\" is \"unsigned char *\".",
	"source": "sonarlint",
	"startLineNumber": 47,
	"startColumn": 5,
	"endLineNumber": 47,
	"endColumn": 18
},{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "sonarlint",
	"code": "c:S5276",
	"severity": 4,
	"message": "implicit conversion from 'int' to 'uint8_t' (aka 'unsigned char') changes value from 555 to 43",
	"source": "sonarlint",
	"startLineNumber": 50,
	"startColumn": 19,
	"endLineNumber": 50,
	"endColumn": 22
},{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "sonarlint",
	"code": "c:S3584",
	"severity": 4,
	"message": "Potential leak of memory pointed to by 'ptr1' [+10 locations]",
	"source": "sonarlint",
	"startLineNumber": 51,
	"startColumn": 76,
	"endLineNumber": 51,
	"endColumn": 108
},{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "sonarlint",
	"code": "c:S1854",
	"severity": 4,
	"message": "Value stored to 'ptr2' is never read",
	"source": "sonarlint",
	"startLineNumber": 54,
	"startColumn": 5,
	"endLineNumber": 54,
	"endColumn": 9
},{
	"resource": "/c:/Users/petrikas.lu/Desktop/WORK/gitchangelog-test/my_project/main/hello_world_main.c",
	"owner": "sonarlint",
	"code": "c:S3584",
	"severity": 4,
	"message": "Potential leak of memory pointed to by 'ptr2' [+10 locations]",
	"source": "sonarlint",
	"startLineNumber": 55,
	"startColumn": 77,
	"endLineNumber": 55,
	"endColumn": 109
}]
I have ensured that the SonarLint is pointing to the right compile_commands json:
compile_Commands.png
compile_Commands.png (10.16 KiB) Viewed 17242 times


Does anyone else use SonarLint and know what could be the problems? Can you recommend a way to solve them or recommend another linter?

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: VSCode SonarLint detecting errors

Postby liaifat85 » Tue Dec 19, 2023 1:43 pm

You can check this for the #include error : https://stackabuse.com/bytes/troublesho ... n-vs-code/

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: VSCode SonarLint detecting errors

Postby zazas321 » Wed Dec 20, 2023 1:22 pm

liaifat85 wrote:
Tue Dec 19, 2023 1:43 pm
You can check this for the #include error : https://stackabuse.com/bytes/troublesho ... n-vs-code/
I have tried to restart vscode as shown in the link that you have provided.

Additionally, I have tried the following:
iclude_path.png
iclude_path.png (25.53 KiB) Viewed 17128 times
But clicking on the lightbulb does not do anything except open the IntelliSense Configuration window:
intellisense.png
intellisense.png (115.9 KiB) Viewed 17128 times

Perhaps I should add a Compiler path in .vscode/c_cpp_properties.json as shown below:

compiler_path.png
compiler_path.png (44.48 KiB) Viewed 17128 times
What would be the compiler path for esp-idf projects?

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: VSCode SonarLint detecting errors

Postby liaifat85 » Sun Jan 07, 2024 8:52 pm

Not sure. I just hope somebody from the esp authority will look into this thread.

Who is online

Users browsing this forum: No registered users and 17 guests