From 6fc75a1b12bf7d9c157fe30211d8623e5087507b Mon Sep 17 00:00:00 2001 From: Phil Phauler Date: Thu, 3 Sep 2026 14:21:54 +0200 Subject: [PATCH] Fix #2811, close file descriptor after table load command CFE_TBL_LoadCmd opens a file via TxnOpenTableLoadFile but never closes it; TxnFinish only releases the registry lock. Every load command leaks one OS file descriptor, eventually exhausting the OSAL file table. Close the file inside the open-success block, the way CFE_TBL_LoadFromFile already does with the same helper. The descriptor exported on a failed open is not to be trusted (the OSAL stub hands back a fake id), so no close is attempted there. Tests assert OS_close is called once after a successful load and after a failure that follows the open, and not at all when the open fails. --- modules/tbl/fsw/src/cfe_tbl_task_cmds.c | 23 ++++++++++++++--------- modules/tbl/ut-coverage/tbl_ut_load.c | 6 ++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/modules/tbl/fsw/src/cfe_tbl_task_cmds.c b/modules/tbl/fsw/src/cfe_tbl_task_cmds.c index 0512c9128..e8c984cc7 100644 --- a/modules/tbl/fsw/src/cfe_tbl_task_cmds.c +++ b/modules/tbl/fsw/src/cfe_tbl_task_cmds.c @@ -418,6 +418,8 @@ CFE_Status_t CFE_TBL_LoadCmd(const CFE_TBL_LoadCmd_t *data) Status = CFE_TBL_TxnOpenTableLoadFile(&Txn, LoadFilename, &FileDescriptor, &Header); if (Status == CFE_SUCCESS) { + /* NOTE: the FileDescriptor is valid and must be closed before leaving this block */ + /* Locate specified table in registry (wrapped in a lock) */ /* NOTE: The header reading code ensures null term on the table name string, so its OK to pass direct */ CFE_TBL_TxnLockRegistry(&Txn); @@ -430,17 +432,20 @@ CFE_Status_t CFE_TBL_LoadCmd(const CFE_TBL_LoadCmd_t *data) } CFE_TBL_TxnUnlockRegistry(&Txn); - } - if (Status == CFE_SUCCESS) - { - Status = CFE_TBL_ValidateFileIsLoadable(&Txn, &Header.Tbl); - } + if (Status == CFE_SUCCESS) + { + Status = CFE_TBL_ValidateFileIsLoadable(&Txn, &Header.Tbl); + } - if (Status == CFE_SUCCESS) - { - /* Read the file content into the working buffer */ - Status = CFE_TBL_LoadContentFromFile(&Txn, FileDescriptor, Header.Tbl.Offset, Header.Tbl.NumBytes); + if (Status == CFE_SUCCESS) + { + /* Read the file content into the working buffer */ + Status = CFE_TBL_LoadContentFromFile(&Txn, FileDescriptor, Header.Tbl.Offset, Header.Tbl.NumBytes); + } + + /* Done with the file now -- must always close the file regardless of what happened */ + OS_close(FileDescriptor); } /* If all the above worked out, then set the meta info in the load buffer */ diff --git a/modules/tbl/ut-coverage/tbl_ut_load.c b/modules/tbl/ut-coverage/tbl_ut_load.c index e49b7f062..c1024b210 100644 --- a/modules/tbl/ut-coverage/tbl_ut_load.c +++ b/modules/tbl/ut-coverage/tbl_ut_load.c @@ -308,6 +308,8 @@ void Test_CFE_TBL_LoadCmd(void) UT_SetDefaultReturnValue(UT_KEY(OS_OpenCreate), OS_ERROR); UtAssert_INT32_EQ(CFE_TBL_LoadCmd(&LoadCmd), CFE_SUCCESS); CFE_UtAssert_COUNTER_INCR(CFE_TBL_Global.CommandErrorCounter); + /* Nothing was opened, so nothing is closed */ + UtAssert_STUB_COUNT(OS_close, 0); /* Test response to inability to find the table in the registry */ UT_InitData_TBL(); @@ -317,6 +319,8 @@ void Test_CFE_TBL_LoadCmd(void) UtAssert_INT32_EQ(CFE_TBL_LoadCmd(&LoadCmd), CFE_SUCCESS); CFE_UtAssert_COUNTER_INCR(CFE_TBL_Global.CommandErrorCounter); CFE_UtAssert_EVENTSENT(CFE_TBL_NO_SUCH_TABLE_ERR_EID); + /* The file was opened before the lookup failed and must not leak */ + UtAssert_STUB_COUNT(OS_close, 1); /* The rest of the tests will use registry 0, note empty name matches */ RegRec0Ptr->OwnerAppId = AppID; @@ -362,6 +366,8 @@ void Test_CFE_TBL_LoadCmd(void) UT_SetReadHeader(&StdFileHeader, sizeof(StdFileHeader)); UtAssert_INT32_EQ(CFE_TBL_LoadCmd(&LoadCmd), CFE_SUCCESS); CFE_UtAssert_COUNTER_INCR(CFE_TBL_Global.CommandCounter); + /* A successful load closes its file exactly once */ + UtAssert_STUB_COUNT(OS_close, 1); /* Test with differing amount of data from header's claim */ UT_InitData_TBL();