Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

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

Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby zazas321 » Fri Mar 22, 2024 1:24 pm

We use SonarLint to analyze the code. The default way to create a simple FreeRTOS task is as shown below:

Code: Select all

static void HELLO_TASK(void *param)
{
    UNUSED(param);
    for (;;)
    {
        printf("This is normal message1 without ANSI color code \n");
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
However, sonarlint complains about this and gives us a warning :
Make the type of this parameter a pointer-to-const. The current type of "param" is "void *".

What could be the issue with this and why is it giving a warning? I know its not a big deal but I am just curious to understand.

User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby mbratch » Fri Mar 22, 2024 1:55 pm

FreeRTOS has a macro with casting built in, `pdMS_TO_TICKS`. I use this all the time instead of literally putting the conversion in as `1000/period_in_ms`. So I would write:

Code: Select all

vTaskDelay(pdMS_TO_TICKS(portTICK_PERIOD_MS));
Have you tried that and does it get rid of the warning?
The only caveat is that the macro only works if `configTICK_RATE_HZ` is not greater than 1000.

MicroController
Posts: 1735
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby MicroController » Fri Mar 22, 2024 3:38 pm

zazas321 wrote:
Fri Mar 22, 2024 1:24 pm
Make the type of this parameter a pointer-to-const. The current type of "param" is "void *".

What could be the issue with this and why is it giving a warning? I know its not a big deal but I am just curious to understand.
Not 100% sure about Sonar here, but I assume that Sonar sees that you 1) receive a pointer as argument and 2) never write to the pointer's target. If you never (intend to) write, declaring the parameter 'const <whatever>*' would be cleaner/safer/more explicit and hence better practice, because it expresses that what's pointed to is input and input-only to your function, and might spare hypothetical callers of your function the need to cast away a const modifier.

(I just googled it and, apparently, 'pointee' is a term actually used for "an object a pointer points to"... So please mentally replace 'pointer's target' above by 'pointee' :))

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

Re: Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby zazas321 » Mon Mar 25, 2024 1:36 pm

It is still not clear to me how can I avoid this warning.

I have tried to declare my task with const void* param as shown below:

Code: Select all

static void HELLO_TASK(const void *param)
{
    UNUSED(param);
    for (;;)
    {
        printf("This is normal message1 without ANSI color code \n");
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
This time, I am getting another warning:

Code: Select all

Either add a parameter list or the "&" operator to this use of "HELLO_TASK".

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

Re: Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby zazas321 » Mon Mar 25, 2024 1:37 pm

mbratch wrote:
Fri Mar 22, 2024 1:55 pm
FreeRTOS has a macro with casting built in, `pdMS_TO_TICKS`. I use this all the time instead of literally putting the conversion in as `1000/period_in_ms`. So I would write:

Code: Select all

vTaskDelay(pdMS_TO_TICKS(portTICK_PERIOD_MS));
Have you tried that and does it get rid of the warning?
The only caveat is that the macro only works if `configTICK_RATE_HZ` is not greater than 1000.

I do not think that the issue is related to vTaskDelay(pdMS_TO_TICKS(portTICK_PERIOD_MS))

I think it is related to task function declaration:

Code: Select all

static void HELLO_TASK(void *param)


User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby mbratch » Mon Mar 25, 2024 3:04 pm

zazas321 wrote:
Mon Mar 25, 2024 1:37 pm
I do not think that the issue is related to vTaskDelay(pdMS_TO_TICKS(portTICK_PERIOD_MS))

I think it is related to task function declaration:

Code: Select all

static void HELLO_TASK(void *param)

You're right! I'm so sorry and misread your original post and missed the detail you noted. My apologies.
Make the type of this parameter a pointer-to-const. The current type of "param" is "void *".
What's the context of this function? Is it just a function, or is it a static member of a class? You are showing the definition of the function, but is there a declaration in a header? How does "UNUSED" expand in this case?

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

Re: Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby zazas321 » Mon Mar 25, 2024 3:38 pm

@mbratch

It is a simple static declaration of a freertos task function in my main.c. The full code is as following (my main.c):

Code: Select all



#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"
#include "esp_system.h"

#define UNUSED(x) (void)(x) // macro for unused parameters


static void HELLO_TASK(const void *param);

void app_main(void)
{
    printf("Hello world!\n");
    xTaskCreate( HELLO_TASK,  "Hello_task", 2048, NULL, 5, NULL);
    
}


static void HELLO_TASK(const void *param)
{
    UNUSED(param);
    for (;;)
    {
        printf("This is normal message1 without ANSI color code \n");
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

It is as simple as that. The error will persist if I declare a function in some .c file and declare a prototype in .h file so the fact that this is a static declaration does not have anything to do with it.
zazas321 wrote:How does "UNUSED" expand in this case?

Code: Select all

#define UNUSED(x) (void)(x) // macro for unused parameters
But it should not have anything to do with that warning. I can remove the UNUSED line and still get the same warning :)

MicroController
Posts: 1735
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby MicroController » Mon Mar 25, 2024 4:50 pm

zazas321 wrote:
Mon Mar 25, 2024 1:36 pm
It is still not clear to me how can I avoid this warning.
What do you mean? You got rid of the warning by using a pointer-to-const, didn't you?
zazas321 wrote:
Mon Mar 25, 2024 1:36 pm
This time, I am getting another warning:

Code: Select all

Either add a parameter list or the "&" operator to this use of "HELLO_TASK".
->

Code: Select all

xTaskCreate( &HELLO_TASK,  "Hello_task", 2048, NULL, 5, NULL);
xTaskCreate expects a function pointer as it's first argument. From the context, i.e. the function name 'HELLO_TASK' appearing without trailing (someParameter), the compiler can deduce that you actually want to refer to the function's address instead of calling HELLO_TASK, but making your intention explicit here is recommended - and may be required by some C standard.

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby chegewara » Mon Mar 25, 2024 6:52 pm

Those 2 are not the same declarations/definitions and one is not what xTaskCreate expects

Code: Select all

void HELLO_TASK(void *param); // GOOD
vs
void HELLO_TASK(const void *param); // WRONG
and here is error example

Code: Select all

error: invalid conversion from 'void (*)(const void*)' to 'TaskFunction_t' {aka 'void (*)(void*)'} [-fpermissive]

Another thing. It does not matter how you pass function, because function is always passed by address/pointer, its just for convenience
Both are the same and correct

Code: Select all

xTaskCreate( HELLO_TASK,  "Hello_task", 2048, NULL, 5, NULL); // w/o &
vs 
xTaskCreate( &HELLO_TASK,  "Hello_task", 2048, NULL, 5, NULL); // with &
As for the OP, i have no idea about SonarLint, but i found this, which is suggesting the problem is in SonarLint itself (not your code):
https://community.sonarsource.com/t/wro ... st/58181/3

MicroController
Posts: 1735
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Make the type of this parameter a pointer-to-const warning when declaring a FreeRTOS task

Postby MicroController » Mon Mar 25, 2024 8:29 pm

chegewara wrote:
Mon Mar 25, 2024 6:52 pm

Code: Select all

error: invalid conversion from 'void (*)(const void*)' to 'TaskFunction_t' {aka 'void (*)(void*)'} [-fpermissive]
You're partially right. Actually, it's a C++ error... In C I get a warning.
Another thing. It does not matter how you pass function, ...
To Sonar it does matter, as it tells us "Either add a parameter list or the "&" operator to this use of "HELLO_TASK"." If you have a better idea, please share.

Who is online

Users browsing this forum: kenone, QQ26750452 and 115 guests