Skip to content

Out of boundary exception using MSVC std::string_view in //api/test/common:string_util_test with -c dbg #1951

@malkia

Description

@malkia

This fails a simple unit test, so I'll ignore the details.

I believe the line here https://github.com/open-telemetry/opentelemetry-cpp/blob/main/api/include/opentelemetry/common/string_util.h#L17 and few lines below

while (str[static_cast<std::size_t>(left)] == ' ' && left <= right)

should be:

while (left <= right && str[static_cast<std::size_t>(left)] == ' ')

In both cases the boundary check (left <= right) must be done first, and then read character from the array.

This was caught with HAVE_CPP_STDLIB=1 and -c dbg on //api/test/common:string_util_test (in bazel) using MSVC
It doesn't trigger error in fastbld/opt which makes me thing the MSVC standard string_view throws an exception there.

Probably as room for improvement, the nostd::string_view should also throw an exception (in debug) if this happens.
Not sure how gcc/clang work.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions