diff options
author | 2021-10-12 17:57:10 +0000 | |
---|---|---|
committer | 2021-10-12 17:57:10 +0000 | |
commit | a409cc9e657d8d1642b7d20c8e48725001de9b79 (patch) | |
tree | 9392d301a6da3bdd3f132d4ce567eac69965a07a | |
parent | version: bump (diff) | |
download | wireguard-nt-a409cc9e657d8d1642b7d20c8e48725001de9b79.tar.xz wireguard-nt-a409cc9e657d8d1642b7d20c8e48725001de9b79.zip |
api: driver: don't allocate for instance ID
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | api/driver.c | 40 | ||||
-rw-r--r-- | api/wireguard.h | 2 |
2 files changed, 11 insertions, 31 deletions
diff --git a/api/driver.c b/api/driver.c index 818e26d..78c7f10 100644 --- a/api/driver.c +++ b/api/driver.c @@ -70,27 +70,18 @@ SnapshotConfigurationAndState( 0); DWORD LastError = ERROR_SUCCESS; - DWORD RequiredBytes; - if (SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, NULL, 0, &RequiredBytes) || - (LastError = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) - { - LOG_ERROR(LastError, L"Failed to query adapter \"%s\" instance ID size", Name); - return FALSE; - } - LastError = ERROR_SUCCESS; - LPWSTR InstanceId = ZallocArray(RequiredBytes, sizeof(*InstanceId)); - if (!InstanceId) - return FALSE; + WCHAR InstanceId[MAX_INSTANCE_ID]; + DWORD RequiredBytes = _countof(InstanceId); if (!SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, InstanceId, RequiredBytes, &RequiredBytes)) { LastError = LOG_LAST_ERROR(L"Failed to get adapter \"%s\" instance ID", Name); - goto cleanupInstanceId; + goto cleanup; } HANDLE NdisHandle = OpenDeviceObject(InstanceId); if (NdisHandle == INVALID_HANDLE_VALUE) { LastError = LOG(WIREGUARD_LOG_ERR, L"Failed to get adapter \"%s\" object", Name); - goto cleanupInstanceId; + goto cleanup; } WG_IOCTL_ADAPTER_STATE Op = WG_IOCTL_ADAPTER_STATE_QUERY; if (!DeviceIoControl( @@ -120,8 +111,7 @@ SnapshotConfigurationAndState( } cleanupHandle: CloseHandle(NdisHandle); -cleanupInstanceId: - Free(InstanceId); +cleanup: return RET_ERROR(TRUE, LastError); } @@ -147,27 +137,18 @@ RestoreConfigurationAndState( 0); DWORD LastError = ERROR_SUCCESS; - DWORD RequiredBytes; - if (SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, NULL, 0, &RequiredBytes) || - (LastError = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) - { - LOG_ERROR(LastError, L"Failed to query adapter \"%s\" instance ID size", Name); - return FALSE; - } - LastError = ERROR_SUCCESS; - LPWSTR InstanceId = ZallocArray(RequiredBytes, sizeof(*InstanceId)); - if (!InstanceId) - return FALSE; + WCHAR InstanceId[MAX_INSTANCE_ID]; + DWORD RequiredBytes = _countof(InstanceId); if (!SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, InstanceId, RequiredBytes, &RequiredBytes)) { LastError = LOG_LAST_ERROR(L"Failed to get adapter \"%s\" instance ID", Name); - goto cleanupInstanceId; + goto cleanup; } HANDLE NdisHandle = OpenDeviceObject(InstanceId); if (NdisHandle == INVALID_HANDLE_VALUE) { LastError = LOG(WIREGUARD_LOG_ERR, L"Failed to get adapter \"%s\" object", Name); - goto cleanupInstanceId; + goto cleanup; } if (!DeviceIoControl(NdisHandle, WG_IOCTL_SET, NULL, 0, Configuration, ConfigurationBytes, &RequiredBytes, NULL)) { @@ -181,8 +162,7 @@ RestoreConfigurationAndState( } cleanupHandle: CloseHandle(NdisHandle); -cleanupInstanceId: - Free(InstanceId); +cleanup: return RET_ERROR(TRUE, LastError); } diff --git a/api/wireguard.h b/api/wireguard.h index d60cbd2..48e5754 100644 --- a/api/wireguard.h +++ b/api/wireguard.h @@ -45,7 +45,7 @@ typedef struct _WIREGUARD_ADAPTER *WIREGUARD_ADAPTER_HANDLE; * characters. * * @param TunelType Name of the adapter tunnel type. Zero-terminated string of up to MAX_ADAPTER_NAME-1 - * characters. + * characters. * * @param RequestedGUID The GUID of the created network adapter, which then influences NLA generation deterministically. * If it is set to NULL, the GUID is chosen by the system at random, and hence a new NLA entry is |