Skip to content

Commit 26baea9

Browse files
committed
chore: add gaurd for zero denominator
1 parent 01cacf5 commit 26baea9

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

libc/src/__support/fixed_point/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_header_library(
1616
.fx_rep
1717
libc.include.llvm-libc-macros.stdfix_macros
1818
libc.src.__support.macros.attributes
19+
libc.src.__support.macros.null_check
1920
libc.src.__support.macros.optimization
2021
libc.src.__support.CPP.type_traits
2122
libc.src.__support.CPP.bit

libc/src/__support/fixed_point/fx_bits.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "src/__support/CPP/type_traits.h"
1616
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1717
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
18+
#include "src/__support/macros/null_check.h" // LIBC_CRASH_ON_VALUE
1819
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
1920
#include "src/__support/math_extras.h"
2021

@@ -209,11 +210,9 @@ idiv(T x, T y) {
209210
using FXRep = FXRep<T>;
210211
using CompType = typename FXRep::CompType;
211212

212-
if (y == FXRep::ZERO()) {
213-
// If the value of the second operand of the / operator is zero, the
214-
// behavior is undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16
215-
return static_cast<XType>(0);
216-
}
213+
// If the value of the second operand of the / operator is zero, the
214+
// behavior is undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16
215+
LIBC_CRASH_ON_VALUE(y, FXRep::ZERO());
217216

218217
CompType x_comp = static_cast<CompType>(FXBits(x).get_bits());
219218
CompType y_comp = static_cast<CompType>(FXBits(y).get_bits());

libc/src/__support/macros/null_check.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@
1919
if (LIBC_UNLIKELY((ptr) == nullptr)) \
2020
__builtin_trap(); \
2121
} while (0)
22+
#define LIBC_CRASH_ON_VALUE(var, value) \
23+
do { \
24+
if (LIBC_UNLIKELY((var) == (value))) \
25+
__builtin_trap(); \
26+
} while (0)
27+
2228
#else
2329
#define LIBC_CRASH_ON_NULLPTR(ptr) \
2430
do { \
2531
} while (0)
32+
#define LIBC_CRASH_ON_VALUE(var, value) \
33+
do { \
34+
} while (0)
2635
#endif
2736

2837
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_NULL_CHECK_H

0 commit comments

Comments
 (0)