Skip to content

Commit

Permalink
fix safe mode triggering when opening unreadable f
Browse files Browse the repository at this point in the history
  • Loading branch information
tildearrow committed Oct 29, 2023
1 parent 497c32b commit 5febd48
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,18 +566,21 @@ int main(int argc, char** argv) {
FILE* f=ps_fopen(fileName.c_str(),"rb");
if (f==NULL) {
reportError(fmt::sprintf("couldn't open file! (%s)",strerror(errno)));
e.everythingOK();
finishLogFile();
return 1;
}
if (fseek(f,0,SEEK_END)<0) {
reportError(fmt::sprintf("couldn't open file! (couldn't get file size: %s)",strerror(errno)));
e.everythingOK();
fclose(f);
finishLogFile();
return 1;
}
ssize_t len=ftell(f);
if (len==(SIZE_MAX>>1)) {
reportError(fmt::sprintf("couldn't open file! (couldn't get file length: %s)",strerror(errno)));
e.everythingOK();
fclose(f);
finishLogFile();
return 1;
Expand All @@ -588,20 +591,23 @@ int main(int argc, char** argv) {
} else {
reportError(fmt::sprintf("couldn't open file! (tell error: %s)",strerror(errno)));
}
e.everythingOK();
fclose(f);
finishLogFile();
return 1;
}
unsigned char* file=new unsigned char[len];
if (fseek(f,0,SEEK_SET)<0) {
reportError(fmt::sprintf("couldn't open file! (size error: %s)",strerror(errno)));
e.everythingOK();
fclose(f);
delete[] file;
finishLogFile();
return 1;
}
if (fread(file,1,(size_t)len,f)!=(size_t)len) {
reportError(fmt::sprintf("couldn't open file! (read error: %s)",strerror(errno)));
e.everythingOK();
fclose(f);
delete[] file;
finishLogFile();
Expand All @@ -610,6 +616,7 @@ int main(int argc, char** argv) {
fclose(f);
if (!e.load(file,(size_t)len)) {
reportError(fmt::sprintf("could not open file! (%s)",e.getLastError()));
e.everythingOK();
finishLogFile();
return 1;
}
Expand Down

0 comments on commit 5febd48

Please sign in to comment.