@@ -52,35 +52,58 @@ static FAR const char *const g_type_check_kinds[] =
5252 "downcast of"
5353};
5454
55+ /****************************************************************************
56+ * Private Data
57+ ****************************************************************************/
58+
59+ static bool g_ubsan_reporting;
60+
5561/****************************************************************************
5662 * Private Functions
5763 ****************************************************************************/
5864
59- static void ubsan_prologue(FAR struct source_location *loc,
65+ static bool ubsan_prologue(FAR struct source_location *loc,
6066 FAR const char *reason)
6167{
68+ /* Printing a report can trip another check; never report recursively. */
69+
70+ if (g_ubsan_reporting)
71+ {
72+ return false;
73+ }
74+
75+ g_ubsan_reporting = true;
6276 _alert("========================================"
6377 "========================================\n");
6478 _alert("UBSAN: %s in %s:%" PRIu32 ":%" PRIu32"\n",
6579 reason, loc->file_name, loc->line, loc->column);
80+ return true;
6681}
6782
6883static void ubsan_epilogue(void)
6984{
7085 _alert("========================================"
7186 "========================================\n");
87+ g_ubsan_reporting = false;
7288}
7389
7490static void ubsan_prologue_epilogue(FAR struct source_location *loc,
7591 FAR const char *reason)
7692{
77- ubsan_prologue(loc, reason);
93+ if (!ubsan_prologue(loc, reason))
94+ {
95+ return;
96+ }
97+
7898 ubsan_epilogue();
7999}
80100
81101static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
82102{
83- ubsan_prologue(data->location, "null-pointer-dereference");
103+ if (!ubsan_prologue(data->location, "null-pointer-dereference"))
104+ {
105+ return;
106+ }
84107
85108 _alert("%s null pointer of type %s\n",
86109 g_type_check_kinds[data->type_check_kind], data->type->type_name);
@@ -91,7 +114,10 @@ static void handle_null_ptr_deref(FAR struct type_mismatch_data_common *data)
91114static void handle_misaligned_access(
92115 FAR struct type_mismatch_data_common *data, uintptr_t ptr)
93116{
94- ubsan_prologue(data->location, "misaligned-access");
117+ if (!ubsan_prologue(data->location, "misaligned-access"))
118+ {
119+ return;
120+ }
95121
96122 _alert("%s misaligned address %p for type %s\n",
97123 g_type_check_kinds[data->type_check_kind], (FAR void *)ptr,
@@ -104,7 +130,10 @@ static void handle_misaligned_access(
104130static void handle_object_size_mismatch(
105131 FAR struct type_mismatch_data_common *data, uintptr_t ptr)
106132{
107- ubsan_prologue(data->location, "object-size-mismatch");
133+ if (!ubsan_prologue(data->location, "object-size-mismatch"))
134+ {
135+ return;
136+ }
108137
109138 _alert("%s address %p with insufficient space\n",
110139 g_type_check_kinds[data->type_check_kind], (FAR void *)ptr);
@@ -211,7 +240,10 @@ void __ubsan_handle_out_of_bounds(FAR void *data, FAR void *index)
211240 FAR struct out_of_bounds_data *info = data;
212241 char index_str[40];
213242
214- ubsan_prologue(&info->location, "array-index-out-of-bounds");
243+ if (!ubsan_prologue(&info->location, "array-index-out-of-bounds"))
244+ {
245+ return;
246+ }
215247
216248 val_to_string(index_str, sizeof(index_str), info->index_type, index);
217249 _alert("index %s is out of range for type %s\n",
@@ -228,7 +260,10 @@ void __ubsan_handle_shift_out_of_bounds(FAR void *data,
228260 char rhs_str[40];
229261 char lhs_str[40];
230262
231- ubsan_prologue(&info->location, "shift-out-of-bounds");
263+ if (!ubsan_prologue(&info->location, "shift-out-of-bounds"))
264+ {
265+ return;
266+ }
232267
233268 val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
234269 val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
@@ -262,7 +297,10 @@ void __ubsan_handle_divrem_overflow(FAR void *data,
262297 FAR struct overflow_data *info = data;
263298 char rhs_val_str[40];
264299
265- ubsan_prologue(&info->location, "division-overflow");
300+ if (!ubsan_prologue(&info->location, "division-overflow"))
301+ {
302+ return;
303+ }
266304
267305 val_to_string(rhs_val_str, sizeof(rhs_val_str), info->type, rhs);
268306
@@ -275,6 +313,8 @@ void __ubsan_handle_divrem_overflow(FAR void *data,
275313 {
276314 _alert("division by zero\n");
277315 }
316+
317+ ubsan_epilogue();
278318}
279319
280320void __ubsan_handle_alignment_assumption(FAR void *data, uintptr_t ptr,
@@ -283,7 +323,10 @@ void __ubsan_handle_alignment_assumption(FAR void *data, uintptr_t ptr,
283323 FAR struct alignment_assumption_data *info = data;
284324 uintptr_t real_ptr;
285325
286- ubsan_prologue(&info->location, "alignment-assumption");
326+ if (!ubsan_prologue(&info->location, "alignment-assumption"))
327+ {
328+ return;
329+ }
287330
288331 if (offset)
289332 {
0 commit comments