Skip to content

FreeRTOS wrong macro definition for portEND_SWITCHING_ISR() #194

Open
@adam-szabo-xtalin

Description

@adam-szabo-xtalin

Describe the set-up
STM32F4 FW1.28.2
CMSIS V1
FreeRTOS Kernel V10.3.1

Describe the bug
In Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F/portmacro.h, line 92:

#define portEND_SWITCHING_ISR( xSwitchRequired ) if( xSwitchRequired != pdFALSE ) portYIELD()
#define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )

This macro is very unsafe to call, it can totally mess up a code.

Example1:
portYIELD_FROM_ISR( condition1 || condition2 );
Translates to:
if( condition1 || condition2 != pdFALSE ) portYIELD()
which is equal to
if( condition1 || ( condition2 != pdFALSE )) portYIELD()
which is not the intended behaviour, it should translate to:
if(( condition1 || condition2 ) != pdFALSE ) portYIELD()
Although the result of the two if() conditions is the same, it is a bad practice to make a "function-like" macro this way.

Example2:

if( condition1 )
  portYIELD_FROM_ISR( condition2 );
else
  foo();

Translates to:

if( condition1 )
  if( condition2 != pdFALSE )
    portYIELD();
else
  foo();

which is equal to

if( condition1 ) {
  if( condition2 != pdFALSE ) 
    portYIELD();
  else
    foo();
}

which is not the intended operation, it should translate to:

if( condition1 ) {
  if( condition2 != pdFALSE ) 
    portYIELD();
}
else {
  foo();
}

The correct macro is (additional parentheses around xSwitchRequired and do{} while(0) ):

#define portEND_SWITCHING_ISR( xSwitchRequired ) do{ if(( xSwitchRequired ) != pdFALSE ) portYIELD(); } while(0)
#define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )

How To Reproduce
There is no need to "reproduce", this is C syntax.
Please check all other macros in the generated code base, libraries. This kind of mistakes can cause a lot of trouble for the developers.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfreertosFree Real-Time Operating SystemmwMiddleware-related issue or pull-request.rtosReal-Time Operating System

Type

Projects

Status

Analyzed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions