Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
🐛 Fix copy ctor and dtor nullptr dereference
Browse files Browse the repository at this point in the history
Previous changes to inplace_function to reduce memory resulted in a case
where the vtable_ptr_ could be set to nullptr and be dereferenced. This
change checks if vtable_ptr_ is a null reference.

- 🎨 update code to the latest template library standard
- ⬆️ Bump version to 2.0.1
  • Loading branch information
Khalil Estell authored and kammce committed Aug 23, 2023
1 parent cba67c5 commit 5c1c247
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 62 deletions.
20 changes: 5 additions & 15 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.files import copy
from conan.tools.build import check_min_cppstd
from conan.errors import ConanInvalidConfiguration
import os


Expand All @@ -27,7 +26,7 @@

class libhal_conan(ConanFile):
name = "libhal"
version = "2.0.0"
version = "2.0.1"
license = "Apache-2.0"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://libhal.github.io/libhal"
Expand Down Expand Up @@ -59,19 +58,8 @@ def validate(self):
if self.settings.get_safe("compiler.cppstd"):
check_min_cppstd(self, self._min_cppstd)

def lazy_lt_semver(v1, v2):
lv1 = [int(v) for v in v1.split(".")]
lv2 = [int(v) for v in v2.split(".")]
min_length = min(len(lv1), len(lv2))
return lv1[:min_length] < lv2[:min_length]

compiler = str(self.settings.compiler)
version = str(self.settings.compiler.version)
minimum_version = self._compilers_minimum_version.get(compiler, False)

if minimum_version and lazy_lt_semver(version, minimum_version):
raise ConanInvalidConfiguration(
f"{self.name} {self.version} requires C++{self._min_cppstd}, which your compiler ({compiler}-{version}) does not support")
def build_requirements(self):
self.tool_requires("cmake/3.27.1")

def requirements(self):
self.requires("tl-function-ref/1.0.0")
Expand Down Expand Up @@ -109,6 +97,8 @@ def package_info(self):
if self._bare_metal:
self.cpp_info.defines = [
"BOOST_LEAF_EMBEDDED",
# TODO(#694): Remove this or have it be configurable. Users
# should not be forced to operate without thread support
"BOOST_LEAF_NO_THREADS"
]

Expand Down
24 changes: 0 additions & 24 deletions demos/libhal.tweaks.hpp

This file was deleted.

12 changes: 10 additions & 2 deletions include/libhal/third_party/inplace_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
* of the called function will NOT be valid.
* 4. Move SG14_INPLACE_FUNCTION_THROW is no longer needed and thus
* this library no longer needs exceptions.
* 5. After removing the `nullptr_t` constructors, there is a new edge case
* where the vtable_ptr_ is now nullptr and not an empty_vtable, which
* means that access to that vtable_ptr_ results in a null pointer
* dereference. A check must be put in the copy ctor and dtor.
*/

#pragma once
Expand Down Expand Up @@ -320,7 +324,9 @@ class inplace_function<R(Args...), Capacity, Alignment>

inplace_function& operator=(inplace_function other) noexcept
{
vtable_ptr_->destructor_ptr(std::addressof(storage_));
if (vtable_ptr_) {
vtable_ptr_->destructor_ptr(std::addressof(storage_));
}

vtable_ptr_ = std::exchange(
other.vtable_ptr_,
Expand All @@ -332,7 +338,9 @@ class inplace_function<R(Args...), Capacity, Alignment>

~inplace_function()
{
vtable_ptr_->destructor_ptr(std::addressof(storage_));
if (vtable_ptr_) {
vtable_ptr_->destructor_ptr(std::addressof(storage_));
}
}

R operator()(Args... args) const
Expand Down
3 changes: 3 additions & 0 deletions test_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"

def build_requirements(self):
self.tool_requires("cmake/3.27.1")

def requirements(self):
self.requires(self.tested_reference_str)

Expand Down
21 changes: 0 additions & 21 deletions test_package/libhal.tweaks.hpp

This file was deleted.

0 comments on commit 5c1c247

Please sign in to comment.