From e5f2ed4c66cb8188b9bcc20ad5345b91803da330 Mon Sep 17 00:00:00 2001
From: Mike McCormack <mike@codeweavers.com>
Date: Wed, 26 Jan 2005 20:39:54 +0000
Subject: [PATCH] Fix MsiRecordSetString for NULL strings and update test case.

---
 dlls/msi/record.c       | 39 ++++++++++++++++++++++++++++-----------
 dlls/msi/tests/record.c |  4 ++++
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/dlls/msi/record.c b/dlls/msi/record.c
index ac5487170fc..cccbbfdcc16 100644
--- a/dlls/msi/record.c
+++ b/dlls/msi/record.c
@@ -454,12 +454,20 @@ UINT MSI_RecordSetStringA( MSIRECORD *rec, unsigned int iField, LPCSTR szValue )
     if( iField > rec->count )
         return ERROR_INVALID_FIELD;
 
-    len = MultiByteToWideChar( CP_ACP, 0, szValue, -1, NULL, 0 );
-    str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
-    MultiByteToWideChar( CP_ACP, 0, szValue, -1, str, len );
     MSI_FreeField( &rec->fields[iField] );
-    rec->fields[iField].type = MSIFIELD_WSTR;
-    rec->fields[iField].u.szwVal = str;
+    if( szValue )
+    {
+        len = MultiByteToWideChar( CP_ACP, 0, szValue, -1, NULL, 0 );
+        str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
+        MultiByteToWideChar( CP_ACP, 0, szValue, -1, str, len );
+        rec->fields[iField].type = MSIFIELD_WSTR;
+        rec->fields[iField].u.szwVal = str;
+    }
+    else
+    {
+        rec->fields[iField].type = MSIFIELD_NULL;
+        rec->fields[iField].u.szwVal = NULL;
+    }
 
     return 0;
 }
@@ -491,13 +499,22 @@ UINT MSI_RecordSetStringW( MSIRECORD *rec, unsigned int iField, LPCWSTR szValue
     if( iField > rec->count )
         return ERROR_INVALID_FIELD;
 
-    len = lstrlenW(szValue) + 1;
-    str = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR));
-    lstrcpyW( str, szValue );
-
     MSI_FreeField( &rec->fields[iField] );
-    rec->fields[iField].type = MSIFIELD_WSTR;
-    rec->fields[iField].u.szwVal = str;
+
+    if( szValue )
+    {
+        len = lstrlenW(szValue) + 1;
+        str = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR));
+        lstrcpyW( str, szValue );
+
+        rec->fields[iField].type = MSIFIELD_WSTR;
+        rec->fields[iField].u.szwVal = str;
+    }
+    else
+    {
+        rec->fields[iField].type = MSIFIELD_NULL;
+        rec->fields[iField].u.szwVal = NULL;
+    }
 
     return 0;
 }
diff --git a/dlls/msi/tests/record.c b/dlls/msi/tests/record.c
index de26e9d86c2..d0054dc23f6 100644
--- a/dlls/msi/tests/record.c
+++ b/dlls/msi/tests/record.c
@@ -119,6 +119,10 @@ void test_msirecord(void)
     ok(r == 1, "failed to get integer\n");
 
     /* same record, but add a string to it */
+    r = MsiRecordSetString(h, 0, NULL);
+    ok(r == ERROR_SUCCESS, "Failed to set null string at 0\n");
+    r = MsiRecordIsNull(h, 0);
+    ok(r == TRUE, "null string not null field\n");
     r = MsiRecordSetString(h,0,str);
     ok(r == ERROR_SUCCESS, "Failed to set string at 0\n");
     r = MsiRecordGetInteger(h, 0);
-- 
GitLab