From 8e20794ba788b077ae46cb1329e4190f55c6faea Mon Sep 17 00:00:00 2001 From: nashit hayyat Date: Thu, 23 Jul 2026 13:46:38 +0530 Subject: [PATCH] cap trace value memcpy at scalar value size --- util/HalideTraceUtils.h | 6 ++++-- util/HalideTraceViz.cpp | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/util/HalideTraceUtils.h b/util/HalideTraceUtils.h index b06335d1350d..ba181574985b 100644 --- a/util/HalideTraceUtils.h +++ b/util/HalideTraceUtils.h @@ -2,6 +2,7 @@ #define HALIDE_TRACE_UTILS_H #include "HalideRuntime.h" +#include #include #include @@ -78,8 +79,9 @@ struct Packet : public halide_trace_packet_t { // so that value_as<>() won't complain under sanitizers. halide_scalar_value_t aligned_value; // Only copy the number of bytes in the type: the stream isn't guaranteed - // to be padded to sizeof(halide_scalar_value_t). - memcpy(&aligned_value, val, type().bits / 8); + // to be padded to sizeof(halide_scalar_value_t). type().bits comes from + // the stream, so cap the copy so a bogus type can't overrun aligned_value. + memcpy(&aligned_value, val, std::min(type().bits / 8, sizeof(aligned_value))); return value_as(type(), aligned_value); } diff --git a/util/HalideTraceViz.cpp b/util/HalideTraceViz.cpp index 19edebaa7d6b..ac6ffa136a14 100644 --- a/util/HalideTraceViz.cpp +++ b/util/HalideTraceViz.cpp @@ -144,8 +144,9 @@ T get_value_as(const halide_trace_packet_t &p, int idx) { // so that value_as<>() won't complain under sanitizers. halide_scalar_value_t aligned_value; // Only copy the number of bytes in the type: the stream isn't guaranteed - // to be padded to sizeof(halide_scalar_value_t). - memcpy(&aligned_value, val, type.bits / 8); + // to be padded to sizeof(halide_scalar_value_t). type.bits comes from the + // stream, so cap the copy so a bogus type can't overrun aligned_value. + memcpy(&aligned_value, val, std::min(type.bits / 8, sizeof(aligned_value))); return value_as(type, aligned_value); }