-
Notifications
You must be signed in to change notification settings - Fork 78
Handle cases where passing an overly long size could cause UB due to integer overflow #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ documentation. | |
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <limits> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
|
|
@@ -139,6 +140,20 @@ class pbf_reader { | |
| T{m_data, m_data}}; | ||
| } | ||
|
|
||
| // Adds size to ptr safely, and returning ptr if overflow would occur. | ||
kkaefer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const char* safe_ptr_add(const char* ptr, size_t length) { | ||
| #if defined __has_builtin | ||
| #if __has_builtin(__builtin_add_overflow) | ||
| uintptr_t result; | ||
| return __builtin_add_overflow(reinterpret_cast<uintptr_t>(ptr), length, &result) ? ptr : reinterpret_cast<const char*>(result); | ||
| #endif | ||
| #endif | ||
| if (length > std::numeric_limits<uintptr_t>::max() - reinterpret_cast<uintptr_t>(ptr)) { | ||
| return ptr; | ||
| } | ||
| return ptr + length; | ||
| } | ||
|
Comment on lines
+143
to
+155
|
||
|
|
||
| public: | ||
|
|
||
| /** | ||
|
|
@@ -153,7 +168,7 @@ class pbf_reader { | |
| */ | ||
| explicit pbf_reader(const data_view& view) noexcept | ||
| : m_data{view.data()}, | ||
| m_end{view.data() + view.size()} { | ||
| m_end{safe_ptr_add(view.data(), view.size())} { | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -168,7 +183,7 @@ class pbf_reader { | |
| */ | ||
| pbf_reader(const char* data, std::size_t size) noexcept | ||
| : m_data{data}, | ||
| m_end{data + size} { | ||
| m_end{safe_ptr_add(data, size)} { | ||
| } | ||
|
|
||
| #ifndef PROTOZERO_STRICT_API | ||
|
|
@@ -185,7 +200,7 @@ class pbf_reader { | |
| */ | ||
| explicit pbf_reader(const std::pair<const char*, std::size_t>& data) noexcept | ||
| : m_data{data.first}, | ||
| m_end{data.first + data.second} { | ||
| m_end{safe_ptr_add(data.first, data.second)} { | ||
| } | ||
| #endif | ||
|
|
||
|
|
@@ -201,7 +216,7 @@ class pbf_reader { | |
| */ | ||
| explicit pbf_reader(const std::string& data) noexcept | ||
| : m_data{data.data()}, | ||
| m_end{data.data() + data.size()} { | ||
| m_end{safe_ptr_add(data.data(), data.size())} { | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -102,7 +102,7 @@ if(PROTOBUF_FOUND) | |||||
| target_link_libraries(writer_tests PRIVATE protobuf::libprotobuf-lite) | ||||||
|
|
||||||
| if(NOT MSVC) | ||||||
| set_target_properties(writer_tests PROPERTIES COMPILE_FLAGS "-pthread") | ||||||
| set_target_properties(writer_tests PROPERTIES COMPILE_FLAGS "-pthread -Wno-nullability-extension") | ||||||
|
||||||
| set_target_properties(writer_tests PROPERTIES COMPILE_FLAGS "-pthread -Wno-nullability-extension") | |
| set_target_properties(writer_tests PROPERTIES COMPILE_FLAGS "$<$<CXX_COMPILER_ID:Clang>:-pthread -Wno-nullability-extension>$<$<NOT:$<CXX_COMPILER_ID:Clang>>:-pthread>") |
Uh oh!
There was an error while loading. Please reload this page.