From: Antoine Fontaine Date: Tue, 23 Mar 2021 02:35:37 +0000 (+0100) Subject: radiant/preferences: fix memory issue when saving pref X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=832101b1611ef0ae21f2b0bc9854ee8481727b89;p=xonotic%2Fnetradiant.git radiant/preferences: fix memory issue when saving pref Valgrind: ``` ==80929== Invalid read of size 2 ==80929== at 0x4842B30: memmove (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==80929== by 0x43618E: char* std::__copy_move::__copy_m(char const*, char const*, char*) (stl_algobase.h:386) ==80929== by 0x435ABC: char* std::__copy_move_a(char const*, char const*, char*) (stl_algobase.h:404) ==80929== by 0x436131: char* std::__copy_move_a2(char const*, char const*, char*) (stl_algobase.h:440) ==80929== by 0x4359BF: char* std::copy(char const*, char const*, char*) (stl_algobase.h:474) ==80929== by 0x59F28C: Array >::Array(char const*, char const*) (array.h:86) ==80929== by 0x59A2A0: Preferences_Save_Safe(PreferenceDictionary&, char const*) (preferences.cpp:207) ==80929== by 0x59C7A2: Preferences_Save() (preferences.cpp:919) ==80929== by 0x524500: Radiant_Shutdown() (mainframe.cpp:798) ==80929== by 0x5223FB: main (main.cpp:654) ==80929== Address 0xba88510 is 0 bytes after a block of size 64 alloc'd ==80929== at 0x483B723: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==80929== by 0x483E017: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==80929== by 0x5272F3F: g_realloc (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.6400.6) ==80929== by 0x5290003: ??? (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.6400.6) ==80929== by 0x5290066: g_string_sized_new (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.6400.6) ==80929== by 0x52905BA: g_string_new (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.6400.6) ==80929== by 0x59B65A: PrefsDlg::Init() (preferences.cpp:551) ==80929== by 0x5222A1: main (main.cpp:595) ``` --- diff --git a/radiant/preferences.cpp b/radiant/preferences.cpp index 6fca8c12..e57380eb 100644 --- a/radiant/preferences.cpp +++ b/radiant/preferences.cpp @@ -204,13 +204,10 @@ bool Preferences_Save( PreferenceDictionary& preferences, const char* filename ) } bool Preferences_Save_Safe( PreferenceDictionary& preferences, const char* filename ){ - Array tmpName( filename, filename + strlen( filename ) + 1 + 3 ); - *( tmpName.end() - 4 ) = 'T'; - *( tmpName.end() - 3 ) = 'M'; - *( tmpName.end() - 2 ) = 'P'; - *( tmpName.end() - 1 ) = '\0'; + std::string tmpName( filename ); + tmpName += "TMP"; - return Preferences_Save( preferences, tmpName.data() ) + return Preferences_Save( preferences, tmpName.c_str() ) && ( !file_exists( filename ) || file_remove( filename ) ) && file_move( tmpName.data(), filename ); }