Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
couple of memory leaks fixes.
  • Loading branch information
devnexen authored and nerzhul committed Aug 10, 2016
1 parent 1be3894 commit 48b3bb9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/script/cpp_api/s_security.cpp
Expand Up @@ -285,6 +285,10 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path)

if (c == LUA_SIGNATURE[0]) {
lua_pushliteral(L, "Bytecode prohibited when mod security is enabled.");
std::fclose(fp);
if (path) {
delete [] chunk_name;
}
return false;
}

Expand All @@ -295,14 +299,26 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path)
size_t size = std::ftell(fp) - start;
char *code = new char[size];
ret = std::fseek(fp, start, SEEK_SET);
CHECK_FILE_ERR(ret, fp);
if (ret) {
lua_pushfstring(L, "%s: %s", path, strerror(errno));
std::fclose(fp);
delete [] code;
if (path) {
delete [] chunk_name;
}
return false;
}

size_t num_read = std::fread(code, 1, size, fp);
if (path) {
std::fclose(fp);
}
if (num_read != size) {
lua_pushliteral(L, "Error reading file to load.");
delete [] code;
if (path) {
delete [] chunk_name;
}
return false;
}

Expand Down
1 change: 1 addition & 0 deletions src/sound_openal.cpp
Expand Up @@ -144,6 +144,7 @@ SoundBuffer *load_opened_ogg_file(OggVorbis_File *oggFile,
ov_clear(oggFile);
infostream << "Audio: Error decoding "
<< filename_for_logging << std::endl;
delete snd;
return NULL;
}

Expand Down
4 changes: 3 additions & 1 deletion src/unittest/test_map_settings_manager.cpp
Expand Up @@ -75,8 +75,10 @@ std::string read_file_to_string(const std::string &filepath)
fseek(f, 0, SEEK_END);

long filesize = ftell(f);
if (filesize == -1)
if (filesize == -1) {
fclose(f);
return "";
}
rewind(f);

buf.resize(filesize);
Expand Down
1 change: 1 addition & 0 deletions src/util/areastore.cpp
Expand Up @@ -95,6 +95,7 @@ void AreaStore::deserialize(std::istream &is)
is.read(data, data_len);
a.data = std::string(data, data_len);
insertArea(&a);
delete [] data;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/util/srp.cpp
Expand Up @@ -542,7 +542,7 @@ static SRP_Result fill_buff()

if (!fp) return SRP_ERR;

if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) return SRP_ERR;
if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) { fclose(fp); return SRP_ERR; }
if (fclose(fp)) return SRP_ERR;
#endif
return SRP_OK;
Expand Down

0 comments on commit 48b3bb9

Please sign in to comment.