Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
de63dc9
Fix clang-tidy bugprone-exception-escape warnings in API
dbarker Apr 1, 2026
b3fb6d2
Fix ci failures. Fix function_ref warning. Fix additional exception e…
dbarker Apr 1, 2026
b3e5740
Add unreachable macro. Add tests
dbarker Apr 1, 2026
0b6d984
Use OPENTELEMETRY_HAVE_EXCEPTIONS macro uniformly in API. Fix eol on …
dbarker Apr 1, 2026
6090ef9
fix iwyu warning in string_view test
dbarker Apr 2, 2026
0a9c076
Merge branch 'main' into fix_clangtidy_exception_escape_in_api
dbarker Apr 2, 2026
c76e6d4
Merge branch 'main' into fix_clangtidy_exception_escape_in_api
dbarker Apr 3, 2026
5f3d5ee
revert changes covered by #3965
dbarker Apr 3, 2026
79eed4e
revert cmakelist change to api trace test
dbarker Apr 3, 2026
fe2da7e
Merge branch 'main' into fix_clangtidy_exception_escape_in_api
dbarker Apr 8, 2026
b511dbe
Merge branch 'main' into fix_clangtidy_exception_escape_in_api
ThomsonTan Apr 9, 2026
db192e0
Merge branch 'main' into fix_clangtidy_exception_escape_in_api
dbarker Apr 13, 2026
a1253df
Remove exception try/catch and unreachable macros
dbarker Apr 13, 2026
6105492
Merge branch 'main' into fix_clangtidy_exception_escape_in_api
dbarker Apr 13, 2026
3a944d8
revert change to kv_properties.h
dbarker Apr 13, 2026
4426ba8
Merge branch 'main' into fix_clangtidy_exception_escape_in_api
dbarker Apr 23, 2026
6fdb531
move trace state exception handling to static methods with regex call…
dbarker Apr 23, 2026
48dcdae
Merge branch 'main' into fix_clangtidy_exception_escape_in_api
dbarker Apr 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion api/include/opentelemetry/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <ctype.h>

#include "opentelemetry/common/macros.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/version.h"

Expand All @@ -25,7 +26,18 @@ class StringUtil
{
right--;
}
return str.substr(left, 1 + right - left);
#if OPENTELEMETRY_HAVE_EXCEPTIONS
try
#endif
{
return str.substr(left, 1 + right - left);
}
#if OPENTELEMETRY_HAVE_EXCEPTIONS
catch (...)
{
return nostd::string_view();
}
#endif
}

static nostd::string_view Trim(nostd::string_view str) noexcept
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/nostd/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class string_view
{
if (pos > length_)
{
# if __EXCEPTIONS
# if OPENTELEMETRY_HAVE_EXCEPTIONS
Comment thread
dbarker marked this conversation as resolved.
throw std::out_of_range{"opentelemetry::nostd::string_view"};
# else
std::terminate();
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/nostd/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// Header-only. Without compiling the actual Abseil binary. As Abseil moves on to new
// toolchains, it may drop support for Visual Studio 2015 in future versions.

# if defined(__EXCEPTIONS)
# if OPENTELEMETRY_HAVE_EXCEPTIONS
# include <exception>
OPENTELEMETRY_BEGIN_NAMESPACE
namespace nostd
Expand Down
30 changes: 14 additions & 16 deletions api/include/opentelemetry/plugin/detail/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@

#pragma once

#if __EXCEPTIONS
# include <new>
#endif // __EXCEPTIONS
#include "opentelemetry/version.h"

#include <string>

#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace plugin
{
namespace detail
{
inline void CopyErrorMessage(const char *source, std::string &destination) noexcept
#if __EXCEPTIONS
try
#endif
{
if (source == nullptr)
#if OPENTELEMETRY_HAVE_EXCEPTIONS
Comment thread
dbarker marked this conversation as resolved.
try
#endif
{
if (source == nullptr)
{
return;
}
destination.assign(source);
}
#if OPENTELEMETRY_HAVE_EXCEPTIONS
catch (...)
{
return;
}
destination.assign(source);
}
#if __EXCEPTIONS
catch (const std::bad_alloc &)
{
return;
}
#endif
}
} // namespace detail
} // namespace plugin
OPENTELEMETRY_END_NAMESPACE
4 changes: 2 additions & 2 deletions api/include/opentelemetry/std/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using monostate = std::monostate;
// Apple Platforms provide std::bad_variant_access only in newer versions of OS.
// To keep API compatible with any version of OS - we are providing our own
// implementation of nostd::bad_variant_access exception.
# if __EXCEPTIONS
# if OPENTELEMETRY_HAVE_EXCEPTIONS

// nostd::bad_variant_access
class bad_variant_access : public std::exception
Expand All @@ -48,7 +48,7 @@ class bad_variant_access : public std::exception
}
# endif

# if __EXCEPTIONS
# if OPENTELEMETRY_HAVE_EXCEPTIONS
# define THROW_BAD_VARIANT_ACCESS throw_bad_variant_access()
# else
# define THROW_BAD_VARIANT_ACCESS std::terminate()
Expand Down
60 changes: 41 additions & 19 deletions api/include/opentelemetry/trace/trace_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class OPENTELEMETRY_EXPORT TraceState
* For multi-tenant vendor scenarios, an at sign (@) can be used to prefix the vendor name.
*
*/
static bool IsValidKey(nostd::string_view key)
static bool IsValidKey(nostd::string_view key) noexcept
{
#if OPENTELEMETRY_HAVE_WORKING_REGEX
return IsValidKeyRegEx(key);
Expand All @@ -222,7 +222,7 @@ class OPENTELEMETRY_EXPORT TraceState
* The value is an opaque string containing up to 256 printable ASCII (RFC0020)
* characters ((i.e., the range 0x20 to 0x7E) except comma , and equal =)
*/
static bool IsValidValue(nostd::string_view value)
static bool IsValidValue(nostd::string_view value) noexcept
{
#if OPENTELEMETRY_HAVE_WORKING_REGEX
return IsValidValueRegEx(value);
Expand All @@ -249,29 +249,51 @@ class OPENTELEMETRY_EXPORT TraceState
}

#if OPENTELEMETRY_HAVE_WORKING_REGEX
static bool IsValidKeyRegEx(nostd::string_view key)
static bool IsValidKeyRegEx(nostd::string_view key) noexcept
{
static std::regex reg_key("^[a-z0-9][a-z0-9*_\\-/]{0,255}$");
static std::regex reg_key_multitenant(
"^[a-z0-9][a-z0-9*_\\-/]{0,240}(@)[a-z0-9][a-z0-9*_\\-/]{0,13}$");
std::string key_s(key.data(), key.size());
if (std::regex_match(key_s, reg_key) || std::regex_match(key_s, reg_key_multitenant))
# if OPENTELEMETRY_HAVE_EXCEPTIONS
try
{
return true;
# endif
static std::regex reg_key("^[a-z0-9][a-z0-9*_\\-/]{0,255}$");
static std::regex reg_key_multitenant(
"^[a-z0-9][a-z0-9*_\\-/]{0,240}(@)[a-z0-9][a-z0-9*_\\-/]{0,13}$");
std::string key_s(key.data(), key.size());
if (std::regex_match(key_s, reg_key) || std::regex_match(key_s, reg_key_multitenant))
{
return true;
}
return false;
# if OPENTELEMETRY_HAVE_EXCEPTIONS
}
catch (...)
{
return false;
}
return false;
# endif
}

static bool IsValidValueRegEx(nostd::string_view value)
static bool IsValidValueRegEx(nostd::string_view value) noexcept
{
// Hex 0x20 to 0x2B, 0x2D to 0x3C, 0x3E to 0x7E
static std::regex reg_value(
"^[\\x20-\\x2B\\x2D-\\x3C\\x3E-\\x7E]{0,255}[\\x21-\\x2B\\x2D-\\x3C\\x3E-\\x7E]$");
// Need to benchmark without regex, as a string object is created here.
return std::regex_match(std::string(value.data(), value.size()), reg_value);
# if OPENTELEMETRY_HAVE_EXCEPTIONS
try
{
# endif
// Hex 0x20 to 0x2B, 0x2D to 0x3C, 0x3E to 0x7E
static std::regex reg_value(
"^[\\x20-\\x2B\\x2D-\\x3C\\x3E-\\x7E]{0,255}[\\x21-\\x2B\\x2D-\\x3C\\x3E-\\x7E]$");
// Need to benchmark without regex, as a string object is created here.
return std::regex_match(std::string(value.data(), value.size()), reg_value);
# if OPENTELEMETRY_HAVE_EXCEPTIONS
}
catch (...)
{
return false;
}
# endif
}
#else
static bool IsValidKeyNonRegEx(nostd::string_view key)
static bool IsValidKeyNonRegEx(nostd::string_view key) noexcept
{
if (key.empty() || key.size() > kKeyMaxSize || !IsLowerCaseAlphaOrDigit(key[0]))
{
Expand All @@ -294,7 +316,7 @@ class OPENTELEMETRY_EXPORT TraceState
return true;
}

static bool IsValidValueNonRegEx(nostd::string_view value)
static bool IsValidValueNonRegEx(nostd::string_view value) noexcept
{
if (value.empty() || value.size() > kValueMaxSize)
{
Expand All @@ -312,7 +334,7 @@ class OPENTELEMETRY_EXPORT TraceState
}
#endif

static bool IsLowerCaseAlphaOrDigit(char c) { return isdigit(c) || islower(c); }
static bool IsLowerCaseAlphaOrDigit(char c) noexcept { return isdigit(c) || islower(c); }

private:
// Store entries in a C-style array to avoid using std::array or std::vector.
Expand Down
10 changes: 10 additions & 0 deletions api/test/common/string_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <gtest/gtest.h>
#include <string.h>

#include <opentelemetry/common/macros.h>
#include <opentelemetry/common/string_util.h>
#include "opentelemetry/nostd/string_view.h"

Expand Down Expand Up @@ -50,3 +51,12 @@ TEST(StringUtilTest, TrimString)
EXPECT_EQ(StringUtil::Trim(testcase.input), testcase.expected);
}
}

TEST(StringUtilTest, TrimStringOutOfRange)
{
#if OPENTELEMETRY_HAVE_EXCEPTIONS
EXPECT_EQ(StringUtil::Trim("x", 2, 1), "");
#else
EXPECT_DEATH(StringUtil::Trim("x", 2, 1), "");
#endif
}
3 changes: 2 additions & 1 deletion api/test/nostd/string_view_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <utility>

#include "opentelemetry/common/macros.h"
#include "opentelemetry/nostd/string_view.h"

using opentelemetry::nostd::string_view;
Expand Down Expand Up @@ -74,7 +75,7 @@ TEST(StringViewTest, SubstrPortion)
TEST(StringViewTest, SubstrOutOfRange)
{
string_view s = "abc123";
#if __EXCEPTIONS || (defined(OPENTELEMETRY_STL_VERSION) && (OPENTELEMETRY_STL_VERSION >= 2017))
#if OPENTELEMETRY_HAVE_EXCEPTIONS
EXPECT_THROW((void)s.substr(10), std::out_of_range);
#else
EXPECT_DEATH({ s.substr(10); }, "");
Expand Down
2 changes: 1 addition & 1 deletion api/test/nostd/variant_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ TEST(VariantTest, Get)
EXPECT_EQ(nostd::get<int>(w), 12);
EXPECT_EQ(*nostd::get_if<int>(&v), 12);
EXPECT_EQ(nostd::get_if<float>(&v), nullptr);
#if __EXCEPTIONS || (defined(OPENTELEMETRY_STL_VERSION) && (OPENTELEMETRY_STL_VERSION >= 2017))
#if OPENTELEMETRY_HAVE_EXCEPTIONS
EXPECT_THROW(nostd::get<float>(w), nostd::bad_variant_access);
#else
EXPECT_DEATH({ nostd::get<float>(w); }, "");
Expand Down
10 changes: 10 additions & 0 deletions api/test/trace/trace_state_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ TEST(TraceStateTest, ValidateHeaderParsing)
} testcases[] = {{"k1=v1", "k1=v1"},
{"K1=V1", ""},
{"k1=v1,k2=v2,k3=v3", "k1=v1,k2=v2,k3=v3"},
{"k1=v1,InvalidKey=v2", ""},
{"k1=v1,k2=v2,,", "k1=v1,k2=v2"},
{"k1=v1,k2=v2,invalidmember", ""},
{"1a-2f@foo=bar1,a*/foo-_/bar=bar4", "1a-2f@foo=bar1,a*/foo-_/bar=bar4"},
Expand All @@ -73,6 +74,15 @@ TEST(TraceStateTest, ValidateHeaderParsing)
}
}

TEST(TraceStateTest, ExceedsMaxKeyValuePairs)
{
std::string header = header_with_max_members();
header += ",overflow=value";

auto ts = TraceState::FromHeader(header);
EXPECT_EQ(ts->ToHeader(), "");
}

TEST(TraceStateTest, TraceStateGet)
{

Expand Down
Loading