]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: diagnostics: Fix mismatch between new[] and free
authorArthur Cohen <arthur.cohen@embecosm.com>
Thu, 3 Aug 2023 14:40:07 +0000 (16:40 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:00:31 +0000 (19:00 +0100)
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

gcc/rust/rust-diagnostics.cc

index 6b99ab5f6c9594bb3d6e00ef23b63c552eeab6d4..683fc531140593a873bdd91eb1e6919b3cce9c47 100644 (file)
@@ -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;
   }