Skip to content

Commit 2295974

Browse files
jdfiguerjdfiguer
jdfiguer
authored and
jdfiguer
committed
Fix nasa#129, Reduce cyclomatic complexity of GetElfHeader
1 parent 46b29f8 commit 2295974

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

elf2cfetbl.c

+39-19
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ int32 GetSrcFilename(void);
5959
int32 GetDstFilename(void);
6060
int32 OpenSrcFile(void);
6161
int32 OpenDstFile(void);
62+
int32 checkELFFileMagicNumber(void);
6263
int32 GetElfHeader(void);
6364
void SwapElfHeader(void);
6465
int32 GetSectionHeader(int32 SectionIndex, union Elf_Shdr *SectionHeader);
@@ -1464,24 +1465,48 @@ int32 OpenDstFile(void)
14641465
*
14651466
*/
14661467

1467-
int32 GetElfHeader(void)
1468+
bool checkEndianness(int32 EndiannessCheck)
14681469
{
1469-
int32 Status = SUCCESS;
1470-
size_t NumHdrsRead = 0;
1471-
char VerboseStr[60];
1472-
int32 EndiannessCheck = 0x01020304;
1473-
14741470
if (((char *)&EndiannessCheck)[0] == 0x01)
14751471
{
14761472
ThisMachineIsLittleEndian = false;
1473+
return true;
14771474
}
14781475
else if (((char *)&EndiannessCheck)[0] != 0x04)
14791476
{
14801477
printf("Unable to determine endianness of this machine! (0x%02x, 0x%02x, 0x%02x, 0x%02x)\n",
14811478
((char *)&EndiannessCheck)[0], ((char *)&EndiannessCheck)[1], ((char *)&EndiannessCheck)[2],
14821479
((char *)&EndiannessCheck)[3]);
1483-
return FAILED;
1480+
return false;
14841481
}
1482+
return true;
1483+
}
1484+
1485+
/**
1486+
*
1487+
*/
1488+
1489+
int32 checkELFFileMagicNumber(void)
1490+
{
1491+
if (get_e_ident(&ElfHeader, EI_MAG0) != ELFMAG0 || get_e_ident(&ElfHeader, EI_MAG1) != ELFMAG1 ||
1492+
get_e_ident(&ElfHeader, EI_MAG2) != ELFMAG2 || get_e_ident(&ElfHeader, EI_MAG3) != ELFMAG3)
1493+
return FAILED;
1494+
return SUCCESS;
1495+
}
1496+
1497+
/**
1498+
*
1499+
*/
1500+
1501+
int32 GetElfHeader(void)
1502+
{
1503+
int32 Status = SUCCESS;
1504+
size_t NumHdrsRead = 0;
1505+
char VerboseStr[60];
1506+
int32 EndiannessCheck = 0x01020304;
1507+
1508+
if (!checkEndianness(EndiannessCheck))
1509+
return FAILED;
14851510

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

14951520
if (Verbose)
1496-
printf("ELF Header:\n");
1497-
if (Verbose)
1498-
printf(" e_ident[EI_MAG0..3] = 0x%02x,%c%c%c\n", get_e_ident(&ElfHeader, EI_MAG0),
1499-
get_e_ident(&ElfHeader, EI_MAG1), get_e_ident(&ElfHeader, EI_MAG2), get_e_ident(&ElfHeader, EI_MAG3));
1521+
{
1522+
printf("ELF Header:\n"
1523+
" e_ident[EI_MAG0..3] = 0x%02x,%c%c%c\n",
1524+
get_e_ident(&ElfHeader, EI_MAG0), get_e_ident(&ElfHeader, EI_MAG1), get_e_ident(&ElfHeader, EI_MAG2),
1525+
get_e_ident(&ElfHeader, EI_MAG3));
1526+
}
15001527

15011528
/* Verify the ELF file magic number */
1502-
if (get_e_ident(&ElfHeader, EI_MAG0) != ELFMAG0)
1503-
Status = FAILED;
1504-
if (get_e_ident(&ElfHeader, EI_MAG1) != ELFMAG1)
1505-
Status = FAILED;
1506-
if (get_e_ident(&ElfHeader, EI_MAG2) != ELFMAG2)
1507-
Status = FAILED;
1508-
if (get_e_ident(&ElfHeader, EI_MAG3) != ELFMAG3)
1509-
Status = FAILED;
1529+
Status = checkELFFileMagicNumber();
15101530

15111531
if (Status == FAILED)
15121532
{

0 commit comments

Comments
 (0)