Skip to content

Commit

Permalink
Fix #129, Reduce cyclomatic complexity of GetElfHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
jdfiguer authored and jdfiguer committed Jul 17, 2023
1 parent 46b29f8 commit 2295974
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions elf2cfetbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ int32 GetSrcFilename(void);
int32 GetDstFilename(void);
int32 OpenSrcFile(void);
int32 OpenDstFile(void);
int32 checkELFFileMagicNumber(void);
int32 GetElfHeader(void);
void SwapElfHeader(void);
int32 GetSectionHeader(int32 SectionIndex, union Elf_Shdr *SectionHeader);
Expand Down Expand Up @@ -1464,24 +1465,48 @@ int32 OpenDstFile(void)
*
*/

int32 GetElfHeader(void)
bool checkEndianness(int32 EndiannessCheck)

Check notice

Code scanning / CodeQL

Long function without assertion Note

All functions of more than 10 lines should have at least one assertion.
{
int32 Status = SUCCESS;
size_t NumHdrsRead = 0;
char VerboseStr[60];
int32 EndiannessCheck = 0x01020304;

if (((char *)&EndiannessCheck)[0] == 0x01)
{
ThisMachineIsLittleEndian = false;
return true;
}
else if (((char *)&EndiannessCheck)[0] != 0x04)
{
printf("Unable to determine endianness of this machine! (0x%02x, 0x%02x, 0x%02x, 0x%02x)\n",
((char *)&EndiannessCheck)[0], ((char *)&EndiannessCheck)[1], ((char *)&EndiannessCheck)[2],
((char *)&EndiannessCheck)[3]);
return FAILED;
return false;
}
return true;
}

/**
*
*/

int32 checkELFFileMagicNumber(void)
{
if (get_e_ident(&ElfHeader, EI_MAG0) != ELFMAG0 || get_e_ident(&ElfHeader, EI_MAG1) != ELFMAG1 ||
get_e_ident(&ElfHeader, EI_MAG2) != ELFMAG2 || get_e_ident(&ElfHeader, EI_MAG3) != ELFMAG3)
return FAILED;
return SUCCESS;
}

/**
*
*/

int32 GetElfHeader(void)

Check notice

Code scanning / CodeQL

Long function without assertion Note

All functions of more than 10 lines should have at least one assertion.

Check notice

Code scanning / CodeQL

Function too long Note

GetElfHeader has too many lines (193, while 60 are allowed).
{
int32 Status = SUCCESS;
size_t NumHdrsRead = 0;
char VerboseStr[60];
int32 EndiannessCheck = 0x01020304;

if (!checkEndianness(EndiannessCheck))

Check warning

Code scanning / CodeQL

Side effect in a Boolean expression Warning

This Boolean expression is not side-effect free.
return FAILED;

/* Begin by reading e_ident characters */
NumHdrsRead = fread(&ElfHeader, EI_NIDENT, 1, SrcFileDesc);
Expand All @@ -1493,20 +1518,15 @@ int32 GetElfHeader(void)
}

if (Verbose)
printf("ELF Header:\n");
if (Verbose)
printf(" e_ident[EI_MAG0..3] = 0x%02x,%c%c%c\n", get_e_ident(&ElfHeader, EI_MAG0),
get_e_ident(&ElfHeader, EI_MAG1), get_e_ident(&ElfHeader, EI_MAG2), get_e_ident(&ElfHeader, EI_MAG3));
{
printf("ELF Header:\n"
" e_ident[EI_MAG0..3] = 0x%02x,%c%c%c\n",
get_e_ident(&ElfHeader, EI_MAG0), get_e_ident(&ElfHeader, EI_MAG1), get_e_ident(&ElfHeader, EI_MAG2),
get_e_ident(&ElfHeader, EI_MAG3));
}

/* Verify the ELF file magic number */
if (get_e_ident(&ElfHeader, EI_MAG0) != ELFMAG0)
Status = FAILED;
if (get_e_ident(&ElfHeader, EI_MAG1) != ELFMAG1)
Status = FAILED;
if (get_e_ident(&ElfHeader, EI_MAG2) != ELFMAG2)
Status = FAILED;
if (get_e_ident(&ElfHeader, EI_MAG3) != ELFMAG3)
Status = FAILED;
Status = checkELFFileMagicNumber();

if (Status == FAILED)
{
Expand Down

0 comments on commit 2295974

Please sign in to comment.