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 #85, Add checks for all return values from fseek() #123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
50 changes: 45 additions & 5 deletions elf2cfetbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,15 @@ int32 GetSectionHeader(int32 SectionIndex, union Elf_Shdr *SectionHeader)
SeekOffset = SectionHeaderStringTableDataOffset + get_sh_name(SectionHeader);
if (Verbose)
printf(" sh_name = 0x%08x - ", get_sh_name(SectionHeader));
fseek(SrcFileDesc, SeekOffset, SEEK_SET);

Status = fseek(SrcFileDesc, SeekOffset, SEEK_SET);

if (Status != 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be comparing to SeekOffset, not 0? fseek() returns the actual offset in the file on success, or -1 on error. So alternatively it could check for Status < 0. But I think != 0 is incorrect.

Copy link
Contributor Author

@thnkslprpt thnkslprpt May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jphickey
Hi Joe - can you point me to the documentation for fseek() that describes that? - I was definitely under the impression that it returns 0 on success.

{
printf("Error setting file ptr after retrieving sh_name for Section Header #%d in file '%s'\n",
SectionIndex, SrcFilename);
return FAILED;
}

while ((VerboseStr[i] = fgetc(SrcFileDesc)) != '\0')
{
Expand Down Expand Up @@ -1926,7 +1934,15 @@ int32 GetSymbol(int32 SymbolIndex, union Elf_Sym *Symbol)
SeekOffset = StringTableDataOffset + get_st_name(Symbol);
if (Verbose)
printf(" st_name = 0x%08x - ", get_st_name(Symbol));
fseek(SrcFileDesc, SeekOffset, SEEK_SET);

Status = fseek(SrcFileDesc, SeekOffset, SEEK_SET);

if (Status != 0)
{
printf("Error setting file ptr after retrieving st_name for Symbol #%d in file '%s'\n", SymbolIndex,
SrcFilename);
return FAILED;
}

while ((i < sizeof(VerboseStr)) && ((VerboseStr[i] = fgetc(SrcFileDesc)) != '\0'))
{
Expand Down Expand Up @@ -2279,7 +2295,15 @@ int32 GetTblDefInfo(void)
printf("Error: SeekOffset may not be %lu\n", (long unsigned int)calculated_offset);
Status = FAILED;
}
fseek(SrcFileDesc, SeekOffset, SEEK_SET);

Status = fseek(SrcFileDesc, SeekOffset, SEEK_SET);

if (Status != 0)
{
printf("Error setting file ptr during read-in of '%s' in file '%s'\n", TBL_DEF_SYMBOL_NAME, SrcFilename);
return FAILED;
}

NumDefsRead = fread(&TblFileDef, sizeof(CFE_TBL_FileDef_t), 1, SrcFileDesc);

/* ensuring all are strings are null-terminated */
Expand Down Expand Up @@ -2433,7 +2457,15 @@ int32 LocateAndReadUserObject(void)
printf("Error: SeekOffset may not be %lu\n", (long unsigned int)calculated_offset);
Status = FAILED;
}
fseek(SrcFileDesc, SeekOffset, SEEK_SET);

Status = fseek(SrcFileDesc, SeekOffset, SEEK_SET);

if (Status != 0)
{
printf("Error setting file ptr while locating data for '%s' object in file '%s'\n",
TblFileDef.ObjectName, SrcFilename);
return FAILED;
}

/* Determine if the elf file contained the size of the object */
if (get_st_size(SymbolPtrs[UserObjSymbolIndex]) != 0)
Expand Down Expand Up @@ -2474,8 +2506,16 @@ int32 LocateAndReadUserObject(void)
j = 0;
}
}

/* Reset the file pointer */
fseek(SrcFileDesc, SeekOffset, SEEK_SET);
Status = fseek(SrcFileDesc, SeekOffset, SEEK_SET);

if (Status != 0)
{
printf("Error resetting file ptr after retrieving '%s' object data in file '%s'\n",
TblFileDef.ObjectName, SrcFilename);
return FAILED;
}
}
}
}
Expand Down