Blame SOURCES/gnome-system-monitor-40.1-changing-points-crash.patch

ab4ab5
From cc5200c00aa82894a97ac0a89efe19fcb9db5260 Mon Sep 17 00:00:00 2001
ab4ab5
From: Sergey Bugaev <bugaevc@gmail.com>
ab4ab5
Date: Sat, 1 May 2021 14:23:33 +0300
ab4ab5
Subject: [PATCH] Fix crash when changing the number of points
ab4ab5
ab4ab5
std::rotate accepts an "end" iterator that points one after the last
ab4ab5
item. In plain C, it's possible to create a "one past end" pointer by
ab4ab5
using the following idiom: &arr[size], which is equivalent to arr + size.
ab4ab5
No actual pointer dereference happens in this case. In C++, vector[index]
ab4ab5
always causes a vector.operator[](index) invocation; it is undefined
ab4ab5
behavior to use this method with out-of-bounds indexes. Therefore, this
ab4ab5
idiom cannot be used in C++ to get a "one past end" iterator. Instead,
ab4ab5
C++ provides .begin() and .end() methods. Use those instead.
ab4ab5
ab4ab5
Fixes https://gitlab.gnome.org/GNOME/gnome-system-monitor/-/issues/197
ab4ab5
Fixes https://gitlab.gnome.org/GNOME/gnome-system-monitor/-/issues/181
ab4ab5
---
ab4ab5
 src/load-graph.cpp | 23 +++++++++--------------
ab4ab5
 1 file changed, 9 insertions(+), 14 deletions(-)
ab4ab5
ab4ab5
diff --git a/src/load-graph.cpp b/src/load-graph.cpp
ab4ab5
index b18bc61c..aa28589f 100644
ab4ab5
--- a/src/load-graph.cpp
ab4ab5
+++ b/src/load-graph.cpp
ab4ab5
@@ -792,9 +792,9 @@ void
ab4ab5
 load_graph_update_data (LoadGraph *graph)
ab4ab5
 {
ab4ab5
     // Rotate data one element down.
ab4ab5
-    std::rotate(&graph->data[0],
ab4ab5
-                &graph->data[graph->num_points - 2],
ab4ab5
-                &graph->data[graph->num_points - 1]);
ab4ab5
+    std::rotate(graph->data.begin(),
ab4ab5
+                graph->data.end() - 1,
ab4ab5
+                graph->data.end());
ab4ab5
 
ab4ab5
     // Update rotation counter.
ab4ab5
     graph->latest = (graph->latest + 1) % graph->num_points;
ab4ab5
@@ -1061,31 +1061,26 @@ void
ab4ab5
 load_graph_change_num_points(LoadGraph *graph,
ab4ab5
                              guint new_num_points)
ab4ab5
 {
ab4ab5
-    //Don't do anything if the value didn't change.
ab4ab5
+    // Don't do anything if the value didn't change.
ab4ab5
     if (graph->num_points == new_num_points)
ab4ab5
         return;
ab4ab5
 
ab4ab5
     // Sort the values in the data_block vector in the order they were accessed in by the pointers in data.
ab4ab5
-    std::rotate(&graph->data_block[0],
ab4ab5
-                &graph->data_block[(graph->num_points - graph->latest) * graph->n],
ab4ab5
-                &graph->data_block[graph->num_points * graph->n]);
ab4ab5
+    std::rotate(graph->data_block.begin(),
ab4ab5
+                graph->data_block.begin() + (graph->num_points - graph->latest) * graph->n,
ab4ab5
+                graph->data_block.end());
ab4ab5
 
ab4ab5
     // Reset rotation counter.
ab4ab5
     graph->latest = 0;
ab4ab5
 
ab4ab5
     // Resize the vectors to the new amount of data points.
ab4ab5
+    // Fill the new values with -1.
ab4ab5
     graph->data.resize(new_num_points);
ab4ab5
-    graph->data_block.resize(graph->n * new_num_points);
ab4ab5
+    graph->data_block.resize(graph->n * new_num_points, -1.0);
ab4ab5
     if (graph->type == LOAD_GRAPH_NET) {
ab4ab5
         graph->net.values.resize(new_num_points);
ab4ab5
     }
ab4ab5
 
ab4ab5
-    // Fill the new values with -1 instead of 0 if the vectors got bigger.
ab4ab5
-    if (new_num_points > graph->num_points) {
ab4ab5
-        std::fill(&graph->data_block[graph->n * graph->num_points],
ab4ab5
-                  &graph->data_block[graph->n * new_num_points], -1.0);
ab4ab5
-    }
ab4ab5
-
ab4ab5
     // Replace the pointers in data, to match the new data_block values.
ab4ab5
     for (guint i = 0; i < new_num_points; ++i) {
ab4ab5
         graph->data[i] = &graph->data_block[0] + i * graph->n;
ab4ab5
-- 
ab4ab5
2.36.1
ab4ab5