summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAhmad Fatoum <ahmad@a3f.at>2019-09-20 09:58:12 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2019-10-14 12:52:11 +0200
commit55397b9ebe3a21a3aeb6a98131c0991bff0f7123 (patch)
tree00857db3076772b6c9d907e53be4fdf94286b377 /lib
parent95a4e202f84995718ce4a689dd60972bc416c133 (diff)
downloadbarebox-55397b9ebe3a21a3aeb6a98131c0991bff0f7123.tar.gz
barebox-55397b9ebe3a21a3aeb6a98131c0991bff0f7123.tar.xz
common: ubsan: ignore shifting one into sign bit
The __ubsan_handle_shift_out_of_bounds handler would be called for code shifting a one into the sign bit like (1 << 31), which is all too common in barebox. It's technically UB, but it's so prevalent that it's highly unlikely to be treated by a compiler as anything else than the standard-compliant (1U << 31). Check for this case here and ignore it selectively. Signed-off-by: Ahmad Fatoum <ahmad@a3f.at> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/ubsan.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/ubsan.c b/lib/ubsan.c
index 89ca6e580b..41a5731dda 100644
--- a/lib/ubsan.c
+++ b/lib/ubsan.c
@@ -382,6 +382,26 @@ void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
if (suppress_report(&data->location))
return;
+ /* This handler would be called for code shifting a one into the
+ * sign bit like (1 << 31), which is all too common in barebox.
+ * It's technically UB, but it's so prevalent that it's highly
+ * unlikely to be treated by a compiler as anything else than the
+ * standard-compliant (1U << 31). Thus check for this case here
+ * and ignore it selectively
+ */
+ if (type_is_signed(lhs_type)) {
+ s_max lhs_int, rhs_int;
+
+ lhs_int = get_signed_val(lhs_type, lhs);
+ rhs_int = get_signed_val(rhs_type, rhs);
+
+ if (fls(lhs_int) + rhs_int == type_bit_width(lhs_type)) {
+ pr_debug("signed left shift of %lld by %lld ignored.\n",
+ (s64)lhs_int, (s64)rhs_int);
+ return;
+ }
+ }
+
ubsan_prologue(&data->location, &flags);
val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);