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 #1171, check chmod return #1179

Closed
Closed
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
70 changes: 41 additions & 29 deletions ut_assert/src/uttools.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,29 @@ bool UtMem2BinFile(const void *Memory, const char *Filename, uint32 Length)

if ((fp = fopen(Filename, "w")))
{
if (stat(Filename, &dststat) == 0)
int fd = fileno(fp);
fstat(fd, &dststat);
if ((fchmod(fd, dststat.st_mode & ~(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) == 0))
{
chmod(Filename, dststat.st_mode & ~(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH));
stat(Filename, &dststat);
fwrite(Memory, Length, 1, fp);
fclose(fp);
return (true);
}
else
{
printf("Unable to limit permissions on file");
fclose(fp);
return (false);
}

fwrite(Memory, Length, 1, fp);
fclose(fp);
return (true);
}
else
{
printf("UtMem2BinFile: Error Opening File: %s, %s\n", Filename, strerror(errno));
UtAssert_True(false, "UtMem2BinFile: Error Opening File");
return (false);
}

return (false);
}

bool UtBinFile2Mem(void *Memory, const char *Filename, uint32 Length)
Expand Down Expand Up @@ -110,32 +117,37 @@ bool UtMem2HexFile(const void *Memory, const char *Filename, uint32 Length)

if ((fp = fopen(Filename, "w")))
{
if (stat(Filename, &dststat) == 0)
int fd = fileno(fp);
fstat(fd, &dststat);
if ((fchmod(fd, dststat.st_mode & ~(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) == 0))
{
chmod(Filename, dststat.st_mode & ~(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH));
stat(Filename, &dststat);
}

for (i = 0; i < Length; i += 16)
{
fprintf(fp, " %06lX: ", (unsigned long)i);
for (j = 0; j < 16; j++)
{
if ((i + j) < Length)
fprintf(fp, "%02X ", ((uint8 *)Memory)[i + j]);
else
fprintf(fp, " ");
}
fprintf(fp, " ");
for (j = 0; j < 16; j++)
for (i = 0; i < Length; i += 16)
{
if ((i + j) < Length)
fprintf(fp, "%c", isprint(((uint8 *)Memory)[i + j]) ? ((uint8 *)Memory)[i + j] : '.');
fprintf(fp, " %06lX: ", (unsigned long)i);
for (j = 0; j < 16; j++)
{
if ((i + j) < Length)
fprintf(fp, "%02X ", ((uint8 *)Memory)[i + j]);
else
fprintf(fp, " ");
}
fprintf(fp, " ");
for (j = 0; j < 16; j++)
{
if ((i + j) < Length)
fprintf(fp, "%c", isprint(((uint8 *)Memory)[i + j]) ? ((uint8 *)Memory)[i + j] : '.');
}
fprintf(fp, "\n");
}
fprintf(fp, "\n");
fclose(fp);
return (true);
}
else
{
printf("Unable to limit permissions on file\n");
fclose(fp);
return (false);
}
fclose(fp);
return (true);
}
else
{
Expand Down