ctf: add explicit cast to CStringView in ternary
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 27 Sep 2024 21:02:41 +0000 (17:02 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 9 Oct 2024 02:56:57 +0000 (22:56 -0400)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/13299
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/plugins/ctf/common/src/metadata/metadata-stream-parser.cpp

index 9032d7b736f8db9f8b1c63a6fa4489ecaf152749..33267625c2681e9f4bd7960632469530ee0981dc 100644 (file)
@@ -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<bt2c::CStringView>(*clkCls.origin()->ns()) :
+                                            nullptr,
                                         clkCls.origin()->name(), clkCls.origin()->uid());
             }
         } else {
This page took 0.027542 seconds and 4 git commands to generate.