From: Simon Marchi Date: Fri, 27 Sep 2024 21:02:41 +0000 (-0400) Subject: ctf: add explicit cast to CStringView in ternary X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=602446e40e7a077f89dcdbdd6adf7fd2cfe051ef;p=babeltrace.git ctf: add explicit cast to CStringView in ternary In this ternary, the true branch yields a reference to std::string. The false branch, `nullptr` is therefore implicitly cast to an std::string. It builds, since you can build an std::string from a pointer to a C string, but it would crash at runtime, since constructing an std::string from nullptr doesn't work. Ultimately, we want to pass this to the ClockClass::origin() setter, which takes a CStringView, so cast the true branch to CStringView. If the condition is false, this constructs a CStringView from nullptr, which is correct and what we want to do here. This problem was reported by clang-tidy as a bugprone-string-constructor [1] error ("Constructing string from nullptr is undefined behaviour"). [1] https://clang.llvm.org/extra/clang-tidy/checks/bugprone/string-constructor.html Change-Id: Ib9c8dfa619cbd652c8284ef7c3a4ccebeb0e6fbd Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/13299 Tested-by: jenkins Reviewed-by: Philippe Proulx --- diff --git a/src/plugins/ctf/common/src/metadata/metadata-stream-parser.cpp b/src/plugins/ctf/common/src/metadata/metadata-stream-parser.cpp index 9032d7b7..33267625 100644 --- a/src/plugins/ctf/common/src/metadata/metadata-stream-parser.cpp +++ b/src/plugins/ctf/common/src/metadata/metadata-stream-parser.cpp @@ -1820,7 +1820,9 @@ private: clkCls.libCls()->setOriginIsUnixEpoch(); } else if (_mMipVersion >= 1) { /* Custom (MIP 1+) */ - clkCls.libCls()->origin(clkCls.origin()->ns() ? *clkCls.origin()->ns() : nullptr, + clkCls.libCls()->origin(clkCls.origin()->ns() ? + static_cast(*clkCls.origin()->ns()) : + nullptr, clkCls.origin()->name(), clkCls.origin()->uid()); } } else {