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) virtual_identity *virtual_identity::find(void *vtable)
{ {
if (!vtable)
return NULL;
// Actually, a reader/writer lock would be sufficient, // Actually, a reader/writer lock would be sufficient,
// since the table is only written once per class. // since the table is only written once per class.
tthread::lock_guard<tthread::mutex> lock(*known_mutex); tthread::lock_guard<tthread::mutex> lock(*known_mutex);

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