Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ static Ort::Status OrtOpAttrToProto(const OrtOpAttr& ort_attr, onnx::AttributePr
ORT_EP_UTILS_C_RETURN_IF_ERROR(ort_api.ReadOpAttr(&ort_attr, attr_type, str->data(), total_attr_bytes,
&total_attr_bytes));

str->resize(total_attr_bytes - 1); // remove extra ending terminating '\0' character.
str->resize(total_attr_bytes);
break;
}
case OrtOpAttrType::ORT_OP_ATTR_STRINGS: {
Expand Down
2 changes: 1 addition & 1 deletion include/onnxruntime/core/session/onnxruntime_cxx_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,7 @@ inline std::string ShapeInferContext::GetAttrString(const char* attr_name) {
if (status) {
std::vector<char> chars(out, '\0');
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_STRING, chars.data(), out, &out));
return {chars.data()};
return std::string{chars.data(), out};
} else {
return {c};
}
Expand Down
9 changes: 3 additions & 6 deletions onnxruntime/core/session/custom_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,14 @@ ORT_API_STATUS_IMPL(OrtApis::ReadOpAttr, _In_ const OrtOpAttr* op_attr, _In_ Ort
}
case OrtOpAttrType::ORT_OP_ATTR_STRING: {
const auto& s = attr->s();
if (len < s.size() + 1) {
if (len < s.size()) {
ret = OrtApis::CreateStatus(OrtErrorCode::ORT_INVALID_ARGUMENT,
"Size of data not large enough to hold the string.");
} else {
char* output_c = reinterpret_cast<char*>(data);
for (char c : s) {
*output_c++ = c;
}
*output_c = '\0';
memcpy(output_c, s.data(), s.size());
}
*out = s.size() + 1;
*out = s.size();
break;
}
case OrtOpAttrType::ORT_OP_ATTR_STRINGS: {
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/core/session/standalone_op_invoker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ onnxruntime::Status CreateOpAttr(const char* name, const void* data, int len, Or
attr->set_type(ONNX_NAMESPACE::AttributeProto_AttributeType::AttributeProto_AttributeType_FLOATS);
break;
case OrtOpAttrType::ORT_OP_ATTR_STRING:
attr->set_s(std::string{str});
attr->set_s(std::string{str, str + len});
attr->set_type(ONNX_NAMESPACE::AttributeProto_AttributeType::AttributeProto_AttributeType_STRING);
break;
case OrtOpAttrType::ORT_OP_ATTR_STRINGS:
Expand Down
5 changes: 3 additions & 2 deletions onnxruntime/test/shared_lib/custom_op_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,9 @@ void StandaloneCustomKernel::InitGru() {
float betas[1] = {2.f};
Ort::OpAttr activation_beta = Ort::OpAttr("activation_beta ", betas, 1, OrtOpAttrType::ORT_OP_ATTR_FLOATS);

const char* direction_string = "bidirectional";
Ort::OpAttr direction = Ort::OpAttr("direction", direction_string, 1, OrtOpAttrType::ORT_OP_ATTR_STRING);
const std::string direction_string = "bidirectional";
Ort::OpAttr direction = Ort::OpAttr("direction", direction_string.c_str(), static_cast<int>(direction_string.length()),
OrtOpAttrType::ORT_OP_ATTR_STRING);

int64_t linear_before_reset_value = 0;
Ort::OpAttr linear_before_reset = Ort::OpAttr("linear_before_reset", &linear_before_reset_value, 1,
Expand Down
Loading