From: Martin Liska Date: Thu, 23 Jun 2022 12:43:50 +0000 (+0200) Subject: c++: properly initialize UBSAN built-ins X-Git-Tag: basepoints/gcc-14~5990 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8a15cd3396aa08dc2633982481fe392af0aa9e78;p=thirdparty%2Fgcc.git c++: properly initialize UBSAN built-ins PR c++/106062 gcc/ChangeLog: * ubsan.cc (sanitize_unreachable_fn): Change order of calls in order to initialize UBSAN built-ins. gcc/testsuite/ChangeLog: * gfortran.dg/ubsan/pr106062.f90: New test. --- diff --git a/gcc/testsuite/gfortran.dg/ubsan/pr106062.f90 b/gcc/testsuite/gfortran.dg/ubsan/pr106062.f90 new file mode 100644 index 000000000000..879a67eb7494 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/ubsan/pr106062.f90 @@ -0,0 +1,11 @@ +! PR c++/106062 +! { dg-do compile } +! { dg-options "-O2 -fsanitize=undefined" } + +call test (reshape ((/ 'a', 'b', 'c', 'd' /), (/ 2, 2 /))) +contains + subroutine test (a) + character (*), dimension (:, :) :: a + if (len (a) .ne. 1) STOP + end +end diff --git a/gcc/ubsan.cc b/gcc/ubsan.cc index f15026872a13..b6c4301dfaf0 100644 --- a/gcc/ubsan.cc +++ b/gcc/ubsan.cc @@ -654,9 +654,10 @@ sanitize_unreachable_fn (tree *data, location_t loc) } else if (san) { - fn = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE); + /* Call ubsan_create_data first as it initializes SANITIZER built-ins. */ *data = ubsan_create_data ("__ubsan_unreachable_data", 1, &loc, NULL_TREE, NULL_TREE); + fn = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE); *data = build_fold_addr_expr_loc (loc, *data); } else