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

Commit 5c1c247

Browse files
Khalil Estellkammce
authored andcommitted
🐛 Fix copy ctor and dtor nullptr dereference
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
1 parent cba67c5 commit 5c1c247

File tree

5 files changed

+18
-62
lines changed

5 files changed

+18
-62
lines changed

conanfile.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from conan.tools.cmake import CMake, cmake_layout
1919
from conan.tools.files import copy
2020
from conan.tools.build import check_min_cppstd
21-
from conan.errors import ConanInvalidConfiguration
2221
import os
2322

2423

@@ -27,7 +26,7 @@
2726

2827
class libhal_conan(ConanFile):
2928
name = "libhal"
30-
version = "2.0.0"
29+
version = "2.0.1"
3130
license = "Apache-2.0"
3231
url = "https://github.com/conan-io/conan-center-index"
3332
homepage = "https://libhal.github.io/libhal"
@@ -59,19 +58,8 @@ def validate(self):
5958
if self.settings.get_safe("compiler.cppstd"):
6059
check_min_cppstd(self, self._min_cppstd)
6160

62-
def lazy_lt_semver(v1, v2):
63-
lv1 = [int(v) for v in v1.split(".")]
64-
lv2 = [int(v) for v in v2.split(".")]
65-
min_length = min(len(lv1), len(lv2))
66-
return lv1[:min_length] < lv2[:min_length]
67-
68-
compiler = str(self.settings.compiler)
69-
version = str(self.settings.compiler.version)
70-
minimum_version = self._compilers_minimum_version.get(compiler, False)
71-
72-
if minimum_version and lazy_lt_semver(version, minimum_version):
73-
raise ConanInvalidConfiguration(
74-
f"{self.name} {self.version} requires C++{self._min_cppstd}, which your compiler ({compiler}-{version}) does not support")
61+
def build_requirements(self):
62+
self.tool_requires("cmake/3.27.1")
7563

7664
def requirements(self):
7765
self.requires("tl-function-ref/1.0.0")
@@ -109,6 +97,8 @@ def package_info(self):
10997
if self._bare_metal:
11098
self.cpp_info.defines = [
11199
"BOOST_LEAF_EMBEDDED",
100+
# TODO(#694): Remove this or have it be configurable. Users
101+
# should not be forced to operate without thread support
112102
"BOOST_LEAF_NO_THREADS"
113103
]
114104

demos/libhal.tweaks.hpp

Lines changed: 0 additions & 24 deletions
This file was deleted.

include/libhal/third_party/inplace_function.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
* of the called function will NOT be valid.
3636
* 4. Move SG14_INPLACE_FUNCTION_THROW is no longer needed and thus
3737
* this library no longer needs exceptions.
38+
* 5. After removing the `nullptr_t` constructors, there is a new edge case
39+
* where the vtable_ptr_ is now nullptr and not an empty_vtable, which
40+
* means that access to that vtable_ptr_ results in a null pointer
41+
* dereference. A check must be put in the copy ctor and dtor.
3842
*/
3943

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

321325
inplace_function& operator=(inplace_function other) noexcept
322326
{
323-
vtable_ptr_->destructor_ptr(std::addressof(storage_));
327+
if (vtable_ptr_) {
328+
vtable_ptr_->destructor_ptr(std::addressof(storage_));
329+
}
324330

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

333339
~inplace_function()
334340
{
335-
vtable_ptr_->destructor_ptr(std::addressof(storage_));
341+
if (vtable_ptr_) {
342+
vtable_ptr_->destructor_ptr(std::addressof(storage_));
343+
}
336344
}
337345

338346
R operator()(Args... args) const

test_package/conanfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class TestPackageConan(ConanFile):
2424
settings = "os", "arch", "compiler", "build_type"
2525
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
2626

27+
def build_requirements(self):
28+
self.tool_requires("cmake/3.27.1")
29+
2730
def requirements(self):
2831
self.requires(self.tested_reference_str)
2932

test_package/libhal.tweaks.hpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)