From da2341ec12128d3b4983a67d39aeaf76b1781fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 20 Oct 2024 12:02:20 +0000 Subject: Add printf like behaviour to assert --- .../include/arch/exception_handling/assert.hpp | 52 +++++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/exception_handling/assert.hpp b/arch/x86_64/include/arch/exception_handling/assert.hpp index f355a61..152e653 100644 --- a/arch/x86_64/include/arch/exception_handling/assert.hpp +++ b/arch/x86_64/include/arch/exception_handling/assert.hpp @@ -1,16 +1,56 @@ #ifndef TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_ASSERT_HPP #define TEACHOS_ARCH_X86_64_EXCEPTION_HANDLING_ASSERT_HPP +#include "arch/exception_handling/panic.hpp" + +#include + namespace teachos::arch::exception_handling { + namespace + { + char constexpr FAILED_MESSAGE[] = "Invalid arguments passed to format specifiers (%) in exception_handling::assert"; + } + /** - * @brief assert a condition to be true, if not do not continue - * execution of the code and print message to screen + * @brief Assert a condition to be true, if not do not continue + * execution of the code and print the formatted message to screen. * - * @param condition - * @param message + * @param condition Condition we want to be true. + * @param format Formatting message that the given arguments will be inserted into. + * @param ...args Arguments that will be formatted and inserted into the resulting string, replacing their respective + * specifiers. Uses the printf specifiers see https://cplusplus.com/reference/cstdio/printf/ for more information. */ - auto assert(bool condition, char const * message) -> void; + template + auto assert(bool condition, char const * format, Args const &... args) -> void + { + if (condition) + { + return; + } + else if constexpr (sizeof...(args) == 0) + { + panic("Assertion Violation: ", format); + } + + // Result is what would have been written if the passed buffer would have been large enough not counting null + // character, or if an error occured while creating the string a negative number is returned instead. To ensure this + // will not crash the system when creating an array with negative size we assert beforehand with a clear error + // message. + int const size = snprintf(nullptr, 0U, format, args...) + 1U; + if (size < 0) + { + panic("Assertion Violation: ", FAILED_MESSAGE); + } +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wvla" + char arguments[size] = {}; +#pragma GCC diagnostic pop + int const written_characters = snprintf(arguments, size, format, args...); + // Written characters is expected to be one less, because of the null termination character. + bool const result = (written_characters == (size - 1)); + panic("Assertion Violation: ", result ? arguments : FAILED_MESSAGE); + } } // namespace teachos::arch::exception_handling -#endif \ No newline at end of file +#endif -- cgit v1.2.3