Skip to content

Commit 48b3bb9

Browse files
devnexennerzhul
authored andcommitted
couple of memory leaks fixes.
1 parent 1be3894 commit 48b3bb9

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

src/script/cpp_api/s_security.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path)
285285

286286
if (c == LUA_SIGNATURE[0]) {
287287
lua_pushliteral(L, "Bytecode prohibited when mod security is enabled.");
288+
std::fclose(fp);
289+
if (path) {
290+
delete [] chunk_name;
291+
}
288292
return false;
289293
}
290294

@@ -295,14 +299,26 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path)
295299
size_t size = std::ftell(fp) - start;
296300
char *code = new char[size];
297301
ret = std::fseek(fp, start, SEEK_SET);
298-
CHECK_FILE_ERR(ret, fp);
302+
if (ret) {
303+
lua_pushfstring(L, "%s: %s", path, strerror(errno));
304+
std::fclose(fp);
305+
delete [] code;
306+
if (path) {
307+
delete [] chunk_name;
308+
}
309+
return false;
310+
}
299311

300312
size_t num_read = std::fread(code, 1, size, fp);
301313
if (path) {
302314
std::fclose(fp);
303315
}
304316
if (num_read != size) {
305317
lua_pushliteral(L, "Error reading file to load.");
318+
delete [] code;
319+
if (path) {
320+
delete [] chunk_name;
321+
}
306322
return false;
307323
}
308324

src/sound_openal.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ SoundBuffer *load_opened_ogg_file(OggVorbis_File *oggFile,
144144
ov_clear(oggFile);
145145
infostream << "Audio: Error decoding "
146146
<< filename_for_logging << std::endl;
147+
delete snd;
147148
return NULL;
148149
}
149150

src/unittest/test_map_settings_manager.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ std::string read_file_to_string(const std::string &filepath)
7575
fseek(f, 0, SEEK_END);
7676

7777
long filesize = ftell(f);
78-
if (filesize == -1)
78+
if (filesize == -1) {
79+
fclose(f);
7980
return "";
81+
}
8082
rewind(f);
8183

8284
buf.resize(filesize);

src/util/areastore.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void AreaStore::deserialize(std::istream &is)
9595
is.read(data, data_len);
9696
a.data = std::string(data, data_len);
9797
insertArea(&a);
98+
delete [] data;
9899
}
99100
}
100101

src/util/srp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ static SRP_Result fill_buff()
542542

543543
if (!fp) return SRP_ERR;
544544

545-
if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) return SRP_ERR;
545+
if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) { fclose(fp); return SRP_ERR; }
546546
if (fclose(fp)) return SRP_ERR;
547547
#endif
548548
return SRP_OK;

0 commit comments

Comments
 (0)