-
-
Notifications
You must be signed in to change notification settings - Fork 220
Add protection against integer overflows in sugar functions #1457
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
Draft
Enchufa2
wants to merge
8
commits into
master
Choose a base branch
from
safe_math
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+338
−146
Draft
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
38178a8
implementation of safe add/sub/mul for integers
Enchufa2 7bcb01b
fix duplicated define
Enchufa2 f7420e4
fix couple of tests, explicitly check for overflow errors
Enchufa2 3adff65
fix functions/diff.h
Enchufa2 5232eea
fix functions/cumprod.h
Enchufa2 3835da4
fix functions/cumsum.h
Enchufa2 98734f9
move functions further down into the detail namespace
Enchufa2 0e79600
fix functions/rowSums.h
Enchufa2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- | ||
| // | ||
| // safe_math.h: Rcpp R/C++ interface class library -- | ||
| // | ||
| // Copyright (C) 2026 Iñaki Ucar | ||
| // | ||
| // This file is part of Rcpp. | ||
| // | ||
| // Rcpp is free software: you can redistribute it and/or modify it | ||
| // under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 2 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Rcpp is distributed in the hope that it will be useful, but | ||
| // WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| #ifndef Rcpp__sugar__tools_safe_math_h | ||
| #define Rcpp__sugar__tools_safe_math_h | ||
|
|
||
| #ifndef safe_math__has_builtin | ||
| # ifdef __has_builtin | ||
| # define safe_math__has_builtin(x) __has_builtin(x) | ||
| # else | ||
| # define safe_math__has_builtin(x) 0 | ||
| # endif | ||
| #endif | ||
|
|
||
| #define RCPP_SAFE_ADD(a, b) Rcpp::sugar::safe_add(a, b, __func__) | ||
| #define RCPP_SAFE_SUB(a, b) Rcpp::sugar::safe_sub(a, b, __func__) | ||
| #define RCPP_SAFE_MUL(a, b) Rcpp::sugar::safe_mul(a, b, __func__) | ||
|
|
||
| namespace Rcpp { | ||
| namespace sugar { | ||
|
|
||
| inline void stop_overflow(const char* caller) { | ||
| if (caller) | ||
| Rcpp::stop("[%s] Integer overflow!", caller); | ||
| Rcpp::stop("Integer overflow!"); | ||
| } | ||
|
|
||
| // Addition | ||
| template <typename T> | ||
| inline typename std::enable_if<std::is_integral<T>::value, T>::type | ||
| safe_add(T a, T b, const char* caller = nullptr) { | ||
| #if safe_math__has_builtin(__builtin_add_overflow) | ||
| T result; | ||
| if (__builtin_add_overflow(a, b, &result)) | ||
| stop_overflow(caller); | ||
| return result; | ||
| #else // fallback | ||
| if (std::is_signed<T>::value) { | ||
| if ((b > 0 && a > std::numeric_limits<T>::max() - b) || | ||
| (b < 0 && a < std::numeric_limits<T>::min() - b)) | ||
| stop_overflow(caller); | ||
| } else { | ||
| if (a > std::numeric_limits<T>::max() - b) | ||
| stop_overflow(caller); | ||
| } | ||
| return a + b; | ||
| #endif | ||
| } | ||
|
|
||
| template <typename T> | ||
| inline typename std::enable_if<!std::is_integral<T>::value, T>::type | ||
| safe_add(T a, T b, const char* caller = nullptr) { return a + b; } | ||
|
|
||
| // Subtraction | ||
| template <typename T> | ||
| inline typename std::enable_if<std::is_integral<T>::value, T>::type | ||
| safe_sub(T a, T b, const char* caller = nullptr) { | ||
| #if safe_math__has_builtin(__builtin_sub_overflow) | ||
| T result; | ||
| if (__builtin_sub_overflow(a, b, &result)) | ||
| stop_overflow(caller); | ||
| return result; | ||
| #else // fallback | ||
| if (std::is_signed<T>::value) { | ||
| if ((b < 0 && a > std::numeric_limits<T>::max() + b) || | ||
| (b > 0 && a < std::numeric_limits<T>::min() + b)) | ||
| stop_overflow(caller); | ||
| } else { | ||
| if (a < b) | ||
| stop_overflow(caller); | ||
| } | ||
| return a - b; | ||
| #endif | ||
| } | ||
|
|
||
| template <typename T> | ||
| inline typename std::enable_if<!std::is_integral<T>::value, T>::type | ||
| safe_sub(T a, T b, const char* caller = nullptr) { return a - b; } | ||
|
|
||
| // Multiplication | ||
| template <typename T> | ||
| inline typename std::enable_if<std::is_integral<T>::value, T>::type | ||
| safe_mul(T a, T b, const char* caller = nullptr) { | ||
| #if safe_math__has_builtin(__builtin_mul_overflow) | ||
| T result; | ||
| if (__builtin_mul_overflow(a, b, &result)) | ||
| stop_overflow(caller); | ||
| return result; | ||
| #else // fallback | ||
| if (a == 0 || b == 0) return 0; | ||
| if (std::is_signed<T>::value) { | ||
| if ((a > 0 && b > 0 && a > std::numeric_limits<T>::max() / b) || | ||
| (a > 0 && b < 0 && b < std::numeric_limits<T>::min() / a) || | ||
| (a < 0 && b > 0 && a < std::numeric_limits<T>::min() / b) || | ||
| (a < 0 && b < 0 && a < std::numeric_limits<T>::max() / b)) | ||
| stop_overflow(caller); | ||
| } else { | ||
| if (b > 0 && a > std::numeric_limits<T>::max() / b) | ||
| stop_overflow(caller); | ||
| } | ||
| return a * b; | ||
| #endif | ||
| } | ||
|
|
||
| template <typename T> | ||
| inline typename std::enable_if<!std::is_integral<T>::value, T>::type | ||
| safe_mul(T a, T b, const char* caller = nullptr) { return a * b; } | ||
|
|
||
| } // namespace sugar | ||
| } // namespace Rcpp | ||
|
|
||
| #undef safe_math__has_builtin | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
|
|
||
| // sugar_safe_math.cpp: Rcpp R/C++ interface class library -- safe math unit tests | ||
| // | ||
| // Copyright (C) 2026 Iñaki Ucar | ||
| // | ||
| // This file is part of Rcpp. | ||
| // | ||
| // Rcpp is free software: you can redistribute it and/or modify it | ||
| // under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 2 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Rcpp is distributed in the hope that it will be useful, but | ||
| // WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| #include <Rcpp.h> | ||
|
|
||
| // [[Rcpp::export]] | ||
| int safe_add(int a, int b){ | ||
| return RCPP_SAFE_ADD(a, b); | ||
| } | ||
|
|
||
| // [[Rcpp::export]] | ||
| int safe_sub(int a, int b){ | ||
| return RCPP_SAFE_SUB(a, b); | ||
| } | ||
|
|
||
| // [[Rcpp::export]] | ||
| int safe_mul(int a, int b){ | ||
| return RCPP_SAFE_MUL(a, b); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
|
|
||
| // sugar_safe_math.cpp: Rcpp R/C++ interface class library -- safe math unit tests | ||
| // | ||
| // Copyright (C) 2026 Iñaki Ucar | ||
| // | ||
| // This file is part of Rcpp. | ||
| // | ||
| // Rcpp is free software: you can redistribute it and/or modify it | ||
| // under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 2 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Rcpp is distributed in the hope that it will be useful, but | ||
| // WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| #define define safe_math__has_builtin(x) 0 | ||
| #include <Rcpp.h> | ||
|
|
||
| // [[Rcpp::export]] | ||
| int safe_add_fallback(int a, int b){ | ||
| return RCPP_SAFE_ADD(a, b); | ||
| } | ||
|
|
||
| // [[Rcpp::export]] | ||
| int safe_sub_fallback(int a, int b){ | ||
| return RCPP_SAFE_SUB(a, b); | ||
| } | ||
|
|
||
| // [[Rcpp::export]] | ||
| int safe_mul_fallback(int a, int b){ | ||
| return RCPP_SAFE_MUL(a, b); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.