From b7b30f8d479b4920b15316e97449609b1d825a55 Mon Sep 17 00:00:00 2001 From: Sean Lynch <42618346+swlynch99@users.noreply.github.com> Date: Fri, 19 Jan 2024 19:33:17 -0500 Subject: [PATCH] Fix unbounded recursion in Metric impl for CoreMetric (metriken v0.3.5) (#104) * Fix unbounded recursion in Metric impl for CoreMetric All these infinitely recurse. Unfortunately the recursion was indirect so it was not caught by rustc. * Bump to v0.3.5 * formatting --- metriken/Cargo.toml | 2 +- metriken/src/lib.rs | 12 ++++++------ metriken/tests/as_any.rs | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 metriken/tests/as_any.rs diff --git a/metriken/Cargo.toml b/metriken/Cargo.toml index 333b4648..e703a146 100644 --- a/metriken/Cargo.toml +++ b/metriken/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "metriken" -version = "0.3.4" +version = "0.3.5" edition = "2021" authors = [ "Brian Martin ", diff --git a/metriken/src/lib.rs b/metriken/src/lib.rs index d01c5d7c..b93c55d9 100644 --- a/metriken/src/lib.rs +++ b/metriken/src/lib.rs @@ -165,15 +165,15 @@ pub mod export { impl metriken_core::Metric for WrapMetric { fn is_enabled(&self) -> bool { - ::is_enabled(self) + ::is_enabled(self) } fn as_any(&self) -> Option<&dyn std::any::Any> { - ::as_any(self) + ::as_any(self) } fn value(&self) -> Option { - let value = ::value(self)?; + let value = ::value(self)?; Some(match value { crate::Value::Counter(val) => metriken_core::Value::Counter(val), @@ -310,17 +310,17 @@ impl std::fmt::Debug for MetricEntry { impl Metric for T { fn is_enabled(&self) -> bool { - ::is_enabled(self) + ::is_enabled(self) } fn as_any(&self) -> Option<&dyn Any> { - ::as_any(self) + ::as_any(self) } fn value(&self) -> Option { use metriken_core::Value as CoreValue; - Some(match ::value(self)? { + Some(match ::value(self)? { CoreValue::Counter(val) => Value::Counter(val), CoreValue::Gauge(val) => Value::Gauge(val), CoreValue::Other(val) => { diff --git a/metriken/tests/as_any.rs b/metriken/tests/as_any.rs new file mode 100644 index 00000000..456d3aa7 --- /dev/null +++ b/metriken/tests/as_any.rs @@ -0,0 +1,14 @@ +use metriken::{metric, metrics, Counter}; + +#[metric] +static THE_METRIC: Counter = Counter::new(); + +#[test] +fn as_any_does_not_recurse_infinitely() { + let metrics = metrics().static_metrics(); + let metric = metrics.iter().find(|entry| entry.is(&THE_METRIC)).unwrap(); + + let counter = metric.as_any().unwrap().downcast_ref::().unwrap(); + + assert_eq!(counter.value(), THE_METRIC.value()); +}