Stop field_offset from crashing due to missing vtables

Now, a pointer to NULL is cast to the type in question, avoiding the need to
call new() or delete() with potentially-misaligned types. Also,
virtual_identity::find has been tweaked to prevent it from crashing on NULL
vtable pointers.

This was suggested by Angavrilov.
develop
lethosor 2016-08-18 15:59:46 -04:00
parent c2997b9c79
commit 0b6597ddb2
2 changed files with 9 additions and 8 deletions

@ -255,6 +255,9 @@ virtual_identity *virtual_identity::get(virtual_ptr instance_ptr)
virtual_identity *virtual_identity::find(void *vtable)
{
if (!vtable)
return NULL;
// Actually, a reader/writer lock would be sufficient,
// since the table is only written once per class.
tthread::lock_guard<tthread::mutex> lock(*known_mutex);

@ -307,14 +307,12 @@ function field_ref(handle,...)
end
function field_offset(type,...)
local handle = df.new(type)
local _,haddr = df.sizeof(handle)
local _,addr = df.sizeof(field_ref(handle,...))
-- to aid in diagnosis of bad virtual dtors
io.stderr:write('memscan: deleting instance of '..tostring(type) .. '\n'):flush()
df.delete(handle)
io.stderr:write('successfully deleted\n'):flush()
return addr-haddr
local tmp = df.new('intptr_t') -- pointer to nullptr
local _, haddr = df.sizeof(tmp)
local handle = df.reinterpret_cast(type, tmp)
local _, addr = df.sizeof(field_ref(handle,...))
df.delete(tmp)
return addr - haddr
end
function MemoryArea:object_by_field(addr,type,...)