Fix a crash when opening an empty file.

Fixes: https://github.com/solvespace/solvespace/issues/918

The problem was that here:
11a8a0abd5/src/file.cpp (L480)
we cleared all groups. Then we tried to read an empty file and therefore this `while` exited immediately:
11a8a0abd5/src/file.cpp (L487)
and we came to here:
11a8a0abd5/src/file.cpp (L548)
where there was no `fileLoadError` and thus we were left with no groups, and so this assert failed:
11a8a0abd5/src/graphicswin.cpp (L394)
since is is not allowed to have no groups :-)
pull/922/head
ruevs 2021-02-01 15:43:19 +02:00 committed by phkahler
parent 11a8a0abd5
commit 1c5c4c048c
1 changed files with 8 additions and 0 deletions

View File

@ -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;
@ -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
@ -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."));