[PATCH] cmd: Fixed replace_escaped_spaces to handle non-nul terminated string

Marcus Meissner meissner at suse.de
Mon Jul 4 09:48:38 CDT 2011


Hi,

the routine did not handle the fact that the input data is not
\0 terminated correctly. Or at least just halfway.

Ciao, Marcus
---
 programs/cmd/tests/batch.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/programs/cmd/tests/batch.c b/programs/cmd/tests/batch.c
index 3c12fe7..664ef1e 100644
--- a/programs/cmd/tests/batch.c
+++ b/programs/cmd/tests/batch.c
@@ -30,16 +30,27 @@ static const char* replace_escaped_spaces(const char *data, DWORD size, DWORD *n
 {
     static const char escaped_space[] = {'@','s','p','a','c','e','@','\0'};
     const char *a, *b;
-    char *new_data;
+    char *new_data, *old_data;
     DWORD len_space = sizeof(escaped_space) -1;
 
-    a = b = data;
     *new_size = 0;
 
-    new_data = HeapAlloc(GetProcessHeap(), 0, size*sizeof(char));
+    /* Convert data to a \0 terminated string for easier handling */
+    old_data = HeapAlloc(GetProcessHeap(), 0, (size+1)*sizeof(char));
+    ok(old_data != NULL, "HeapAlloc failed\n");
+    if(!old_data)
+        return NULL;
+    memcpy(old_data, data, size);
+    old_data[size] = '\0';
+    a = b = old_data;
+
+    /* size+1 is sufficient, as we shrink the string (or keep it same + \0) */
+    new_data = HeapAlloc(GetProcessHeap(), 0, (size+1)*sizeof(char));
     ok(new_data != NULL, "HeapAlloc failed\n");
-    if(!new_data)
+    if(!new_data) {
+        HeapFree(GetProcessHeap(), 0, old_data);
         return NULL;
+    }
 
     while( (b = strstr(a, escaped_space)) )
     {
@@ -52,6 +63,7 @@ static const char* replace_escaped_spaces(const char *data, DWORD size, DWORD *n
     strncpy(new_data + *new_size, a, strlen(a) + 1);
     *new_size += strlen(a);
 
+    HeapFree(GetProcessHeap(), 0, old_data);
     return new_data;
 }
 
-- 
1.7.1




More information about the wine-patches mailing list