Skip to content

Commit cee08ef

Browse files
committed
make sure the object is valid when retrieved from inner address
1 parent 3fec48a commit cee08ef

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

plugins/src/natives/str.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,39 @@ namespace Natives
122122
// native AmxString:str_addr(StringTag:str);
123123
AMX_DEFINE_NATIVE_TAG(str_addr, 1, cell)
124124
{
125-
decltype(strings::pool)::ref_container *str;
125+
std::shared_ptr<decltype(strings::pool)::ref_container> str;
126126
if(!strings::pool.get_by_id(params[1], str) && str != nullptr) amx_LogicError(errors::pointer_invalid, "string", params[1]);
127127
if(str == nullptr)
128128
{
129129
return strings::pool.get_null_address(amx);
130130
}
131-
strings::pool.set_cache(*str);
131+
strings::pool.set_cache(str);
132132
return strings::pool.get_relative_address(amx, *str);
133133
}
134134

135135
// native ConstAmxString:str_addr_const(ConstStringTag:str);
136136
AMX_DEFINE_NATIVE_TAG(str_addr_const, 1, cell)
137137
{
138-
decltype(strings::pool)::ref_container *str;
138+
std::shared_ptr<decltype(strings::pool)::ref_container> str;
139139
if(!strings::pool.get_by_id(params[1], str) && str != nullptr) amx_LogicError(errors::pointer_invalid, "string", params[1]);
140140
if(str == nullptr)
141141
{
142142
return strings::pool.get_null_address(amx);
143143
}
144-
strings::pool.set_cache(*str);
144+
strings::pool.set_cache(str);
145145
return strings::pool.get_relative_address(amx, *str);
146146
}
147147

148148
// native AmxStringBuffer:str_buf_addr(StringTag:str);
149149
AMX_DEFINE_NATIVE_TAG(str_buf_addr, 1, cell)
150150
{
151-
decltype(strings::pool)::ref_container *str;
151+
std::shared_ptr<decltype(strings::pool)::ref_container> str;
152152
if(!strings::pool.get_by_id(params[1], str)) amx_LogicError(errors::pointer_invalid, "string", params[1]);
153153
if(str == nullptr)
154154
{
155155
amx_LogicError(errors::operation_not_supported, "string");
156156
}
157-
strings::pool.set_cache(*str);
157+
strings::pool.set_cache(str);
158158
return strings::pool.get_relative_address(amx, *str);
159159
}
160160

plugins/src/natives/variant.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace Natives
7878
// native AmxVariant:var_addr(VariantTag:str);
7979
AMX_DEFINE_NATIVE_TAG(var_addr, 1, cell)
8080
{
81-
decltype(variants::pool)::ref_container *var;
81+
std::shared_ptr<decltype(variants::pool)::ref_container> var;
8282
if(!variants::pool.get_by_id(params[1], var) && var != nullptr) amx_LogicError(errors::pointer_invalid, "variant", params[1]);
8383
if(var == nullptr)
8484
{
@@ -88,33 +88,33 @@ namespace Natives
8888
{
8989
amx_LogicError(errors::operation_not_supported, "variant");
9090
}
91-
variants::pool.set_cache(*var);
91+
variants::pool.set_cache(var);
9292
return variants::pool.get_relative_address(amx, *var);
9393
}
9494

9595
// native ConstAmxVariant:var_addr_const(ConstVariantTag:str);
9696
AMX_DEFINE_NATIVE_TAG(var_addr_const, 1, cell)
9797
{
98-
decltype(variants::pool)::ref_container *var;
98+
std::shared_ptr<decltype(variants::pool)::ref_container> var;
9999
if(!variants::pool.get_by_id(params[1], var) && var != nullptr) amx_LogicError(errors::pointer_invalid, "variant", params[1]);
100100
if(var == nullptr)
101101
{
102102
return variants::pool.get_null_address(amx);
103103
}
104-
variants::pool.set_cache(*var);
104+
variants::pool.set_cache(var);
105105
return variants::pool.get_relative_address(amx, *var);
106106
}
107107

108108
// native AmxVariantBuffer:var_buf_addr(VariantTag:str);
109109
AMX_DEFINE_NATIVE_TAG(var_buf_addr, 1, cell)
110110
{
111-
decltype(variants::pool)::ref_container *var;
111+
std::shared_ptr<decltype(variants::pool)::ref_container> var;
112112
if(!variants::pool.get_by_id(params[1], var)) amx_LogicError(errors::pointer_invalid, "variant", params[1]);
113113
if(var == nullptr || (*var)->is_cell())
114114
{
115115
amx_LogicError(errors::operation_not_supported, "variant");
116116
}
117-
variants::pool.set_cache(*var);
117+
variants::pool.set_cache(var);
118118
return variants::pool.get_relative_address(amx, *var);
119119
}
120120

plugins/src/objects/object_pool.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class object_pool
226226
private:
227227
list_type global_object_list;
228228
list_type local_object_list;
229-
std::unordered_map<const_inner_ptr, ref_container*> inner_cache;
229+
std::unordered_map<const_inner_ptr, std::weak_ptr<ref_container>> inner_cache;
230230

231231
public:
232232
object_ptr add()
@@ -325,18 +325,21 @@ class object_pool
325325
return false;
326326
}
327327

328-
void set_cache(object_ptr obj)
328+
void set_cache(const std::shared_ptr<ref_container> &obj)
329329
{
330-
inner_cache[&obj->operator[](0)] = &obj;
330+
inner_cache[&(*obj)->operator[](0)] = obj;
331331
}
332332

333333
bool find_cache(const_inner_ptr ptr, ref_container *&obj) const
334334
{
335335
auto it = inner_cache.find(ptr);
336336
if(it != inner_cache.end())
337337
{
338-
obj = it->second;
339-
return true;
338+
if(auto lock = it->second.lock())
339+
{
340+
obj = lock.get();
341+
return true;
342+
}
340343
}
341344
return false;
342345
}

0 commit comments

Comments
 (0)