From 321c1e5730abfab0b5d321cbad75fdef68475e70 Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Sat, 27 Apr 2024 13:41:38 +1000 Subject: [PATCH] Use flatbuffers::String::str instead of c_str. (#20487) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Description flatbuffers::String::c_str returns a pointer that may not be null terminated. This causes a warning when building on an A100 with gcc 11. Not clear why other builds with gcc 11 (e.g. Ubuntu 22.04 WSL) don't generate a warning. Either way it's safer to use str() as that constructs a std::string with data() and size(). Unclear if this is an issue in reality as it's reading from the flatbuffer and most likely didn't write out an empty string in order to save space. There's no perf need to use c_str instead of str, and in LOAD_STR_FROM_ORT_FORMAT we need to convert the return value to a std::string anyway. ```c++ struct String : public Vector { const char *c_str() const { return reinterpret_cast(Data()); } std::string str() const { return std::string(c_str(), size()); } ``` ``` inlined from ‘onnxruntime::common::Status onnxruntime::fbs::utils::LoadAttributeOrtFormat(const onnxruntime::fbs::Attribute&, onnx::AttributeProto&, std::unique_ptr&, onnxruntime::Graph&, onnxruntime::Node&, const onnxruntime::OrtFormatLoadOptions&, const onnxruntime::logging::Logger&)’ at /frdong_data/onnxruntime/onnxruntime/core/graph/graph_flatbuffers_utils.cc:385:3: /usr/include/c++/11/bits/char_traits.h:399:32: error: ‘long unsigned int __builtin_strlen(const char*)’ reading 1 or more bytes from a region of size 0 [-Werror=stringop-overread] ``` ### Motivation and Context Fix build error on A100 --- onnxruntime/core/flatbuffers/flatbuffers_utils.cc | 5 +++-- onnxruntime/core/flatbuffers/flatbuffers_utils.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/flatbuffers/flatbuffers_utils.cc b/onnxruntime/core/flatbuffers/flatbuffers_utils.cc index 06b5a7cceb9f2..1eb3bbdb1237f 100644 --- a/onnxruntime/core/flatbuffers/flatbuffers_utils.cc +++ b/onnxruntime/core/flatbuffers/flatbuffers_utils.cc @@ -163,8 +163,9 @@ Status SaveValueInfoOrtFormat(flatbuffers::FlatBufferBuilder& builder, #endif // #if !defined(ORT_MINIMAL_BUILD) void LoadStringFromOrtFormat(std::string& dst, const flatbuffers::String* fbs_string) { - if (fbs_string) - dst = fbs_string->c_str(); + if (fbs_string) { + dst = fbs_string->str(); + } } static Status LoadTypeInfoOrtFormat(const fbs::TypeInfo& fbs_type_info, diff --git a/onnxruntime/core/flatbuffers/flatbuffers_utils.h b/onnxruntime/core/flatbuffers/flatbuffers_utils.h index 76860d6ab1db8..aed0c201a2dd5 100644 --- a/onnxruntime/core/flatbuffers/flatbuffers_utils.h +++ b/onnxruntime/core/flatbuffers/flatbuffers_utils.h @@ -40,7 +40,7 @@ void LoadStringFromOrtFormat(std::string& dst, const flatbuffers::String* fbs_st #define LOAD_STR_FROM_ORT_FORMAT(protobuf_msg, str_field, fbs_string) \ { \ if (fbs_string) \ - protobuf_msg.set_##str_field(fbs_string->c_str()); \ + protobuf_msg.set_##str_field(fbs_string->str()); \ } onnxruntime::common::Status LoadValueInfoOrtFormat(