Skip to content

Commit

Permalink
Fix #1171, check chmod return
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit b276438
Author: Todd Martin <[email protected]>
Date:   Thu Nov 4 09:50:31 2021 -0700

    remove white space for clanf-format

commit 491d173
Author: Todd Martin <[email protected]>
Date:   Thu Nov 4 09:49:04 2021 -0700

    fix indentation

commit 5962ec8
Author: Todd Martin <[email protected]>
Date:   Thu Nov 4 09:40:50 2021 -0700

    Conform to clang-style

commit ed2c0cb
Author: Todd Martin <[email protected]>
Date:   Tue Oct 12 06:03:23 2021 -0700

    Fix formatting

commit 99a8536
Author: Todd Martin <[email protected]>
Date:   Mon Oct 11 08:41:02 2021 -0700

    Get information about file using fstat

    Needed for fchmod.

commit c759f8a
Author: Todd Martin <[email protected]>
Date:   Mon Oct 11 08:33:56 2021 -0700

    Add in missing parenthesis

commit 220132d
Author: Todd Martin <[email protected]>
Date:   Mon Oct 11 08:32:38 2021 -0700

    Error handling for unable to limit permissions

commit 249c3f9
Author: Todd Martin <[email protected]>
Date:   Mon Oct 11 08:30:31 2021 -0700

    Fix formatting

commit 1fdfa64
Author: Todd Martin <[email protected]>
Date:   Mon Oct 11 08:29:34 2021 -0700

    Use fileno and error handling on unable to limit permissions

commit 810a316
Author: Todd Martin <[email protected]>
Date:   Mon Oct 11 08:22:21 2021 -0700

    Remove use of fstat and exchanged chmod with fchmod

commit 7a3cf3f
Author: Todd Martin <[email protected]>
Date:   Fri Oct 8 00:07:46 2021 -0700

    Fix name of variable during closing of file

commit 3ab23ac
Author: Todd Martin <[email protected]>
Date:   Fri Oct 8 00:02:59 2021 -0700

    Fix formatting

commit 355a1dc
Author: Todd Martin <[email protected]>
Date:   Fri Oct 8 00:02:43 2021 -0700

    Check the return value of chmod

commit 77db4f4
Author: Todd Martin <[email protected]>
Date:   Thu Oct 7 23:58:54 2021 -0700

    Add in stat after chmod

commit 6a63daf
Author: Todd Martin <[email protected]>
Date:   Thu Oct 7 23:51:03 2021 -0700

    Add another return false

    This return of false will be hit if all three calls in the nested if statements
    fails. In the case of chmod, stat and fopen failing.

commit d7cbf36
Author: Todd Martin <[email protected]>
Date:   Thu Oct 7 23:48:41 2021 -0700

    Check return value of chmod

    Rearranged the source since while checking if equal to zero, the chmod is
    performed along with the stat function. Previous implementation invoked stat
    twice in the same context leading to a redundant action.
  • Loading branch information
martintc committed Nov 4, 2021
1 parent 42af0f7 commit 4720153
Showing 1 changed file with 41 additions and 29 deletions.
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

0 comments on commit 4720153

Please sign in to comment.