[Spice-commits] 3 commits - vdagent/display_setting.cpp

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Jul 5 13:13:29 UTC 2018


 vdagent/display_setting.cpp |  103 ++++++++++++++++++++++----------------------
 1 file changed, 52 insertions(+), 51 deletions(-)

New commits:
commit 7b368a134fac63f4e603969694b83c916df04e17
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Sat May 26 07:55:12 2018 +0100

    Allow one more character reading strings from registry
    
    The strings in the registry are usually NUL-terminated but this
    is not a requirement.
    Handle the case when the string, considering the terminator, fit
    into the reading buffer. In this case accept the string. In the
    case the string fit into the buffer but is not terminated
    returns ERROR_MORE_DATA (the error that would be returned if the
    string didn't fit in the buffer as there is no place to add the
    terminator).
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Jonathon Jongsma <jjongsma at redhat.com>

diff --git a/vdagent/display_setting.cpp b/vdagent/display_setting.cpp
index 78c67d6..b6711d7 100644
--- a/vdagent/display_setting.cpp
+++ b/vdagent/display_setting.cpp
@@ -290,11 +290,25 @@ bool DisplaySetting::disable_wallpaper()
 
 static bool RegReadString(HKEY key, const TCHAR *name, TCHAR *buffer, size_t buffer_len)
 {
-    DWORD value_size = (buffer_len - 1) * sizeof(buffer[0]);
+    DWORD value_size = buffer_len * sizeof(buffer[0]);
     DWORD value_type;
     LONG status;
 
     status = RegQueryValueEx(key, name, NULL, &value_type, (LPBYTE)buffer, &value_size);
+    if (status == ERROR_SUCCESS && value_type == REG_SZ) {
+        // ensure NUL-terminated
+        value_size /= sizeof(buffer[0]);
+        if (value_size == buffer_len) {
+            // full buffer but not terminated?
+            if (buffer[value_size-1] != '\0') {
+                status = ERROR_MORE_DATA;
+            }
+        } else {
+            // append a NUL. If there's already a NUL character this
+            // new one will be ignored
+            buffer[value_size] = '\0';
+        }
+    }
     if (status != ERROR_SUCCESS) {
         vd_printf("RegQueryValueEx(%" PRIsTSTR ") : fail %ld", name, status);
         return false;
@@ -305,12 +319,6 @@ static bool RegReadString(HKEY key, const TCHAR *name, TCHAR *buffer, size_t buf
         return false;
     }
 
-    // assure NUL-terminated
-    value_size /= sizeof(buffer[0]);
-    if (!value_size || buffer[value_size - 1] != '\0') {
-        buffer[value_size] = '\0';
-    }
-
     return true;
 }
 
commit 1a2e2d1411c131c63703be04a82522462c89918a
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Sat May 26 07:55:12 2018 +0100

    Factor out an utility function to read strings from registry
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Jonathon Jongsma <jjongsma at redhat.com>

diff --git a/vdagent/display_setting.cpp b/vdagent/display_setting.cpp
index cef3401..78c67d6 100644
--- a/vdagent/display_setting.cpp
+++ b/vdagent/display_setting.cpp
@@ -282,30 +282,52 @@ bool DisplaySetting::disable_wallpaper()
     }
 }
 
-bool DisplaySetting::reload_wallpaper(HKEY desktop_reg_key)
+#if defined(UNICODE) || defined(_UNICODE)
+#define PRIsTSTR "ls"
+#else
+#define PRIsTSTR "s"
+#endif
+
+static bool RegReadString(HKEY key, const TCHAR *name, TCHAR *buffer, size_t buffer_len)
 {
-    TCHAR wallpaper_path[MAX_PATH + 1];
-    DWORD value_size = sizeof(wallpaper_path) - sizeof(wallpaper_path[0]);
+    DWORD value_size = (buffer_len - 1) * sizeof(buffer[0]);
     DWORD value_type;
     LONG status;
-    TCHAR cur_wallpaper[MAX_PATH + 1];
 
-    vd_printf("");
-    status = RegQueryValueEx(desktop_reg_key, TEXT("Wallpaper"), NULL,
-                             &value_type, (LPBYTE)wallpaper_path, &value_size);
+    status = RegQueryValueEx(key, name, NULL, &value_type, (LPBYTE)buffer, &value_size);
     if (status != ERROR_SUCCESS) {
-        vd_printf("RegQueryValueEx(Wallpaper) : fail %ld", status);
+        vd_printf("RegQueryValueEx(%" PRIsTSTR ") : fail %ld", name, status);
         return false;
     }
 
     if (value_type != REG_SZ) {
-        vd_printf("bad wallpaper value type %lu (expected REG_SZ)", value_type);
+        vd_printf("bad %" PRIsTSTR " value type %lu (expected REG_SZ)", name, value_type);
         return false;
     }
 
-    value_size /= sizeof(wallpaper_path[0]);
-    if (!value_size || wallpaper_path[value_size - 1] != '\0') {
-        wallpaper_path[value_size] = '\0';
+    // assure NUL-terminated
+    value_size /= sizeof(buffer[0]);
+    if (!value_size || buffer[value_size - 1] != '\0') {
+        buffer[value_size] = '\0';
+    }
+
+    return true;
+}
+
+template <size_t N>
+static inline bool RegReadString(HKEY key, const TCHAR *name, TCHAR (&buffer)[N])
+{
+    return RegReadString(key, name, buffer, N);
+}
+
+bool DisplaySetting::reload_wallpaper(HKEY desktop_reg_key)
+{
+    TCHAR wallpaper_path[MAX_PATH + 1];
+    TCHAR cur_wallpaper[MAX_PATH + 1];
+
+    vd_printf("");
+    if (!RegReadString(desktop_reg_key, TEXT("Wallpaper"), wallpaper_path)) {
+        return false;
     }
 
     if (SystemParametersInfo(SPI_GETDESKWALLPAPER, SPICE_N_ELEMENTS(cur_wallpaper), cur_wallpaper, 0)) {
@@ -340,29 +362,13 @@ bool DisplaySetting::disable_font_smoothing()
 bool DisplaySetting::reload_font_smoothing(HKEY desktop_reg_key)
 {
     TCHAR smooth_value[4];
-    DWORD value_size = sizeof(smooth_value)-sizeof(smooth_value[0]);
-    DWORD value_type;
-    LONG status;
     BOOL cur_font_smooth;
 
     vd_printf("");
-    status = RegQueryValueEx(desktop_reg_key, TEXT("FontSmoothing"), NULL,
-                             &value_type, (LPBYTE)smooth_value, &value_size);
-    if (status != ERROR_SUCCESS) {
-        vd_printf("RegQueryValueEx(FontSmoothing) : fail %ld", status);
+    if (!RegReadString(desktop_reg_key, TEXT("FontSmoothing"), smooth_value)) {
         return false;
     }
 
-    if (value_type != REG_SZ) {
-        vd_printf("bad font smoothing value type %lu (expected REG_SZ)", value_type);
-        return false;
-    }
-
-    value_size /= sizeof(smooth_value[0]);
-    if (!value_size || smooth_value[value_size - 1] != '\0') {
-        smooth_value[value_size] = '\0';
-    }
-
     if (_tcscmp(smooth_value, TEXT("0")) == 0) {
         vd_printf("font smoothing is disabled in registry. do nothing");
         return true;
@@ -414,8 +420,6 @@ bool DisplaySetting::reload_win_animation(HKEY desktop_reg_key)
 {
     HKEY win_metrics_hkey;
     TCHAR win_anim_value[4];
-    DWORD value_size = sizeof(win_anim_value)-sizeof(win_anim_value[0]);
-    DWORD value_type;
     LONG status;
     ANIMATIONINFO active_win_animation;
 
@@ -428,26 +432,13 @@ bool DisplaySetting::reload_win_animation(HKEY desktop_reg_key)
         return false;
     }
 
-    status = RegQueryValueEx(win_metrics_hkey, TEXT("MinAnimate"), NULL,
-                             &value_type, (LPBYTE)win_anim_value, &value_size);
-    if (status != ERROR_SUCCESS) {
-        vd_printf("RegQueryValueEx(MinAnimate) : fail %ld", status);
+    if (!RegReadString(win_metrics_hkey, TEXT("MinAnimate"), win_anim_value)) {
         RegCloseKey(win_metrics_hkey);
         return false;
     }
 
     RegCloseKey(win_metrics_hkey);
 
-    if (value_type != REG_SZ) {
-        vd_printf("bad MinAnimate value type %lu (expected REG_SZ)", value_type);
-        return false;
-    }
-
-    value_size /= sizeof(win_anim_value[0]);
-    if (!value_size || win_anim_value[value_size - 1] != '\0') {
-        win_anim_value[value_size] = '\0';
-    }
-
     if (!_tcscmp(win_anim_value, TEXT("0"))) {
         vd_printf("window animation is disabled in registry. do nothing");
         return true;
commit 97ef38dc6d69a14365b1245e9a6406105a033a8a
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Mon May 28 16:04:34 2018 +0100

    Use always TCHAR to read string from registry
    
    This is a preparation patch in order to factor out code to read
    a string from registry.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Jonathon Jongsma <jjongsma at redhat.com>

diff --git a/vdagent/display_setting.cpp b/vdagent/display_setting.cpp
index 2b22144..cef3401 100644
--- a/vdagent/display_setting.cpp
+++ b/vdagent/display_setting.cpp
@@ -339,15 +339,15 @@ bool DisplaySetting::disable_font_smoothing()
 
 bool DisplaySetting::reload_font_smoothing(HKEY desktop_reg_key)
 {
-    CHAR smooth_value[4];
-    DWORD value_size = sizeof(smooth_value)-1;
+    TCHAR smooth_value[4];
+    DWORD value_size = sizeof(smooth_value)-sizeof(smooth_value[0]);
     DWORD value_type;
     LONG status;
     BOOL cur_font_smooth;
 
     vd_printf("");
-    status = RegQueryValueExA(desktop_reg_key, "FontSmoothing", NULL,
-                              &value_type, (LPBYTE)smooth_value, &value_size);
+    status = RegQueryValueEx(desktop_reg_key, TEXT("FontSmoothing"), NULL,
+                             &value_type, (LPBYTE)smooth_value, &value_size);
     if (status != ERROR_SUCCESS) {
         vd_printf("RegQueryValueEx(FontSmoothing) : fail %ld", status);
         return false;
@@ -358,15 +358,16 @@ bool DisplaySetting::reload_font_smoothing(HKEY desktop_reg_key)
         return false;
     }
 
+    value_size /= sizeof(smooth_value[0]);
     if (!value_size || smooth_value[value_size - 1] != '\0') {
         smooth_value[value_size] = '\0';
     }
 
-    if (strcmp(smooth_value, "0") == 0) {
+    if (_tcscmp(smooth_value, TEXT("0")) == 0) {
         vd_printf("font smoothing is disabled in registry. do nothing");
         return true;
-    } else if (strcmp(smooth_value, "2") != 0) {
-        vd_printf("unexpectd font smoothing value %s", smooth_value);
+    } else if (_tcscmp(smooth_value, TEXT("2")) != 0) {
+        vd_printf("unexpectd font smoothing value %ls", smooth_value);
         return false;
     }
 
@@ -412,8 +413,8 @@ bool DisplaySetting::disable_animation()
 bool DisplaySetting::reload_win_animation(HKEY desktop_reg_key)
 {
     HKEY win_metrics_hkey;
-    CHAR win_anim_value[4];
-    DWORD value_size = sizeof(win_anim_value)-1;
+    TCHAR win_anim_value[4];
+    DWORD value_size = sizeof(win_anim_value)-sizeof(win_anim_value[0]);
     DWORD value_type;
     LONG status;
     ANIMATIONINFO active_win_animation;
@@ -427,8 +428,8 @@ bool DisplaySetting::reload_win_animation(HKEY desktop_reg_key)
         return false;
     }
 
-    status = RegQueryValueExA(win_metrics_hkey, "MinAnimate", NULL,
-                              &value_type, (LPBYTE)win_anim_value, &value_size);
+    status = RegQueryValueEx(win_metrics_hkey, TEXT("MinAnimate"), NULL,
+                             &value_type, (LPBYTE)win_anim_value, &value_size);
     if (status != ERROR_SUCCESS) {
         vd_printf("RegQueryValueEx(MinAnimate) : fail %ld", status);
         RegCloseKey(win_metrics_hkey);
@@ -442,15 +443,16 @@ bool DisplaySetting::reload_win_animation(HKEY desktop_reg_key)
         return false;
     }
 
+    value_size /= sizeof(win_anim_value[0]);
     if (!value_size || win_anim_value[value_size - 1] != '\0') {
         win_anim_value[value_size] = '\0';
     }
 
-    if (!strcmp(win_anim_value, "0")) {
+    if (!_tcscmp(win_anim_value, TEXT("0"))) {
         vd_printf("window animation is disabled in registry. do nothing");
         return true;
-    }  else if (strcmp(win_anim_value, "1") != 0) {
-        vd_printf("unexpectd window animation value %s", win_anim_value);
+    }  else if (_tcscmp(win_anim_value, TEXT("1")) != 0) {
+        vd_printf("unexpectd window animation value %ls", win_anim_value);
         return false;
     }
     active_win_animation.cbSize = sizeof(ANIMATIONINFO);


More information about the Spice-commits mailing list