Skip to content

Conversation

bradleysmith23
Copy link
Contributor

Description

Rule 13.3 states that a full expression containing the increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator. These were flagged as violations of the rule since the read of a volatile variable is considered to be a side effect in itself. As a result, using += 1 and -= 1 in place of ++ and -- brings these lines into compliance with the rule.

Test Steps

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link

codecov bot commented Feb 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (8c10944) 93.53% compared to head (335af45) 93.53%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #988   +/-   ##
=======================================
  Coverage   93.53%   93.53%           
=======================================
  Files           6        6           
  Lines        3200     3200           
  Branches      889      889           
=======================================
  Hits         2993     2993           
  Misses         92       92           
  Partials      115      115           
Flag Coverage Δ
unittests 93.53% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

include/list.h Outdated
@@ -326,7 +326,7 @@ typedef struct xLIST
} \
\
( pxItemToRemove )->pxContainer = NULL; \
( pxList->uxNumberOfItems )--; \
pxList->uxNumberOfItems -= 1U; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pxList->uxNumberOfItems has the type UBaseType_t . This operation leads to -Wconversion error , conversion from ‘unsigned int’ to ‘UBaseType_t’ . It can be changed to
pxList->uxNumberOfItems = ( UBaseType_t )( pxList->uxNumberOfItems - 1U );

tasks.c Outdated
@@ -255,7 +255,7 @@
pxTemp = pxDelayedTaskList; \
pxDelayedTaskList = pxOverflowDelayedTaskList; \
pxOverflowDelayedTaskList = pxTemp; \
xNumOfOverflows++; \
xNumOfOverflows += 1; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xNumOfOverflows has the type BaseType_t . This operation leads to -Wconversion error , conversion from ‘unsigned int’ to ‘BaseType_t’ . It can be changed to
xNumOfOverflows = ( BaseType_t )( xNumOfOverflows + 1U );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy to change it to this implementation, but went with leaving the compound assignment operators in and casting the value on the right side of the operator to be the appropriate type as the C standard states they differ in that "A compound assignment of the form E1 op= E2 differs from the simple assignment expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also curious here why this variable is volatile, because if it can change (and the post-increment causes a rmw race) then doing the +=1 will create the exact same race right?

tasks.c Outdated
@@ -3807,7 +3807,7 @@ void vTaskSuspendAll( void )

/* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
* is used to allow calls to vTaskSuspendAll() to nest. */
++uxSchedulerSuspended;
uxSchedulerSuspended += 1U;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uxSchedulerSuspended has the type UBaseType_t . This operation leads to -Wconversion error , conversion from ‘unsigned int’ to ‘UBaseType_t’ .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious why this used to be a pre-increment, that seems slightly odd, at least unexpected in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And of course like the other one I am curious why this variable is volatile, because if it is indeed volatile then the read/modify/write we are doing here creates a potential race condition where we read it, then add 1, then the value gets incremented from an ISR e.g. , and then we overwrite with an incorrect value here ?

@bradleysmith23
Copy link
Contributor Author

/bot run formatting

@bradleysmith23 bradleysmith23 marked this pull request as ready for review February 9, 2024 22:34
@bradleysmith23 bradleysmith23 requested a review from a team as a code owner February 9, 2024 22:34
@@ -326,7 +326,7 @@ typedef struct xLIST
} \
\
( pxItemToRemove )->pxContainer = NULL; \
( pxList->uxNumberOfItems )--; \
( ( pxList )->uxNumberOfItems ) -= ( UBaseType_t ) 1U; \
Copy link

@htibosch htibosch Feb 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One simplification:

-    ( ( pxList )->uxNumberOfItems ) -= ( UBaseType_t ) 1U; 
+    ( ( pxList )->uxNumberOfItems ) -= 1U; 

I think that the cast is not necessary. Have a try.

Also try:

-    xNumOfOverflows += ( BaseType_t ) 1;   
+    xNumOfOverflows += 1;   

EDIT Ok, reading back I see that a compiler doesn't agree with an implicit cast from 1U to UBaseType_t. That's a pity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that BaseType_t can be a different size from "unsigned", which means this will work fine with some types as BaseType_t but not others. These cases are tricky because compiling only with one type for BaseType_t will not expose all the problems !

Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@kar-rahul-aws kar-rahul-aws merged commit 4d34700 into FreeRTOS:main Feb 14, 2024
bradleysmith23 added a commit that referenced this pull request Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants