diff --git a/velox/common/process/TraceContext.cpp b/velox/common/process/TraceContext.cpp index b0ee5b724097..78eb5b6dc8f7 100644 --- a/velox/common/process/TraceContext.cpp +++ b/velox/common/process/TraceContext.cpp @@ -18,6 +18,7 @@ #include "velox/common/process/TraceHistory.h" +#include #include namespace facebook::velox::process { @@ -41,7 +42,16 @@ TraceContext::TraceContext(std::string label, bool isTemporary) entry.time = enterTime_; entry.file = __FILE__; entry.line = __LINE__; - snprintf(entry.label, entry.kLabelCapacity, "%s", label_.c_str()); + int result = snprintf(entry.label, entry.kLabelCapacity, "%s", label_.c_str()); + + if (result < 0) { + throw std::runtime_error("Encoding error in snprintf"); + } + + if (result >= entry.kLabelCapacity) { + std::cerr << "Warning: label truncated in snprintf" << std::endl; + entry.label[entry.kLabelCapacity - 1] = '\0'; + } }); traceData_->withValue([&](auto& counts) { auto& data = counts[label_];