-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat: replace sprintf with snprintf #746
feat: replace sprintf with snprintf #746
Conversation
@araujo88 Thank you for the PR. As a convention, we put config macros in |
@ydhuang28 You meant |
@araujo88 wrote:
For instance,
|
Unfortunately, the function It seems safe to use it because it limits the number of bytes written to a buffer. But a problem occurs when it doesn't fit, I have seen different behaviours:
In embedded applications, I use printf-stdarg.c, which has a safe implementation of the When |
@htibosch I agree with limiting the use of I seem to be missing the context of
As I understand the code in its current state, I don't see a protection on writing to the buffer? Perhaps I'm missing something? |
@araujo88 Correct, like @htibosch has mentioned, |
@araujo88 wrote:
Now I've got your point, same thing in FreeRTOS+TCP: you will also see that
Sorry, my mistake: the only case where |
} | ||
#else | ||
{ | ||
snprintf( pcWriteBuffer, configTASK_WRITE_BUFFER_LENGTH, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 snprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of configTASK_WRITE_BUFFER_LENGTH
here should instead be a local size_t variable that is initialized to configTASK_WRITE_BUFFER_LENGTH
, and it should shrink with each loop iteration wherever pcWriteBuffer
is advanced. The two variables should stay in sync.
@@ -6615,8 +6615,16 @@ static void prvResetNextTaskUnblockTime( void ) | |||
pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName ); | |||
|
|||
/* Write the rest of the string. */ | |||
sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */ | |||
pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */ | |||
#ifndef configTASK_WRITE_BUFFER_LENGTH |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce conditional code, configTASK_WRITE_BUFFER_LENGTH
could have a default value of SIZE_MAX if the developer doesn't define it. Actually, it seems SIZE_MAX was introduced in C99, so that symbol would need to be defined conditionally first, e.g., ( (size_t) -1 )
. This change would then eliminate all the calls to sprintf() as they would all be calls to snprintf().
As a (better?) alternative, if configUSE_STATS_FORMATTING_FUNCTIONS > 0
then we could generate a compile-time error if configTASK_WRITE_BUFFER_LENGTH
is not defined. This is not backward compatible but perhaps is justified. Would need direction from the FreeRTOS team on this question. EDIT: See next comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the FreeRTOS team thinks this improvement warrants breaking backward compatibility for vTaskList()
vTaskGetRunTimeStats()
, then the simpler, direct fix is to add a second formal parameter, eg, bufferSize
, to these two functions, and not to add configTASK_WRITE_BUFFER_LENGTH
at all. I think most developers would appreciate being "forced" into this improvement during a FreeRTOS upgrade. Otherwise, they likely wouldn't benefit from this improvement during the upgrade (and likely wouldn't even know about it). These are peripheral functions to the kernel, thus not being backward compatible might be OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current thought is that we don't want to break backwards compatibility (demo code maybe using this function and we want to minimize the impact to that), so I think adding just a config for this is fine.
Codecov ReportPatch coverage has no change and project coverage change:
Additional details and impacted files@@ Coverage Diff @@
## main #746 +/- ##
==========================================
+ Coverage 94.35% 94.55% +0.20%
==========================================
Files 6 6
Lines 2446 4392 +1946
Branches 598 1161 +563
==========================================
+ Hits 2308 4153 +1845
- Misses 85 154 +69
- Partials 53 85 +32
Flags with carried forward coverage won't be shown. Click here to find out more.
☔ View full report in Codecov by Sentry. |
Linked PR (adds the config macro definition): FreeRTOS/FreeRTOS#1071 |
This PR is covered by #802 and therefore will be closed. Thanks for creating this PR. |
Sorry, my mistake. Once PR #802 is merged. We will close this PR. |
Closing this in favor of #802. |
Description
Replaces
sprintf
withsnprintf
.Checklist:
Related Issue
#617