From 4ce518956d5581b2d61d16ac726b80959f9f07cc Mon Sep 17 00:00:00 2001 From: Carlos Nihelton Date: Sun, 5 Feb 2023 13:54:04 -0300 Subject: [PATCH 1/2] (fix) GCC array bounds checking complaint GCC 12.2 is not convinced that dir will never be empty. Since that's an exceptional situation that seems almost impossible to happen, I'm throwing an exception here. Fixes https://github.com/foonathan/cppast/issues/160 --- src/libclang/libclang_parser.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libclang/libclang_parser.cpp b/src/libclang/libclang_parser.cpp index 04a161cd..b669b074 100644 --- a/src/libclang/libclang_parser.cpp +++ b/src/libclang/libclang_parser.cpp @@ -120,6 +120,9 @@ std::string get_full_path(const detail::cxstring& dir, const std::string& file) if (is_absolute(file)) // absolute file return file; + if (dir.empty()) + // should never happen, but GCC 12.2 is not convinced. That's what exceptions are for, isn't it? + throw std::invalid_argument("Either file is absolute or relative to some named dir"); else if (dir[dir.length() - 1] != '/' && dir[dir.length() - 1] != '\\') // relative needing separator return dir.std_str() + '/' + file; From 79bed01f0de3031d379498cd488f7a4db74d9530 Mon Sep 17 00:00:00 2001 From: Carlos Nihelton Date: Sun, 5 Feb 2023 21:03:16 -0300 Subject: [PATCH 2/2] Assertion instead of exception A pragma was necessary, though. GCC up to 12.2 cannot understand the macro DEBUG_ASSERT would have stopped the execution before the suspected line. --- src/libclang/libclang_parser.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libclang/libclang_parser.cpp b/src/libclang/libclang_parser.cpp index b669b074..14cb6cb5 100644 --- a/src/libclang/libclang_parser.cpp +++ b/src/libclang/libclang_parser.cpp @@ -117,13 +117,16 @@ bool is_absolute(const std::string& file) std::string get_full_path(const detail::cxstring& dir, const std::string& file) { + DEBUG_ASSERT(!dir.empty(), detail::precondition_error_handler{}, + "Either file is absolute or relative to some named dir"); if (is_absolute(file)) // absolute file return file; - if (dir.empty()) - // should never happen, but GCC 12.2 is not convinced. That's what exceptions are for, isn't it? - throw std::invalid_argument("Either file is absolute or relative to some named dir"); - else if (dir[dir.length() - 1] != '/' && dir[dir.length() - 1] != '\\') +// GCC fails to understand that the assertion would avoid reaching this line if dir was empty. +#if defined __GNUC__ +# pragma GCC diagnostic ignored "-Warray-bounds" +#endif + if (dir[dir.length() - 1] != '/' && dir[dir.length() - 1] != '\\') // relative needing separator return dir.std_str() + '/' + file; else