fixed returned status code in RtlAnsiStringToUnicodeString and properly free allocate...
[reactos.git] / reactos / lib / rtl / unicode.c
index 04ce389..9b29db3 100644 (file)
@@ -75,15 +75,15 @@ RtlAnsiStringToUnicodeString(
     if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
     UniDest->Length = (USHORT)Length - sizeof(WCHAR);
 
-    if (AllocateDestinationString == TRUE)
+    if (AllocateDestinationString)
     {
         UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
         UniDest->MaximumLength = Length;
         if (!UniDest->Buffer) return STATUS_NO_MEMORY;
     }
-    else if (Length >= UniDest->MaximumLength)
+    else if (UniDest->Length >= UniDest->MaximumLength)
     {
-        return STATUS_BUFFER_TOO_SMALL;
+        return STATUS_BUFFER_OVERFLOW;
     }
 
     Status = RtlMultiByteToUnicodeN(UniDest->Buffer,
@@ -92,9 +92,13 @@ RtlAnsiStringToUnicodeString(
                                     AnsiSource->Buffer,
                                     AnsiSource->Length);
 
-    if (!NT_SUCCESS(Status) && AllocateDestinationString)
+    if (!NT_SUCCESS(Status))
     {
-        RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
+        if (AllocateDestinationString)
+        {
+            RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
+            UniDest->Buffer = NULL;
+        }
         return Status;
     }
 
@@ -1770,7 +1774,7 @@ NTAPI
 RtlStringFromGUID (IN REFGUID Guid,
                    OUT PUNICODE_STRING GuidString)
 {
-   STATIC CONST PWCHAR Hex = L"0123456789ABCDEF";
+   static CONST PWCHAR Hex = L"0123456789ABCDEF";
    WCHAR Buffer[40];
    PWCHAR BufferPtr;
    ULONG i;
@@ -1943,6 +1947,8 @@ RtlCreateUnicodeString(
     PAGED_CODE_RTL();
 
     Length = (wcslen(Source) + 1) * sizeof(WCHAR);
+    if (Length > 0xFFFE) return FALSE;
+
     UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
 
     if (UniDest->Buffer == NULL) return FALSE;