diff options
author | 2021-01-27 11:41:58 -0800 | |
---|---|---|
committer | 2021-01-28 21:23:25 +0000 | |
commit | a7c66e7e5845b26711b261773179abf0f8b34ced (patch) | |
tree | a621a91059f9a7d2cefc92710fac2cd28350d133 | |
parent | af2aabc47d97556bf0fde258e6ddb472eebe0855 (diff) |
Avoid potential null dereference
In a log statement we were dereferencing a pointer that could
potentially be null. Add support for safely dereferencing a pointer
for ostreams.
Test: ./test.py --host
Bug: 67037140
Change-Id: Ibe04811883575902a9b2fe8a064f18de2b2cea7b
-rw-r--r-- | compiler/optimizing/nodes.h | 2 | ||||
-rw-r--r-- | libartbase/base/stl_util.h | 19 |
2 files changed, 20 insertions, 1 deletions
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h index 9e0f515ba9..6381b2c356 100644 --- a/compiler/optimizing/nodes.h +++ b/compiler/optimizing/nodes.h @@ -8256,7 +8256,7 @@ class HParallelMove final : public HExpression<0> { DCHECK(!destination.OverlapsWith(move.GetDestination())) << "Overlapped destination for two moves in a parallel move: " << move.GetSource() << " ==> " << move.GetDestination() << " and " - << source << " ==> " << destination << " for " << *instruction; + << source << " ==> " << destination << " for " << SafePrint(instruction); } } moves_.emplace_back(source, destination, type, instruction); diff --git a/libartbase/base/stl_util.h b/libartbase/base/stl_util.h index 26b1de8b9b..4d4a686620 100644 --- a/libartbase/base/stl_util.h +++ b/libartbase/base/stl_util.h @@ -304,6 +304,25 @@ static inline IterationRange<FilterNull<InnerIter>> FilterOutNull(IterationRange return Filter(inner, NonNullFilter<typename InnerIter::value_type>()); } +template <typename Val> +struct SafePrinter { + const Val* val_; +}; + +template<typename Val> +std::ostream& operator<<(std::ostream& os, const SafePrinter<Val>& v) { + if (v.val_ == nullptr) { + return os << "NULL"; + } else { + return os << *v.val_; + } +} + +template<typename Val> +SafePrinter<Val> SafePrint(const Val* v) { + return SafePrinter<Val>{v}; +} + } // namespace art #endif // ART_LIBARTBASE_BASE_STL_UTIL_H_ |