From 719f54ff5ee321edd11505b015f5cd0aebe9130e Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Fri, 2 Feb 2024 11:17:46 -0500 Subject: [PATCH] Fix overlapping memcpy It's UB to use memcpy with overlapping source and destination. This might be causing crashes on 32 bit platforms and on OpenBSD. Use memmove instead. Add a bounds check while we're at it since it's unclear whether one-past-end pointer with n=0 is UB. --- src/prism.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/prism.c b/src/prism.c index acc7d26b9d9..a13504a97bf 100644 --- a/src/prism.c +++ b/src/prism.c @@ -18061,7 +18061,9 @@ pm_parser_errors_format_sort(const pm_list_t *error_list, const pm_newline_list_ // Now we're going to shift all of the errors after this one down one // index to make room for the new error. - memcpy(&errors[index + 1], &errors[index], sizeof(pm_error_t) * (error_list->size - index - 1)); + if (index + 1 < error_list->size) { + memmove(&errors[index + 1], &errors[index], sizeof(pm_error_t) * (error_list->size - index - 1)); + } // Finally, we'll insert the error into the array. uint32_t column_end;