Skip to content

Commit 776da26

Browse files
authored
Remove std::string::data() non-const usage from public headers (microsoft#25943)
### Description <!-- Describe your changes. --> Some compilers we use in our pipeline do not support string::data() nonconst
1 parent 5bd1ff0 commit 776da26

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

include/onnxruntime/core/session/onnxruntime_cxx_inline.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,8 +2593,10 @@ struct GetValueImpl<std::string> {
25932593
std::string result;
25942594
if (size > 0) {
25952595
result.resize(size);
2596+
// some compilers in use do not support std::string::data() non-const
2597+
auto* buffer = &result[0];
25962598
status = Status{GetApi().ReadOpAttr(
2597-
attr, OrtOpAttrType::ORT_OP_ATTR_STRING, result.data(), size, &size)};
2599+
attr, OrtOpAttrType::ORT_OP_ATTR_STRING, buffer, size, &size)};
25982600
if (!status.IsOK()) return status;
25992601
}
26002602
out.swap(result);
@@ -2604,16 +2606,15 @@ struct GetValueImpl<std::string> {
26042606
auto status = CheckAttrType(attr, OrtOpAttrType::ORT_OP_ATTR_STRINGS);
26052607
if (!status.IsOK()) return status;
26062608

2607-
size_t total_buffer_size = GetDataSize(attr, OrtOpAttrType::ORT_OP_ATTR_STRINGS);
2608-
2609-
// Create a temporary buffer to hold the string data
2610-
std::vector<char> buffer(total_buffer_size);
2611-
status = Status{GetApi().ReadOpAttr(attr, OrtOpAttrType::ORT_OP_ATTR_STRINGS, buffer.data(),
2612-
total_buffer_size, &total_buffer_size)};
2613-
if (!status.IsOK()) return status;
2614-
26152609
std::vector<std::string> result;
2610+
size_t total_buffer_size = GetDataSize(attr, OrtOpAttrType::ORT_OP_ATTR_STRINGS);
26162611
if (total_buffer_size > 0) {
2612+
// Create a temporary buffer to hold the string data
2613+
std::vector<char> buffer(total_buffer_size);
2614+
status = Status{GetApi().ReadOpAttr(attr, OrtOpAttrType::ORT_OP_ATTR_STRINGS, buffer.data(),
2615+
total_buffer_size, &total_buffer_size)};
2616+
if (!status.IsOK()) return status;
2617+
26172618
const char* data = buffer.data();
26182619
const char* end = data + total_buffer_size;
26192620
while (data < end) {

0 commit comments

Comments
 (0)