From fc197b5117394db8eeddc0d664a9ff7065924486 Mon Sep 17 00:00:00 2001 From: Eli Lindsey Date: Thu, 2 Apr 2020 14:37:45 -0400 Subject: [PATCH] fix undefined behavior in resource.hpp Today, uvw triggers undefined behavior in resource.hpp: runtime error: downcast of address which does not point to an object of type 'uvw::FsEventHandle' note: object is of type 'uvw::BaseHandle' It looks like we're saving the current handle in the void* data member so it can be retrieved on the next callback run. This downcast to a derived class from its parents ctor as it's being constructed isn't valid. Since we're storing this as a void* anyways and all the callsites using it need to do their own cast on retrieval, we can instead persist the this pointer. For downcasting to work in all cases the Resource class tree needs to be leftmost in Handler's multiple inheritance. Tested by verifying that my unit tests no longer show ubsan errors and uvw's test suite still passes. --- src/uvw/handle.hpp | 2 +- src/uvw/resource.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uvw/handle.hpp b/src/uvw/handle.hpp index 16b2356f..b18edd1f 100644 --- a/src/uvw/handle.hpp +++ b/src/uvw/handle.hpp @@ -26,7 +26,7 @@ struct CloseEvent {}; * Base type for all `uvw` handle types. */ template -class Handle: public BaseHandle, public Resource { +class Handle: public Resource, public BaseHandle { protected: static void closeCallback(uv_handle_t *handle) { Handle &ref = *(static_cast(handle->data)); diff --git a/src/uvw/resource.hpp b/src/uvw/resource.hpp index 969b654a..3f7b7780 100644 --- a/src/uvw/resource.hpp +++ b/src/uvw/resource.hpp @@ -42,7 +42,7 @@ public: Emitter{}, std::enable_shared_from_this{} { - this->get()->data = static_cast(this); + this->get()->data = this; } /**