From 7f53571e0c9490b38b1871696e50aa1f75800dde Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 4 May 2023 19:47:46 +0200 Subject: [PATCH] impl-node: avoid division by zero better Since the activation is in shared memory, a bad client could try to modify them after we check the signal_time and prev_signal time and cause a division by zero. Make a unique copy of the values and use those. --- src/pipewire/impl-node.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/pipewire/impl-node.c b/src/pipewire/impl-node.c index 8a9c0d89d..04744119e 100644 --- a/src/pipewire/impl-node.c +++ b/src/pipewire/impl-node.c @@ -1133,9 +1133,11 @@ static inline int node_signal_func(void *data) static inline void calculate_stats(struct pw_impl_node *this, struct pw_node_activation *a) { - if (SPA_LIKELY(a->signal_time > a->prev_signal_time)) { + uint64_t signal_time = a->signal_time; + uint64_t prev_signal_time = a->prev_signal_time; + if (SPA_LIKELY(signal_time > prev_signal_time)) { uint64_t process_time = a->finish_time - a->signal_time; - uint64_t period_time = a->signal_time - a->prev_signal_time; + uint64_t period_time = signal_time - prev_signal_time; float load = (float) process_time / (float) period_time; a->cpu_load[0] = (a->cpu_load[0] + load) / 2.0f; a->cpu_load[1] = (a->cpu_load[1] * 7.0f + load) / 8.0f;