Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1346, error if alignment size not a power of two #1372

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions modules/es/fsw/src/cfe_es_generic_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ int32 CFE_ES_GenPoolInitialize(CFE_ES_GenPoolRecord_t *PoolRecPtr, size_t StartO

/*
* Convert alignment to a bit mask.
* This sets all LSBs if the passed in value was not actually a power of 2.
*/
if (AlignSize <= 1)
{
Expand All @@ -261,11 +260,16 @@ int32 CFE_ES_GenPoolInitialize(CFE_ES_GenPoolRecord_t *PoolRecPtr, size_t StartO
else
{
AlignMask = AlignSize - 1;
AlignMask |= AlignMask >> 1;
AlignMask |= AlignMask >> 2;
AlignMask |= AlignMask >> 4;
AlignMask |= AlignMask >> 8;
AlignMask |= AlignMask >> 16;
}

/*
* This confirms that the passed in value is a power of 2.
* The result of this check should always be 0 if so.
*/
if ((AlignMask & AlignSize) != 0)
{
CFE_ES_WriteToSysLog("%s(): invalid alignment for pool: %lu\n", __func__, (unsigned long)AlignSize);
return CFE_ES_BAD_ARGUMENT;
}

/* complete initialization of pool record entry */
Expand Down
7 changes: 7 additions & 0 deletions modules/es/ut-coverage/es_UT.c
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,13 @@ void TestGenericPool(void)

ES_ResetUnitTest();

/* Test Attempt to create pool with bad alignment / non power of 2 - should reject. */
memset(&UT_MemPoolDirectBuffer, 0xee, sizeof(UT_MemPoolDirectBuffer));
OffsetEnd = sizeof(UT_MemPoolDirectBuffer.Data);
UtAssert_INT32_EQ(CFE_ES_GenPoolInitialize(&Pool1, 0, OffsetEnd, 42, CFE_PLATFORM_ES_POOL_MAX_BUCKETS,
UT_POOL_BLOCK_SIZES, ES_UT_PoolDirectRetrieve, ES_UT_PoolDirectCommit),
CFE_ES_BAD_ARGUMENT);

/* Test successfully creating direct access pool, with alignment, no mutex */
memset(&UT_MemPoolDirectBuffer, 0xee, sizeof(UT_MemPoolDirectBuffer));
OffsetEnd = sizeof(UT_MemPoolDirectBuffer.Data);
Expand Down