Skip to content

Commit

Permalink
Fix heap use after free ASAN/Valgrind
Browse files Browse the repository at this point in the history
Summary:
Dont use c_str() of temp std::string in RocksLuaCompactionFilter::Name()
Closes facebook#1535

Differential Revision: D4199094

Pulled By: IslamAbdelRahman

fbshipit-source-id: e56ce62
  • Loading branch information
IslamAbdelRahman authored and Facebook Github Bot committed Nov 17, 2016
1 parent a4eb738 commit f39452e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion include/rocksdb/utilities/lua/rocks_lua_compaction_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ class RocksLuaCompactionFilter : public rocksdb::CompactionFilter {
explicit RocksLuaCompactionFilter(const RocksLuaCompactionFilterOptions& opt)
: options_(opt),
lua_state_wrapper_(opt.lua_script, opt.libraries),
error_count_(0) {}
error_count_(0),
name_("") {}

virtual bool Filter(int level, const Slice& key, const Slice& existing_value,
std::string* new_value,
Expand All @@ -180,6 +181,7 @@ class RocksLuaCompactionFilter : public rocksdb::CompactionFilter {
RocksLuaCompactionFilterOptions options_;
LuaStateWrapper lua_state_wrapper_;
mutable int error_count_;
mutable std::string name_;
};

} // namespace lua
Expand Down
10 changes: 6 additions & 4 deletions utilities/lua/rocks_lua_compaction_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ bool RocksLuaCompactionFilter::Filter(int level, const Slice& key,
}

const char* RocksLuaCompactionFilter::Name() const {
std::string name = "";
if (name_ != "") {
return name_.c_str();
}
auto* lua_state = lua_state_wrapper_.GetLuaState();
// push the right function into the lua stack
lua_getglobal(lua_state, kNameFunctionName.c_str());
Expand All @@ -146,7 +148,7 @@ const char* RocksLuaCompactionFilter::Name() const {
lua_tostring(lua_state, -1));
// pops out the lua error from stack
lua_pop(lua_state, 1);
return name.c_str();
return name_.c_str();
}

// check the return value
Expand All @@ -159,10 +161,10 @@ const char* RocksLuaCompactionFilter::Name() const {
const size_t name_size = lua_strlen(lua_state, -1);
assert(name_buf[name_size] == '\0');
assert(strlen(name_buf) <= name_size);
name = name_buf;
name_ = name_buf;
}
lua_pop(lua_state, 1);
return name.c_str();
return name_.c_str();
}

/* Not yet supported
Expand Down

0 comments on commit f39452e

Please sign in to comment.