Skip to content

Commit

Permalink
Use fileno and error handling on unable to limit permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
martintc committed Oct 11, 2021
1 parent 810a316 commit 1fdfa64
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions ut_assert/src/uttools.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@ bool UtMem2BinFile(const void *Memory, const char *Filename, uint32 Length)
FILE * fp;
struct stat dststat;

if ((fchmod(fp, dststat.st_mode & ~(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) == 0)
{
fwrite(Memory, Length, 1, fp);
fclose(fp);
return (true);
if (fp = fopen(Filename, "w")) {
int fd = fileno(fp);
if ((fchmod(fd, dststat.st_mode & ~(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) == 0)
{
fwrite(Memory, Length, 1, fp);
fclose(fp);
return (true);
} else {
printf("Unable to limit permissions on file");
fclose(fp);
return (false);
}
}
else
{
Expand Down Expand Up @@ -106,7 +113,8 @@ bool UtMem2HexFile(const void *Memory, const char *Filename, uint32 Length)

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

for (i = 0; i < Length; i += 16)
Expand Down

0 comments on commit 1fdfa64

Please sign in to comment.