[mutter] Change all g_memdup() to g_memdup2()



commit 30e1c51b33eabba3e233293d66648903e2a6d65d
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Thu Feb 4 18:45:59 2021 +0100

    Change all g_memdup() to g_memdup2()
    
    Using g_memdup() is dangerous due to the type of the size argument. See
    https://gitlab.gnome.org/GNOME/glib/-/issues/2319 and
    https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1926 for details.
    
    Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1708>

 .gitlab-ci.yml                                    |  9 +++++++-
 clutter/clutter/clutter-event.c                   | 28 ++++++++++++++++-------
 clutter/clutter/clutter-shader-types.c            | 11 +++++----
 cogl/cogl/cogl-boxed-value.c                      | 18 +++++++--------
 meson.build                                       |  2 +-
 src/backends/meta-monitor-config-manager.c        |  9 ++++----
 src/backends/meta-monitor-manager-dummy.c         |  2 +-
 src/backends/native/meta-input-settings-native.c  |  2 +-
 src/backends/native/meta-kms-crtc.c               |  6 ++---
 src/backends/native/meta-kms-impl-device-simple.c |  2 +-
 src/backends/native/meta-kms-update.c             |  6 ++---
 src/backends/native/meta-monitor-manager-kms.c    |  6 ++---
 src/backends/x11/meta-monitor-manager-xrandr.c    |  6 ++---
 src/backends/x11/meta-output-xrandr.c             |  2 +-
 src/core/boxes.c                                  |  2 +-
 src/core/display.c                                |  2 +-
 src/core/keybindings.c                            |  4 ++--
 src/core/workspace.c                              |  2 +-
 src/x11/window-props.c                            |  6 +++--
 19 files changed, 75 insertions(+), 50 deletions(-)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 94b9a48cc2..2cf3934bd2 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -12,7 +12,7 @@ stages:
 .mutter.fedora:33@common:
   variables:
     FDO_DISTRIBUTION_VERSION: 33
-    BASE_TAG: '2021-01-22.0'
+    BASE_TAG: '2021-02-04.1'
     FDO_UPSTREAM_REPO: GNOME/mutter
     FDO_DISTRIBUTION_EXEC: |
       dnf install -y 'dnf-command(builddep)' &&
@@ -34,6 +34,13 @@ stages:
       dnf builddep -y xorg-x11-server-Xwayland --setopt=install_weak_deps=False &&
       dnf remove -y --noautoremove 'xorg-x11-server-Xwayland' &&
 
+      # For glib
+      dnf builddep -y glib2 --setopt=install_weak_deps=False &&
+
+      ./.gitlab-ci/install-meson-project.sh \
+          https://gitlab.gnome.org/GNOME/glib.git \
+          2.67.3 . &&
+
       ./.gitlab-ci/install-meson-project.sh \
           -Dlibaudit=disabled \
           -Dplymouth=disabled \
diff --git a/clutter/clutter/clutter-event.c b/clutter/clutter/clutter-event.c
index ba54613dc0..c0438a5d4e 100644
--- a/clutter/clutter/clutter-event.c
+++ b/clutter/clutter/clutter-event.c
@@ -1315,20 +1315,29 @@ clutter_event_copy (const ClutterEvent *event)
     case CLUTTER_BUTTON_PRESS:
     case CLUTTER_BUTTON_RELEASE:
       if (event->button.axes != NULL)
-        new_event->button.axes = g_memdup (event->button.axes,
-                                           sizeof (gdouble) * CLUTTER_INPUT_AXIS_LAST);
+        {
+          new_event->button.axes =
+            g_memdup2 (event->button.axes,
+                       sizeof (double) * CLUTTER_INPUT_AXIS_LAST);
+        }
       break;
 
     case CLUTTER_SCROLL:
       if (event->scroll.axes != NULL)
-        new_event->scroll.axes = g_memdup (event->scroll.axes,
-                                           sizeof (gdouble) * CLUTTER_INPUT_AXIS_LAST);
+        {
+          new_event->scroll.axes =
+            g_memdup2 (event->scroll.axes,
+                       sizeof (double) * CLUTTER_INPUT_AXIS_LAST);
+        }
       break;
 
     case CLUTTER_MOTION:
       if (event->motion.axes != NULL)
-        new_event->motion.axes = g_memdup (event->motion.axes,
-                                           sizeof (gdouble) * CLUTTER_INPUT_AXIS_LAST);
+        {
+          new_event->motion.axes =
+            g_memdup2 (event->motion.axes,
+                       sizeof (double) * CLUTTER_INPUT_AXIS_LAST);
+        }
       break;
 
     case CLUTTER_TOUCH_BEGIN:
@@ -1336,8 +1345,11 @@ clutter_event_copy (const ClutterEvent *event)
     case CLUTTER_TOUCH_END:
     case CLUTTER_TOUCH_CANCEL:
       if (event->touch.axes != NULL)
-        new_event->touch.axes = g_memdup (event->touch.axes,
-                                          sizeof (gdouble) * CLUTTER_INPUT_AXIS_LAST);
+        {
+          new_event->touch.axes =
+            g_memdup2 (event->touch.axes,
+                      sizeof (double) * CLUTTER_INPUT_AXIS_LAST);
+        }
       break;
 
     case CLUTTER_DEVICE_ADDED:
diff --git a/clutter/clutter/clutter-shader-types.c b/clutter/clutter/clutter-shader-types.c
index c0265b6976..6a1b3e5049 100644
--- a/clutter/clutter/clutter-shader-types.c
+++ b/clutter/clutter/clutter-shader-types.c
@@ -157,7 +157,8 @@ clutter_value_lcopy_shader_float (const GValue *value,
                             G_VALUE_TYPE_NAME (value));
 
   *float_count = shader_float->size;
-  *floats = g_memdup (shader_float->value, shader_float->size * sizeof (float));
+  *floats = g_memdup2 (shader_float->value,
+                       shader_float->size * sizeof (float));
 
   return NULL;
 }
@@ -248,7 +249,7 @@ clutter_value_lcopy_shader_int (const GValue *value,
                             G_VALUE_TYPE_NAME (value));
 
   *int_count = shader_int->size;
-  *ints = g_memdup (shader_int->value, shader_int->size * sizeof (int));
+  *ints = g_memdup2 (shader_int->value, shader_int->size * sizeof (int));
 
   return NULL;
 }
@@ -339,8 +340,10 @@ clutter_value_lcopy_shader_matrix (const GValue *value,
                             G_VALUE_TYPE_NAME (value));
 
   *float_count = shader_float->size;
-  *floats = g_memdup (shader_float->value,
-                      shader_float->size * shader_float->size * sizeof (float));
+  *floats = g_memdup2 (shader_float->value,
+                       shader_float->size *
+                       shader_float->size *
+                       sizeof (float));
 
   return NULL;
 }
diff --git a/cogl/cogl/cogl-boxed-value.c b/cogl/cogl/cogl-boxed-value.c
index 9a9ba41c21..ef70997755 100644
--- a/cogl/cogl/cogl-boxed-value.c
+++ b/cogl/cogl/cogl-boxed-value.c
@@ -253,21 +253,21 @@ _cogl_boxed_value_copy (CoglBoxedValue *dst,
           break;
 
         case COGL_BOXED_INT:
-          dst->v.int_array = g_memdup (src->v.int_array,
-                                       src->size * src->count * sizeof (int));
+          dst->v.int_array = g_memdup2 (src->v.int_array,
+                                        src->size * src->count * sizeof (int));
           break;
 
         case COGL_BOXED_FLOAT:
-          dst->v.float_array = g_memdup (src->v.float_array,
-                                         src->size *
-                                         src->count *
-                                         sizeof (float));
+          dst->v.float_array = g_memdup2 (src->v.float_array,
+                                          src->size *
+                                          src->count *
+                                          sizeof (float));
           break;
 
         case COGL_BOXED_MATRIX:
-          dst->v.float_array = g_memdup (src->v.float_array,
-                                         src->size * src->size *
-                                         src->count * sizeof (float));
+          dst->v.float_array = g_memdup2 (src->v.float_array,
+                                          src->size * src->size *
+                                          src->count * sizeof (float));
           break;
         }
     }
diff --git a/meson.build b/meson.build
index 88780e43c4..78295bd9d8 100644
--- a/meson.build
+++ b/meson.build
@@ -13,7 +13,7 @@ libmutter_api_version = '@0@'.format(api_version)
 
 # generic version requirements
 fribidi_req = '>= 1.0.0'
-glib_req = '>= 2.61.1'
+glib_req = '>= 2.67.3'
 gi_req = '>= 0.9.5'
 graphene_req = '>= 1.10.2'
 gtk3_req = '>= 3.19.8'
diff --git a/src/backends/meta-monitor-config-manager.c b/src/backends/meta-monitor-config-manager.c
index fc7a8dd544..c37528b203 100644
--- a/src/backends/meta-monitor-config-manager.c
+++ b/src/backends/meta-monitor-config-manager.c
@@ -641,7 +641,7 @@ create_monitor_config (MetaMonitor     *monitor,
   monitor_config = g_new0 (MetaMonitorConfig, 1);
   *monitor_config = (MetaMonitorConfig) {
     .monitor_spec = meta_monitor_spec_clone (monitor_spec),
-    .mode_spec = g_memdup (mode_spec, sizeof (MetaMonitorModeSpec)),
+    .mode_spec = g_memdup2 (mode_spec, sizeof (MetaMonitorModeSpec)),
     .enable_underscanning = meta_monitor_is_underscanning (monitor)
   };
 
@@ -928,8 +928,8 @@ clone_monitor_config_list (GList *monitor_configs_in)
       monitor_config_out = g_new0 (MetaMonitorConfig, 1);
       *monitor_config_out = (MetaMonitorConfig) {
         .monitor_spec = meta_monitor_spec_clone (monitor_config_in->monitor_spec),
-        .mode_spec = g_memdup (monitor_config_in->mode_spec,
-                               sizeof (MetaMonitorModeSpec)),
+        .mode_spec = g_memdup2 (monitor_config_in->mode_spec,
+                                sizeof (MetaMonitorModeSpec)),
         .enable_underscanning = monitor_config_in->enable_underscanning
       };
       monitor_configs_out =
@@ -952,7 +952,8 @@ clone_logical_monitor_config_list (GList *logical_monitor_configs_in)
       logical_monitor_config_in = l->data;
 
       logical_monitor_config_out =
-        g_memdup (logical_monitor_config_in, sizeof (MetaLogicalMonitorConfig));
+        g_memdup2 (logical_monitor_config_in,
+                   sizeof (MetaLogicalMonitorConfig));
       logical_monitor_config_out->monitor_configs =
         clone_monitor_config_list (logical_monitor_config_in->monitor_configs);
 
diff --git a/src/backends/meta-monitor-manager-dummy.c b/src/backends/meta-monitor-manager-dummy.c
index 5cac68de4b..d08fb022bd 100644
--- a/src/backends/meta-monitor-manager-dummy.c
+++ b/src/backends/meta-monitor-manager-dummy.c
@@ -182,7 +182,7 @@ append_monitor (MetaMonitorManager *manager,
         {
           CrtcModeSpec *spec;
 
-          spec = g_memdup (&default_specs[i], sizeof (CrtcModeSpec));
+          spec = g_memdup2 (&default_specs[i], sizeof (CrtcModeSpec));
           mode_specs = g_list_prepend (mode_specs, spec);
         }
     }
diff --git a/src/backends/native/meta-input-settings-native.c 
b/src/backends/native/meta-input-settings-native.c
index ba6ec6d26c..552fa84a43 100644
--- a/src/backends/native/meta-input-settings-native.c
+++ b/src/backends/native/meta-input-settings-native.c
@@ -666,7 +666,7 @@ meta_input_settings_native_set_tablet_aspect_ratio (MetaInputSettings  *settings
 
   task = g_task_new (device, NULL, NULL, NULL);
   g_task_set_task_data (task,
-                        g_memdup (&aspect_ratio, sizeof (double)),
+                        g_memdup2 (&aspect_ratio, sizeof (double)),
                         g_free);
 
   input_settings_native = META_INPUT_SETTINGS_NATIVE (settings);
diff --git a/src/backends/native/meta-kms-crtc.c b/src/backends/native/meta-kms-crtc.c
index 9999cf6070..51d040b440 100644
--- a/src/backends/native/meta-kms-crtc.c
+++ b/src/backends/native/meta-kms-crtc.c
@@ -273,11 +273,11 @@ meta_kms_crtc_predict_state (MetaKmsCrtc   *crtc,
       clear_gamma_state (crtc);
       crtc->current_state.gamma.size = gamma->size;
       crtc->current_state.gamma.red =
-        g_memdup (gamma->red, gamma->size * sizeof (uint16_t));
+        g_memdup2 (gamma->red, gamma->size * sizeof (uint16_t));
       crtc->current_state.gamma.green =
-        g_memdup (gamma->green, gamma->size * sizeof (uint16_t));
+        g_memdup2 (gamma->green, gamma->size * sizeof (uint16_t));
       crtc->current_state.gamma.blue =
-        g_memdup (gamma->blue, gamma->size * sizeof (uint16_t));
+        g_memdup2 (gamma->blue, gamma->size * sizeof (uint16_t));
 
       break;
     }
diff --git a/src/backends/native/meta-kms-impl-device-simple.c 
b/src/backends/native/meta-kms-impl-device-simple.c
index 3ffda075a5..99c09a6877 100644
--- a/src/backends/native/meta-kms-impl-device-simple.c
+++ b/src/backends/native/meta-kms-impl-device-simple.c
@@ -263,7 +263,7 @@ cached_mode_set_new (GList                 *connectors,
   cached_mode_set = g_new0 (CachedModeSet, 1);
   *cached_mode_set = (CachedModeSet) {
     .connectors = g_list_copy (connectors),
-    .drm_mode = g_memdup (drm_mode, sizeof *drm_mode),
+    .drm_mode = g_memdup2 (drm_mode, sizeof *drm_mode),
   };
 
   return cached_mode_set;
diff --git a/src/backends/native/meta-kms-update.c b/src/backends/native/meta-kms-update.c
index 175461126c..06d6d75155 100644
--- a/src/backends/native/meta-kms-update.c
+++ b/src/backends/native/meta-kms-update.c
@@ -364,9 +364,9 @@ meta_kms_crtc_gamma_new (MetaKmsCrtc    *crtc,
   *gamma = (MetaKmsCrtcGamma) {
     .crtc = crtc,
     .size = size,
-    .red = g_memdup (red, size * sizeof (*red)),
-    .green = g_memdup (green, size * sizeof (*green)),
-    .blue = g_memdup (blue, size * sizeof (*blue)),
+    .red = g_memdup2 (red, size * sizeof (*red)),
+    .green = g_memdup2 (green, size * sizeof (*green)),
+    .blue = g_memdup2 (blue, size * sizeof (*blue)),
   };
 
   return gamma;
diff --git a/src/backends/native/meta-monitor-manager-kms.c b/src/backends/native/meta-monitor-manager-kms.c
index cabaa3201c..6b952035ad 100644
--- a/src/backends/native/meta-monitor-manager-kms.c
+++ b/src/backends/native/meta-monitor-manager-kms.c
@@ -353,9 +353,9 @@ meta_monitor_manager_kms_get_crtc_gamma (MetaMonitorManager  *manager,
   crtc_state = meta_kms_crtc_get_current_state (kms_crtc);
 
   *size = crtc_state->gamma.size;
-  *red = g_memdup (crtc_state->gamma.red, *size * sizeof **red);
-  *green = g_memdup (crtc_state->gamma.green, *size * sizeof **green);
-  *blue = g_memdup (crtc_state->gamma.blue, *size * sizeof **blue);
+  *red = g_memdup2 (crtc_state->gamma.red, *size * sizeof **red);
+  *green = g_memdup2 (crtc_state->gamma.green, *size * sizeof **green);
+  *blue = g_memdup2 (crtc_state->gamma.blue, *size * sizeof **blue);
 }
 
 static char *
diff --git a/src/backends/x11/meta-monitor-manager-xrandr.c b/src/backends/x11/meta-monitor-manager-xrandr.c
index 31eac6fb18..c49be6c13d 100644
--- a/src/backends/x11/meta-monitor-manager-xrandr.c
+++ b/src/backends/x11/meta-monitor-manager-xrandr.c
@@ -677,9 +677,9 @@ meta_monitor_manager_xrandr_get_crtc_gamma (MetaMonitorManager  *manager,
                            (XID) meta_crtc_get_id (crtc));
 
   *size = gamma->size;
-  *red = g_memdup (gamma->red, sizeof (unsigned short) * gamma->size);
-  *green = g_memdup (gamma->green, sizeof (unsigned short) * gamma->size);
-  *blue = g_memdup (gamma->blue, sizeof (unsigned short) * gamma->size);
+  *red = g_memdup2 (gamma->red, sizeof (unsigned short) * gamma->size);
+  *green = g_memdup2 (gamma->green, sizeof (unsigned short) * gamma->size);
+  *blue = g_memdup2 (gamma->blue, sizeof (unsigned short) * gamma->size);
 
   XRRFreeGamma (gamma);
 }
diff --git a/src/backends/x11/meta-output-xrandr.c b/src/backends/x11/meta-output-xrandr.c
index 62ad086235..6cf82f2ecf 100644
--- a/src/backends/x11/meta-output-xrandr.c
+++ b/src/backends/x11/meta-output-xrandr.c
@@ -476,7 +476,7 @@ get_edid_property (Display  *xdisplay,
 
   if (actual_type == XA_INTEGER && actual_format == 8)
     {
-      result = g_memdup (prop, nitems);
+      result = g_memdup2 (prop, nitems);
       if (len)
         *len = nitems;
     }
diff --git a/src/core/boxes.c b/src/core/boxes.c
index ac8f2a9fe5..9a9633e052 100644
--- a/src/core/boxes.c
+++ b/src/core/boxes.c
@@ -46,7 +46,7 @@
 MetaRectangle *
 meta_rectangle_copy (const MetaRectangle *rect)
 {
-  return g_memdup (rect, sizeof (MetaRectangle));
+  return g_memdup2 (rect, sizeof (MetaRectangle));
 }
 
 void
diff --git a/src/core/display.c b/src/core/display.c
index 0fa2516a90..5aac698a32 100644
--- a/src/core/display.c
+++ b/src/core/display.c
@@ -2549,7 +2549,7 @@ meta_display_manage_all_xwindows (MetaDisplay *display)
   meta_stack_tracker_get_stack (display->stack_tracker, &_children, &n_children);
 
   /* Copy the stack as it will be modified as part of the loop */
-  children = g_memdup (_children, sizeof (guint64) * n_children);
+  children = g_memdup2 (_children, sizeof (uint64_t) * n_children);
 
   for (i = 0; i < n_children; ++i)
     {
diff --git a/src/core/keybindings.c b/src/core/keybindings.c
index 3d9591964f..8e752a9793 100644
--- a/src/core/keybindings.c
+++ b/src/core/keybindings.c
@@ -95,8 +95,8 @@ resolved_key_combo_copy (MetaResolvedKeyCombo *from,
                          MetaResolvedKeyCombo *to)
 {
   to->len = from->len;
-  to->keycodes = g_memdup (from->keycodes,
-                           from->len * sizeof (xkb_keycode_t));
+  to->keycodes = g_memdup2 (from->keycodes,
+                            from->len * sizeof (xkb_keycode_t));
 }
 
 static gboolean
diff --git a/src/core/workspace.c b/src/core/workspace.c
index 002ee8e566..b03c8b92e6 100644
--- a/src/core/workspace.c
+++ b/src/core/workspace.c
@@ -791,7 +791,7 @@ meta_workspace_invalidate_work_area (MetaWorkspace *workspace)
 static MetaStrut *
 copy_strut(MetaStrut *original)
 {
-  return g_memdup(original, sizeof(MetaStrut));
+  return g_memdup2 (original, sizeof (MetaStrut));
 }
 
 static GSList *
diff --git a/src/x11/window-props.c b/src/x11/window-props.c
index 235a5d59f4..1d8cf63a62 100644
--- a/src/x11/window-props.c
+++ b/src/x11/window-props.c
@@ -1962,9 +1962,11 @@ meta_x11_display_init_window_prop_hooks (MetaX11Display *x11_display)
     { x11_display->atom__NET_WM_WINDOW_OPACITY, META_PROP_VALUE_CARDINAL, reload_window_opacity, LOAD_INIT | 
INCLUDE_OR },
     { 0 },
   };
+  MetaWindowPropHooks *table;
+  MetaWindowPropHooks *cursor;
 
-  MetaWindowPropHooks *table = g_memdup (hooks, sizeof (hooks)),
-    *cursor = table;
+  table = g_memdup2 (hooks, sizeof (hooks)),
+  cursor = table;
 
   g_assert (x11_display->prop_hooks == NULL);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]