Skip to content

Commit

Permalink
Fix #444, Adds JSC 2.1 Static Analysis comments and adds OS_strnlen
Browse files Browse the repository at this point in the history
This commit addresses issues flagged during static analysis by:
- Adding JSC 2.1 disposition comments.
- Replacing strlen with OS_strnlen.
  • Loading branch information
jdfiguer authored and jdfiguer committed Jun 21, 2024
1 parent 12eff1c commit e6c6266
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
20 changes: 2 additions & 18 deletions fsw/src/cf_cfdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,6 @@ CF_Logical_PduBuffer_t *CF_CFDP_ConstructPduHeader(const CF_Transaction_t *txn,
return ph;
}

/*----------------------------------------------------------------
*
* Internal helper routine only, not part of API.
*
*-----------------------------------------------------------------*/
static inline size_t CF_strnlen(const char *str, size_t maxlen)
{
const char *end = memchr(str, 0, maxlen);
if (end != NULL)
{
/* actual length of string is difference */
maxlen = end - str;
}
return maxlen;
}

/*----------------------------------------------------------------
*
* Application-scope internal function
Expand Down Expand Up @@ -344,10 +328,10 @@ CFE_Status_t CF_CFDP_SendMd(CF_Transaction_t *txn)
/* at this point, need to append filenames into md packet */
/* this does not actually copy here - that is done during encode */
md->source_filename.length =
CF_strnlen(txn->history->fnames.src_filename, sizeof(txn->history->fnames.src_filename));
OS_strnlen(txn->history->fnames.src_filename, sizeof(txn->history->fnames.src_filename));
md->source_filename.data_ptr = txn->history->fnames.src_filename;
md->dest_filename.length =
CF_strnlen(txn->history->fnames.dst_filename, sizeof(txn->history->fnames.dst_filename));
OS_strnlen(txn->history->fnames.dst_filename, sizeof(txn->history->fnames.dst_filename));
md->dest_filename.data_ptr = txn->history->fnames.dst_filename;

CF_CFDP_EncodeMd(ph->penc, md);
Expand Down
5 changes: 4 additions & 1 deletion fsw/src/cf_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,23 @@ CFE_Status_t CF_WriteHistoryEntryToFile(osal_id_t fd, const CF_History_t *histor
{
case 0:
CF_Assert(history->dir < CF_Direction_NUM);
/* SAD: No need to check snprintf return; buffer size is sufficient for the formatted output */
snprintf(linebuf, sizeof(linebuf), "SEQ (%lu, %lu)\tDIR: %s\tPEER %lu\tSTAT: %d\t",
(unsigned long)history->src_eid, (unsigned long)history->seq_num, CF_DSTR[history->dir],
(unsigned long)history->peer_eid, (int)history->txn_stat);
break;
case 1:
/* SAD: No need to check snprintf return; buffer size is sufficient for the formatted output */
snprintf(linebuf, sizeof(linebuf), "SRC: %s\t", history->fnames.src_filename);
break;
case 2:
default:
/* SAD: No need to check snprintf return; buffer size is sufficient for the formatted output */
snprintf(linebuf, sizeof(linebuf), "DST: %s\n", history->fnames.dst_filename);
break;
}

len = strlen(linebuf);
len = OS_strnlen(linebuf, (CF_FILENAME_MAX_LEN * 2) + 128);
ret = CF_WrappedWrite(fd, linebuf, len);
if (ret != len)
{
Expand Down
4 changes: 4 additions & 0 deletions unit-test/cf_cfdp_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ void Test_CF_CFDP_SendMd(void)
strncpy(history->fnames.src_filename, "src1", sizeof(history->fnames.src_filename));
txn->state = CF_TxnState_S1;
txn->fsize = 1234;
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(history->fnames.src_filename));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(history->fnames.dst_filename));
UtAssert_INT32_EQ(CF_CFDP_SendMd(txn), CFE_SUCCESS);
UtAssert_UINT32_EQ(md->size, txn->fsize);
UtAssert_STRINGBUF_EQ(md->dest_filename.data_ptr, md->dest_filename.length, history->fnames.dst_filename,
Expand All @@ -625,6 +627,8 @@ void Test_CF_CFDP_SendMd(void)
strncpy(history->fnames.src_filename, "src2", sizeof(history->fnames.src_filename));
txn->state = CF_TxnState_S2;
txn->fsize = 5678;
UT_SetDefaultReturnValue(UT_KEY(OS_strnlen), strlen(history->fnames.src_filename));
UT_SetDeferredRetcode(UT_KEY(OS_strnlen), 2, strlen(history->fnames.dst_filename));
UtAssert_INT32_EQ(CF_CFDP_SendMd(txn), CFE_SUCCESS);
UtAssert_UINT32_EQ(md->size, txn->fsize);
UtAssert_UINT32_EQ(md->dest_filename.length, sizeof(history->fnames.dst_filename));
Expand Down
2 changes: 1 addition & 1 deletion unit-test/stubs/cf_utils_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void UT_DefaultHandler_CF_WriteTxnQueueDataToFile(void *, UT_EntryKey_t, const U
* Generated stub function for CF_FindTransactionBySequenceNumber()
* ----------------------------------------------------
*/
CF_Transaction_t *CF_FindTransactionBySequenceNumber(CF_Channel_t *chan,
CF_Transaction_t *CF_FindTransactionBySequenceNumber(CF_Channel_t * chan,
CF_TransactionSeq_t transaction_sequence_number,
CF_EntityId_t src_eid)
{
Expand Down

0 comments on commit e6c6266

Please sign in to comment.