changed filename string handling for level file info structure
authorHolger Schemel <info@artsoft.org>
Fri, 24 Aug 2018 10:31:53 +0000 (12:31 +0200)
committerHolger Schemel <info@artsoft.org>
Fri, 24 Aug 2018 10:31:53 +0000 (12:31 +0200)
Before, level filenames in the level file info structure (members for
"basename" and "filename") were only handled as references to the
basename/filename strings allocated elsewhere, so if those strings
changed, the references in the level file info structure also pointed
to those changed strings. When determining filenames for both level
files and corresponding level templates, references to level filenames
were replaced by references to template filenames, which resulted in
unexpected/wrong behaviour.

Now, these level filenames are handled as copies of the strings, so if
the strings change, the string copies still stay the same.

src/files.c

index 3cb8fc725f4f67036aa167f5921fc058b09fca1c..b590641afc20b05848461be6327ae59081e52da5 100644 (file)
@@ -1855,8 +1855,9 @@ static void setFileInfoToDefaults(struct LevelFileInfo *level_file_info)
   level_file_info->nr = 0;
   level_file_info->type = LEVEL_FILE_TYPE_UNKNOWN;
   level_file_info->packed = FALSE;
-  level_file_info->basename = NULL;
-  level_file_info->filename = NULL;
+
+  setString(&level_file_info->basename, NULL);
+  setString(&level_file_info->filename, NULL);
 }
 
 int getMappedElement_SB(int, boolean);
@@ -2088,8 +2089,9 @@ static void setLevelFileInfo_SingleLevelFilename(struct LevelFileInfo *lfi,
 {
   lfi->type = type;
   lfi->packed = FALSE;
-  lfi->basename = getSingleLevelBasename(lfi->nr, lfi->type);
-  lfi->filename = getLevelFilenameFromBasename(lfi->basename);
+
+  setString(&lfi->basename, getSingleLevelBasename(lfi->nr, lfi->type));
+  setString(&lfi->filename, getLevelFilenameFromBasename(lfi->basename));
 }
 #endif
 
@@ -2105,8 +2107,9 @@ static void setLevelFileInfo_FormatLevelFilename(struct LevelFileInfo *lfi,
 
   lfi->type = type;
   lfi->packed = FALSE;
-  lfi->basename = basename;
-  lfi->filename = getLevelFilenameFromBasename(lfi->basename);
+
+  setString(&lfi->basename, basename);
+  setString(&lfi->filename, getLevelFilenameFromBasename(lfi->basename));
 }
 
 static void setLevelFileInfo_PackedLevelFilename(struct LevelFileInfo *lfi,
@@ -2114,8 +2117,9 @@ static void setLevelFileInfo_PackedLevelFilename(struct LevelFileInfo *lfi,
 {
   lfi->type = type;
   lfi->packed = TRUE;
-  lfi->basename = getPackedLevelBasename(lfi->type);
-  lfi->filename = getLevelFilenameFromBasename(lfi->basename);
+
+  setString(&lfi->basename, getPackedLevelBasename(lfi->type));
+  setString(&lfi->filename, getLevelFilenameFromBasename(lfi->basename));
 }
 
 static int getFiletypeFromID(char *filetype_id)
@@ -2187,7 +2191,7 @@ static void determineLevelFileInfo_Filename(struct LevelFileInfo *lfi)
                                         getSingleLevelBasename(-1));
 
     /* replace local level template filename with global template filename */
-    lfi->filename = getGlobalLevelTemplateFilename();
+    setString(&lfi->filename, getGlobalLevelTemplateFilename());
 
     /* no fallback if template file not existing */
     return;
@@ -6201,7 +6205,8 @@ void LoadLevelFromFilename(struct LevelInfo *level, char *filename)
 
   level_file_info.nr = 0;                      /* unknown level number */
   level_file_info.type = LEVEL_FILE_TYPE_RND;  /* no others supported yet */
-  level_file_info.filename = filename;
+
+  setString(&level_file_info.filename, filename);
 
   LoadLevelFromFileInfo(level, &level_file_info, FALSE);
 }