Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer silently hide errors in Metal completion handlers (alternative approach) #8240

Merged
Merged
Prev Previous commit
Next Next commit
Add comment noting why we manually null-terminate after strncpy
  • Loading branch information
shoaibkamil committed Jun 10, 2024
commit faaa65e0ddc723b68d38d732854a4cc62c809eff
4 changes: 4 additions & 0 deletions src/runtime/metal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ class MetalContextHolder {
saved_status = halide_error_code_success;
if (error_string != nullptr && result != halide_error_code_success && strnlen(MetalContextHolder::error_string, 1024) > 0) {
strncpy(error_string, MetalContextHolder::error_string, 1024);
// Ensure null-termination, since strncpy won't if the source string is too long
error_string[1023] = '\0';
MetalContextHolder::error_string[0] = '\0';
debug(nullptr) << "MetalContextHolder::get_and_clear_saved_status: " << error_string << "\n";
Expand All @@ -432,6 +433,7 @@ class MetalContextHolder {
int result = saved_status;
if (error_string != nullptr && result != halide_error_code_success && strnlen(MetalContextHolder::error_string, 1024) > 0) {
strncpy(error_string, MetalContextHolder::error_string, 1024);
// Ensure null-termination, since strncpy won't if the source string is too long
error_string[1023] = '\0';
}
halide_mutex_unlock(&saved_status_mutex);
Expand All @@ -443,6 +445,7 @@ class MetalContextHolder {
saved_status = new_status;
if (error_string != nullptr) {
strncpy(MetalContextHolder::error_string, error_string, 1024);
shoaibkamil marked this conversation as resolved.
Show resolved Hide resolved
// Ensure null-termination, since strncpy won't if the source string is too long
error_string[1023] = '\0';
debug(nullptr) << "MetalContextHolder::set_saved_status: " << error_string << "\n";
}
Expand Down Expand Up @@ -509,6 +512,7 @@ WEAK int halide_metal_command_buffer_completion_handler(void *user_context, mtl_
*returned_error_string = (char *)malloc(sizeof(char) * 1024);
if (*returned_error_string != nullptr) {
strncpy(*returned_error_string, error_string, 1024);
// Ensure null-termination, since strncpy won't if the source string is too long
(*returned_error_string)[1023] = '\0';
} else {
debug(user_context) << "halide_metal_command_buffer_completion_handler: Failed to allocate memory for error string.\n";
Expand Down
Loading