Skip to content

Commit 74b27ac

Browse files
committed
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
1 parent 87c5cc1 commit 74b27ac

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

gcc/rust/rust-diagnostics.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ class rust_error_code_rule : public diagnostic_metadata::rule
199199

200200
void format_error_code (char *buffer) const
201201
{
202+
// we can use the `u` format specifier because the `ErrorCode` enum class
203+
// "inherits" from `unsigned int` - add a static assertion to make sure
204+
// that's the case before we do the formatting
202205
static_assert (
203206
std::is_same<std::underlying_type<ErrorCode>::type, unsigned int>::value,
204207
"invalid format specifier for ErrorCode's underlying type");
@@ -210,18 +213,10 @@ class rust_error_code_rule : public diagnostic_metadata::rule
210213
char *make_description () const final override
211214
{
212215
// 'E' + 4 characters + \0
213-
char *buffer = new char[6];
214-
215-
// is that needed. does C++ suck that much that you
216-
// can't zero initialize a new[]'d char array
217-
memset (buffer, 0, 6);
216+
char *buffer = static_cast<char *> (xcalloc (6, sizeof (char)));
218217

219218
format_error_code (buffer);
220219

221-
// we can use the `u` format specifier because the `ErrorCode` enum class
222-
// "inherits" from `unsigned int` - add a static assertion to make sure
223-
// that's the case before we do the formatting
224-
225220
return buffer;
226221
}
227222

0 commit comments

Comments
 (0)