[Spice-devel] [vdagent-win PATCH v4 07/19] Allow one more character reading strings from registry

Frediano Ziglio fziglio at redhat.com
Mon Jul 2 07:43:46 UTC 2018


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>
---
 vdagent/display_setting.cpp | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/vdagent/display_setting.cpp b/vdagent/display_setting.cpp
index b68ef1c..4b47276 100644
--- a/vdagent/display_setting.cpp
+++ b/vdagent/display_setting.cpp
@@ -295,6 +295,20 @@ static bool RegReadString(HKEY key, const TCHAR *name, TCHAR *buffer, size_t buf
     LONG status;
 
     status = RegQueryValueEx(key, name, NULL, &value_type, (LPBYTE)buffer, &value_size);
+    if (status == ERROR_SUCCESS && value_type == REG_SZ) {
+        // assure 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;
 }
 
-- 
2.17.1



More information about the Spice-devel mailing list