Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Replace sprintf with snprintf #617

Closed
Hadatko opened this issue Jan 26, 2023 · 7 comments
Closed

[Feature Request] Replace sprintf with snprintf #617

Hadatko opened this issue Jan 26, 2023 · 7 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Hadatko
Copy link
Contributor

Hadatko commented Jan 26, 2023

Is your feature request related to a problem? Please describe.
Hello, newer compilers are starting to complain related to usage of sprintf function (clang returning warning about deprecation). Their are suggesting to replace that call with snprintf. May you consider align your code?

Describe the solution you'd like
Replace sprintf with snprintf

Describe alternatives you've considered

How many devices will this feature impact?

What are your project timelines?

Additional context

'sprintf' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.

If you have the same (or similar) feature request, please upvote this issue with thumbs up 👍
and use the comments section to provide answers to the questions above.

@Hadatko Hadatko added the enhancement New feature or request label Jan 26, 2023
@n9wxu n9wxu added help wanted Extra attention is needed good first issue Good for newcomers labels Feb 2, 2023
@n9wxu
Copy link
Member

n9wxu commented Feb 2, 2023

sprintf and snprintf are both forbidden by MISRA rule 21.6. The intent is for the kernel to be fully MISRA compliant without exceptions. These print functions are used in the optional runtime statistics functions to fill a user supplied string buffer.

@araujo88
Copy link
Contributor

araujo88 commented Aug 5, 2023

I believe that this library provides a MISRA compliant alternative to snprintf. I guess this lib should be added on a separate PR unrelated to this issue specifically?

@ydhuang28
Copy link

The only instances of sprintf are in vTaskList and vTaskGetRunTimeStats that are (albeit in tasks.c) not considered to be part of the FreeRTOS kernel since their purpose are utility functions meant to produce human-readable output; thus, they may not have to be MISRA-compliant (? @n9wxu). We could move ahead with a PR that replaces sprintf with snprintf and that would solve the immediate issue.

@ydhuang28
Copy link

ydhuang28 commented Aug 8, 2023

@Hadatko Are you using the vTaskList and vTaskGetRunTimeStats functions? If not, simply set configUSE_STATS_FORMATTING_FUNCTIONS = 0 in your FreeRTOSConfig.h, and the complier should stop complaining.

Otherwise...

The parameter(s) of the API of vTaskList and vTaskGetRunTimeStats prevent us from using snprintf properly since they're only given a pointer to a char array of unknown length. A couple suggestions (with backwards-compatibility in mind, i.e., no API changes):

Approach 1

  • We introduce a new config macro for the length of the task printing buffer (e.g. something like #define configTASK_WRITE_BUFFER_LENGTH ( ( size_t ) 1024 ))
  • In vTaskList and vTaskGetRunTimeStats, do something like this:
/* ... getting task status with uxTaskGetSystemState ... */
#ifdef configTASK_WRITE_BUFFER_LENGTH
{
    sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", ... );
}
#else
{
    snprintf( pcWriteBuffer, configTASK_WRITE_BUFFER_LENGTH, "\t%c\t%u\t%u\t%u\r\n", ... );
}
#endif

Approach 2

We manually write to the buffer without using any stdio.h functions and put the buffer writing code in a prv helper function. This would still require somehow knowing how long the buffer is, so a config macro might still be necessary.

@Hadatko if you are using vTaskList and/or vTaskGetRunTimeStats in its current state, let us know if the approaches looks good.

@araujo88
Copy link
Contributor

Added macro configTASK_WRITE_BUFFER_LENGTH on this PR.

@Hadatko
Copy link
Contributor Author

Hadatko commented Aug 17, 2023

HI guys. Sorry for not being answering. I am not directly using mentioned functions. But i found configUSE_STATS_FORMATTING_FUNCTIONS set to 1. Now i putted it to 0 (thank you for heads up). I posted it here as i am using this RTOS so i wanted report issue once noticed ;)

Thank you for your PR. It is great that you want solve this issue ;)

@kar-rahul-aws
Copy link
Member

Hi @Hadatko
Closing this feature request, since the PR #802 which addresses this request has been merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants