[BUG] POSIX (GCC) port: thread marker leaked and thread mis-identified when `pthread_setspecific()` fails

Đang mở
#1,447 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

@94xhn đang làm issue này rồi.

Từ ngày 12/7/2026.

  • #1452 của @94xhn — đang mở
  • #1453 của @saikumar-mandaji — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
Nửa ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
c
Lĩnh vực
operating-systems

Hướng nghiên cứu

The relevant code is in portable/ThirdParty/GCC/Posix/port.c: prvMarkAsFreeRTOSThread(), prvThreadKeyDestructor() and prvIsFreeRTOSThread(). Before starting, read the two linked open pull requests (#1452 and #1453) to see what they already change, since the work is under way. Done means the pthread_setspecific() failure path frees the marker and applies a defined failure policy, and pthread_key_create() status is checked.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug

In portable/ThirdParty/GCC/Posix/port.c, prvMarkAsFreeRTOSThread() allocates a one-byte thread marker and stores it in thread-specific storage via pthread_setspecific(), but ignores the return value. If pthread_setspecific() fails, the marker is neither stored (so the TLS destructor never frees it) nor freed locally, leaking it — and the thread is left unmarked, so prvIsFreeRTOSThread() misclassifies it.

Affected code

portable/ThirdParty/GCC/Posix/port.c:

static void prvThreadKeyDestructor( void * pvData )
{
    free( pvData );
}

/* ... key created once ... */
    pthread_key_create( &xThreadKey, prvThreadKeyDestructor );   /* return ignored */

/* ... per FreeRTOS pthread ... */
    uint8_t * pucThreadData = NULL;

    pucThreadData = malloc( 1 );
    configASSERT( pucThreadData != NULL );

    *pucThreadData = 1;

    pthread_setspecific( xThreadKey, pucThreadData );            /* return ignored */

Problem

On the success path this is not a leak: ownership of the marker transfers to the
pthread TLS, and prvThreadKeyDestructor() frees it when the thread exits or is
cancelled.

On the failure path (pthread_setspecific() returns non-zero, e.g. EINVAL for
an invalid key or ENOMEM for insufficient resources):

  • The pointer is not stored in thread-specific storage.
  • The TLS destructor never receives it.
  • The local variable goes out of scope on return.
  • The one-byte allocation leaks.
  • prvIsFreeRTOSThread() will incorrectly report that this thread is not a
    FreeRTOS thread.

The correctness impact (thread identity) is arguably larger than the one-byte
leak itself.

Severity

Low as a leak: one byte plus allocator metadata per failure, only on an error
path that typically indicates the host is already resource-constrained. Worth
fixing together with the related robustness gaps below.

Suggested fix

    int xResult;

    xResult = pthread_setspecific( xThreadKey, pucThreadData );
    if( xResult != 0 )
    {
        free( pucThreadData );
        configASSERT( xResult == 0 );
    }

Because the function returns void, if configASSERT is compiled out a failure
policy is still needed — terminate the pthread, invoke a port-specific fatal
handler, or change the function to return status so the caller can abort
initialization. Freeing and continuing avoids the leak but leaves the thread
unmarked, which may lead to incorrect later control flow.

Related robustness issues (same area)

  • configASSERT( pucThreadData != NULL ) is followed by an unconditional
    *pucThreadData = 1;. With configASSERT compiled out and malloc() failing,
    this is a NULL dereference.
  • pthread_key_create()'s return value is also ignored; if key creation fails,
    pthread_setspecific() will subsequently fail. A complete fix should save and
    check the key initialization status.
Ngôn ngữ chính
C
Star
4.6k
Fork
1.6k
Merge trung bình
2 ngày 22 giờ
Pull request đã merge (30 ngày)
4

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của FreeRTOS/FreeRTOS-Kernel

Tất cả issue của FreeRTOS/FreeRTOS-Kernel

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.