From 1c5c4c048c83eaa440e82f9d4a3394338a29afda Mon Sep 17 00:00:00 2001 From: ruevs Date: Mon, 1 Feb 2021 15:43:19 +0200 Subject: [PATCH] Fix a crash when opening an empty file. Fixes: https://github.com/solvespace/solvespace/issues/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 :-) --- src/file.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/file.cpp b/src/file.cpp index f36e49b3..160b5ec7 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -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."));