[metrics] Do not write to statsd when not supported
Previously, on platforms that did not support statsd, we would add a
null pointer to the list of backends when statsd was enabled. This led
to a null pointer dereference when trying to report metrics.
The problem was that CreateStatsdBackend returns nullptr when statsd
is not supported, but we were not checking the return value. This CL
fixes it.
Test: ./test.py --run-test --host -t 801 --jit --64 \
--runtime-option=-Xwrite-metrics-to-statsd=true
Bug: 170149255
Change-Id: I02af48d1ec60f902ba2651b6991c017d88d75495
diff --git a/runtime/metrics/reporter.cc b/runtime/metrics/reporter.cc
index 262422e..5ff0a39 100644
--- a/runtime/metrics/reporter.cc
+++ b/runtime/metrics/reporter.cc
@@ -90,7 +90,10 @@
backends_.emplace_back(new FileBackend(config_.dump_to_file.value()));
}
if (config_.dump_to_statsd) {
- backends_.emplace_back(CreateStatsdBackend());
+ auto backend = CreateStatsdBackend();
+ if (backend != nullptr) {
+ backends_.emplace_back(std::move(backend));
+ }
}
MaybeResetTimeout();