Blame SOURCES/0077-util-Fix-the-size-of-sorted_displays-allocation.patch

03a6ff
From 40bc1bb5048f7e2c2abbb7074286b9772d3a2397 Mon Sep 17 00:00:00 2001
03a6ff
From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= <fidencio@redhat.com>
03a6ff
Date: Wed, 21 Oct 2015 15:12:28 +0200
03a6ff
Subject: [PATCH] util: Fix the size of sorted_displays allocation
03a6ff
03a6ff
As sorted_displays is a vector containing all displays' order, its
03a6ff
allocation size must be the maximum display id + 1 instead of the
03a6ff
maximum display id. Also, fix the size used for sorting and iterating
03a6ff
the sorted_displays vector.
03a6ff
03a6ff
Valgrind log:
03a6ff
==15946== Invalid write of size 4
03a6ff
==15946==    at 0x4169C0: virt_viewer_align_monitors_linear (virt-viewer-util.c:581)
03a6ff
==15946==    by 0x42248B: virt_viewer_session_on_monitor_geometry_changed (virt-viewer-session.c:438)
03a6ff
==15946==    by 0xBB41F03: _g_closure_invoke_va (gclosure.c:831)
03a6ff
==15946==    by 0xBB5BC7C: g_signal_emit_valist (gsignal.c:3214)
03a6ff
==15946==    by 0xBB5C764: g_signal_emit_by_name (gsignal.c:3401)
03a6ff
==15946==    by 0x4328F3: virt_viewer_display_spice_monitor_geometry_changed (virt-viewer-display-spice.c:93)
03a6ff
==15946==    by 0x432D60: virt_viewer_display_spice_size_allocate (virt-viewer-display-spice.c:224)
03a6ff
==15946==    by 0xBB41CD4: g_closure_invoke (gclosure.c:768)
03a6ff
==15946==    by 0xBB53538: signal_emit_unlocked_R (gsignal.c:3549)
03a6ff
==15946==    by 0xBB5BEEF: g_signal_emit_valist (gsignal.c:3305)
03a6ff
==15946==    by 0xBB5C29E: g_signal_emit (gsignal.c:3361)
03a6ff
==15946==    by 0x637D6F6: gtk_widget_size_allocate_with_baseline (gtkwidget.c:6093)
03a6ff
==15946==  Address 0x18c79d4c is 0 bytes after a block of size 12 alloc'd
03a6ff
==15946==    at 0x4C2A9C7: calloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
03a6ff
==15946==    by 0xBDD36D1: g_malloc0 (gmem.c:127)
03a6ff
==15946==    by 0x41698D: virt_viewer_align_monitors_linear (virt-viewer-util.c:577)
03a6ff
==15946==    by 0x42248B: virt_viewer_session_on_monitor_geometry_changed (virt-viewer-session.c:438)
03a6ff
==15946==    by 0xBB41F03: _g_closure_invoke_va (gclosure.c:831)
03a6ff
==15946==    by 0xBB5BC7C: g_signal_emit_valist (gsignal.c:3214)
03a6ff
==15946==    by 0xBB5C764: g_signal_emit_by_name (gsignal.c:3401)
03a6ff
==15946==    by 0x4328F3: virt_viewer_display_spice_monitor_geometry_changed (virt-viewer-display-spice.c:93)
03a6ff
==15946==    by 0x432D60: virt_viewer_display_spice_size_allocate (virt-viewer-display-spice.c:224)
03a6ff
==15946==    by 0xBB41CD4: g_closure_invoke (gclosure.c:768)
03a6ff
==15946==    by 0xBB53538: signal_emit_unlocked_R (gsignal.c:3549)
03a6ff
==15946==    by 0xBB5BEEF: g_signal_emit_valist (gsignal.c:3305)
03a6ff
03a6ff
Resolves: rhbz#1272650
03a6ff
Related: rhbz#1267184
03a6ff
(cherry picked from commit 96413a62663cb68f1559abac2c37b0eb80ddf4ab)
03a6ff
---
03a6ff
 src/virt-viewer-util.c | 11 +++++++----
03a6ff
 1 file changed, 7 insertions(+), 4 deletions(-)
03a6ff
03a6ff
diff --git a/src/virt-viewer-util.c b/src/virt-viewer-util.c
03a6ff
index e9f771b..f2ccd13 100644
03a6ff
--- a/src/virt-viewer-util.c
03a6ff
+++ b/src/virt-viewer-util.c
03a6ff
@@ -565,6 +565,7 @@ virt_viewer_align_monitors_linear(GHashTable *displays)
03a6ff
     gint i, x = 0;
03a6ff
     guint *sorted_displays;
03a6ff
     guint max_id = 0;
03a6ff
+    guint ndisplays = 0;
03a6ff
     GHashTableIter iter;
03a6ff
     gpointer key, value;
03a6ff
 
03a6ff
@@ -574,19 +575,21 @@ virt_viewer_align_monitors_linear(GHashTable *displays)
03a6ff
         return;
03a6ff
 
03a6ff
     g_hash_table_foreach(displays, find_max_id, &max_id);
03a6ff
-    sorted_displays = g_new0(guint, max_id);
03a6ff
+    ndisplays = max_id + 1;
03a6ff
+
03a6ff
+    sorted_displays = g_new0(guint, ndisplays);
03a6ff
 
03a6ff
     g_hash_table_iter_init(&iter, displays);
03a6ff
     while (g_hash_table_iter_next(&iter, &key, &value))
03a6ff
         sorted_displays[GPOINTER_TO_INT(key)] = GPOINTER_TO_INT(key);
03a6ff
 
03a6ff
-    g_qsort_with_data(sorted_displays, max_id, sizeof(guint), displays_cmp, displays);
03a6ff
+    g_qsort_with_data(sorted_displays, ndisplays, sizeof(guint), displays_cmp, displays);
03a6ff
 
03a6ff
     /* adjust monitor positions so that there's no gaps or overlap between
03a6ff
      * monitors */
03a6ff
-    for (i = 0; i < max_id; i++) {
03a6ff
+    for (i = 0; i < ndisplays; i++) {
03a6ff
         guint nth = sorted_displays[i];
03a6ff
-        g_assert(nth < max_id);
03a6ff
+        g_assert(nth < ndisplays);
03a6ff
         GdkRectangle *rect = g_hash_table_lookup(displays, GINT_TO_POINTER(nth));
03a6ff
         rect->x = x;
03a6ff
         rect->y = 0;