Skip to content

Commit

Permalink
Merge pull request #1372 from jphickey/fix-1346-alignsize
Browse files Browse the repository at this point in the history
Fix #1346, error if alignment size not a power of two
  • Loading branch information
astrogeco authored Apr 28, 2021
2 parents d9f125b + c518d3a commit c84feec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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 @@ -1997,6 +1997,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

0 comments on commit c84feec

Please sign in to comment.