[NTOS]: Fixes & improvements:

- Fix a bug in CmpInitHiveFromFile(): correctly set the MaximumLength value of NewHive->FileFullPath;
- Code simplification: no need to use temporary ANSI string operations followed by UNICODE conversion, when one can do the same thing directly in UNICODE.
- Use NULL for NULL handles.

svn path=/trunk/; revision=75142
This commit is contained in:
Hermès Bélusca-Maïto 2017-06-19 20:07:15 +00:00
parent f4518d125d
commit 38274203e7

View file

@ -384,7 +384,7 @@ CmpInitHiveFromFile(IN PCUNICODE_STRING HiveName,
HiveName->Buffer, HiveName->Buffer,
HiveName->Length); HiveName->Length);
NewHive->FileFullPath.Length = HiveName->Length; NewHive->FileFullPath.Length = HiveName->Length;
NewHive->FileFullPath.MaximumLength = HiveName->MaximumLength; NewHive->FileFullPath.MaximumLength = HiveName->Length;
} }
/* Return success */ /* Return success */
@ -529,12 +529,10 @@ CmpCreateControlSet(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
OBJECT_ATTRIBUTES ObjectAttributes; OBJECT_ATTRIBUTES ObjectAttributes;
CHAR ValueInfoBuffer[128]; CHAR ValueInfoBuffer[128];
PKEY_VALUE_FULL_INFORMATION ValueInfo; PKEY_VALUE_FULL_INFORMATION ValueInfo;
CHAR Buffer[128];
WCHAR UnicodeBuffer[128]; WCHAR UnicodeBuffer[128];
HANDLE SelectHandle, KeyHandle, ConfigHandle = NULL, ProfileHandle = NULL; HANDLE SelectHandle, KeyHandle, ConfigHandle = NULL, ProfileHandle = NULL;
HANDLE ParentHandle = NULL; HANDLE ParentHandle = NULL;
ULONG ControlSet, HwProfile; ULONG ControlSet, HwProfile;
ANSI_STRING TempString;
NTSTATUS Status; NTSTATUS Status;
ULONG ResultLength, Disposition; ULONG ResultLength, Disposition;
PLOADER_PARAMETER_EXTENSION LoaderExtension; PLOADER_PARAMETER_EXTENSION LoaderExtension;
@ -622,16 +620,11 @@ UseSet:
/* Sanity check */ /* Sanity check */
ASSERT(Disposition == REG_CREATED_NEW_KEY); ASSERT(Disposition == REG_CREATED_NEW_KEY);
/* Initialize the symbolic link name */ /* Initialize the target link name */
sprintf(Buffer, swprintf(UnicodeBuffer,
"\\Registry\\Machine\\System\\ControlSet%03ld", L"\\Registry\\Machine\\System\\ControlSet%03ld",
ControlSet); ControlSet);
RtlInitAnsiString(&TempString, Buffer); RtlInitUnicodeString(&KeyName, UnicodeBuffer);
/* Create a Unicode string out of it */
KeyName.MaximumLength = sizeof(UnicodeBuffer);
KeyName.Buffer = UnicodeBuffer;
Status = RtlAnsiStringToUnicodeString(&KeyName, &TempString, FALSE);
/* Set the value */ /* Set the value */
Status = NtSetValueKey(KeyHandle, Status = NtSetValueKey(KeyHandle,
@ -655,7 +648,7 @@ UseSet:
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
/* Cleanup and exit */ /* Cleanup and exit */
ConfigHandle = 0; ConfigHandle = NULL;
goto Cleanup; goto Cleanup;
} }
@ -698,21 +691,13 @@ UseSet:
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
/* Exit and clean up */ /* Exit and clean up */
ParentHandle = 0; ParentHandle = NULL;
goto Cleanup; goto Cleanup;
} }
/* Build the profile name */ /* Build the profile name */
sprintf(Buffer, "%04ld", HwProfile); swprintf(UnicodeBuffer, L"%04ld", HwProfile);
RtlInitAnsiString(&TempString, Buffer); RtlInitUnicodeString(&KeyName, UnicodeBuffer);
/* Convert it to Unicode */
KeyName.MaximumLength = sizeof(UnicodeBuffer);
KeyName.Buffer = UnicodeBuffer;
Status = RtlAnsiStringToUnicodeString(&KeyName,
&TempString,
FALSE);
ASSERT(Status == STATUS_SUCCESS);
/* Open the associated key */ /* Open the associated key */
InitializeObjectAttributes(&ObjectAttributes, InitializeObjectAttributes(&ObjectAttributes,
@ -759,19 +744,11 @@ UseSet:
ASSERT(Disposition == REG_CREATED_NEW_KEY); ASSERT(Disposition == REG_CREATED_NEW_KEY);
/* Create the profile name */ /* Create the profile name */
sprintf(Buffer, swprintf(UnicodeBuffer,
"\\Registry\\Machine\\System\\CurrentControlSet\\" L"\\Registry\\Machine\\System\\CurrentControlSet\\"
"Hardware Profiles\\%04ld", L"Hardware Profiles\\%04ld",
HwProfile); HwProfile);
RtlInitAnsiString(&TempString, Buffer); RtlInitUnicodeString(&KeyName, UnicodeBuffer);
/* Convert it to Unicode */
KeyName.MaximumLength = sizeof(UnicodeBuffer);
KeyName.Buffer = UnicodeBuffer;
Status = RtlAnsiStringToUnicodeString(&KeyName,
&TempString,
FALSE);
ASSERT(STATUS_SUCCESS == Status);
/* Set it */ /* Set it */
Status = NtSetValueKey(KeyHandle, Status = NtSetValueKey(KeyHandle,