diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-08-03 16:40:07 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-16 19:00:31 +0100 |
commit | e4515c226d5e97bcc68383426cdafbc054df9ac5 (patch) | |
tree | 2c50520ad24a034d6df7e6452de6ae686aa8b5c6 | |
parent | 06af44ece16072c7078c1fee283108a12c963ecb (diff) | |
download | gcc-e4515c226d5e97bcc68383426cdafbc054df9ac5.zip gcc-e4515c226d5e97bcc68383426cdafbc054df9ac5.tar.gz gcc-e4515c226d5e97bcc68383426cdafbc054df9ac5.tar.bz2 |
gccrs: diagnostics: Fix mismatch between new[] and free
We cannot use `free` on a pointer allocated through `new[]`, and this
causes an ASAN failure. This fixes it by using `xcalloc` instead of
`new[]` when creating description buffers for our error codes.
gcc/rust/ChangeLog:
* rust-diagnostics.cc: Switch from new[] to xcalloc
-rw-r--r-- | gcc/rust/rust-diagnostics.cc | 13 |
1 files changed, 4 insertions, 9 deletions
diff --git a/gcc/rust/rust-diagnostics.cc b/gcc/rust/rust-diagnostics.cc index 6b99ab5..683fc53 100644 --- a/gcc/rust/rust-diagnostics.cc +++ b/gcc/rust/rust-diagnostics.cc @@ -199,6 +199,9 @@ public: void format_error_code (char *buffer) const { + // we can use the `u` format specifier because the `ErrorCode` enum class + // "inherits" from `unsigned int` - add a static assertion to make sure + // that's the case before we do the formatting static_assert ( std::is_same<std::underlying_type<ErrorCode>::type, unsigned int>::value, "invalid format specifier for ErrorCode's underlying type"); @@ -210,18 +213,10 @@ public: char *make_description () const final override { // 'E' + 4 characters + \0 - char *buffer = new char[6]; - - // is that needed. does C++ suck that much that you - // can't zero initialize a new[]'d char array - memset (buffer, 0, 6); + char *buffer = static_cast<char *> (xcalloc (6, sizeof (char))); format_error_code (buffer); - // we can use the `u` format specifier because the `ErrorCode` enum class - // "inherits" from `unsigned int` - add a static assertion to make sure - // that's the case before we do the formatting - return buffer; } |