Skip to content

Commit

Permalink
Fix a crash when opening an empty file.
Browse files Browse the repository at this point in the history
Fixes: #918

The problem was that here:
https://github.com/solvespace/solvespace/blob/11a8a0abd5e21a0da6c818674f8b4b657a401a6c/src/file.cpp#L480
we cleared all groups. Then we tried to read an empty file and therefore this `while` exited immediately:
https://github.com/solvespace/solvespace/blob/11a8a0abd5e21a0da6c818674f8b4b657a401a6c/src/file.cpp#L487
and we came to here:
https://github.com/solvespace/solvespace/blob/11a8a0abd5e21a0da6c818674f8b4b657a401a6c/src/file.cpp#L548
where there was no `fileLoadError` and thus we were left with no groups, and so this assert failed:
https://github.com/solvespace/solvespace/blob/11a8a0abd5e21a0da6c818674f8b4b657a401a6c/src/graphicswin.cpp#L394
since is is not allowed to have no groups :-)
  • Loading branch information
ruevs authored and phkahler committed Feb 3, 2021
1 parent 11a8a0a commit 1c5c4c0
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ void SolveSpaceUI::LoadUsingTable(const Platform::Path &filename, char *key, cha
}

bool SolveSpaceUI::LoadFromFile(const Platform::Path &filename, bool canCancel) {
bool fileIsEmpty = true;
allConsistent = false;
fileLoadError = false;

Expand All @@ -485,6 +486,8 @@ bool SolveSpaceUI::LoadFromFile(const Platform::Path &filename, bool canCancel)

char line[1024];
while(fgets(line, (int)sizeof(line), fh)) {
fileIsEmpty = false;

char *s = strchr(line, '\n');
if(s) *s = '\0';
// We should never get files with \r characters in them, but mailers
Expand Down Expand Up @@ -545,6 +548,11 @@ bool SolveSpaceUI::LoadFromFile(const Platform::Path &filename, bool canCancel)

fclose(fh);

if(fileIsEmpty) {
Error(_("The file is empty. It may be corrupt."));
NewFile();
}

if(fileLoadError) {
Error(_("Unrecognized data in file. This file may be corrupt, or "
"from a newer version of the program."));
Expand Down

0 comments on commit 1c5c4c0

Please sign in to comment.