# Invalid task error value during initialization

**URL:** <https://forums.freertos.org/t/invalid-task-error-value-during-initialization/19317>\
**Category:** Kernel\
**Created:** [March 1, 2024, 8:13pm UTC](https://forums.freertos.org/t/invalid-task-error-value-during-initialization/19317 "2024-03-01T20:13:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![modjemai](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@modjemai](https://forums.freertos.org/u/modjemai)\
**Post date:** [March 1, 2024, 8:13pm UTC](https://forums.freertos.org/t/invalid-task-error-value-during-initialization/19317/1 "2024-03-01T20:13:38Z")

</div>

When creating a new task, the task error value is not initialized to zero. The variable `pxNewTCB->iTaskErrno` will end up taking what was in the memory address where it is located.

The fix is to initialize the TCB error value during initialization. Here is a suggested fix to the bug:  
the following function is located in tasks.c file:

```auto
static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
                                  const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
                                  const uint32_t ulStackDepth,
                                  void * const pvParameters,
                                  UBaseType_t uxPriority,
                                  TaskHandle_t * const pxCreatedTask,
                                  TCB_t * pxNewTCB,
                                  const MemoryRegion_t * const xRegions )
{
    StackType_t * pxTopOfStack;
    UBaseType_t x;

    // My code starts here.
    #if ( configUSE_POSIX_ERRNO == 1 )
        {
            /* Initialize task error value to zero. */
            pxNewTCB->iTaskErrno = 0;
        }
    #endif
    // My code ends here.
    .
    .
    .

```

The bug was observed after making FreeRTOS uses dynamic allocation in the heap.

---

<div class="post-metadata">

**Author:** ![archigup](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/archigup/32/1418_2.png) [@archigup](https://forums.freertos.org/u/archigup)\
**Post date:** [March 1, 2024, 10:58pm UTC](https://forums.freertos.org/t/invalid-task-error-value-during-initialization/19317/2 "2024-03-01T22:58:30Z")

</div>

For reference, also was posted to GitHub here: [Initialize task error value to zero when creating the TCB of a new task · Issue #1188 · FreeRTOS/FreeRTOS · GitHub](https://github.com/FreeRTOS/FreeRTOS/issues/1188)

We’d welcome a PR if you’d prefer to do so, else I can open the PR.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [March 3, 2024, 5:29pm UTC](https://forums.freertos.org/t/invalid-task-error-value-during-initialization/19317/3 "2024-03-03T17:29:47Z")

</div>

> [@modjemai](#):
>
> When creating a new task, the task error value is not initialized to zero.

The complete TCB is zeroed out here using `memset` - [FreeRTOS-Kernel/tasks.c at main · FreeRTOS/FreeRTOS-Kernel · GitHub](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L1681). Which FreeRTOS version are you using? Can you share your task creation code?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [March 3, 2024, 5:41pm UTC](https://forums.freertos.org/t/invalid-task-error-value-during-initialization/19317/4 "2024-03-03T17:41:56Z")

</div>

I verified it on a Cortex-M7 platform and `iTaskErrorno` is initizlied to zero:  
 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/d/d707034972c8cb01c8273cb0faaa252fa2bcf0b0.png)
