From 1b3af95be35ebc68f7eba842d0391fbc9d374079 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 30 Oct 2020 13:08:57 +0100 Subject: api: selectively use temporary variable to prepare output Suggested-by: Jason A. Donenfeld Signed-off-by: Simon Rozman --- api/adapter.c | 58 +++++++++++++++++++++++++++++++++------------------------- api/registry.c | 11 +++++++---- api/session.c | 52 ++++++++++++++++++++++++++-------------------------- 3 files changed, 66 insertions(+), 55 deletions(-) (limited to 'api') diff --git a/api/adapter.c b/api/adapter.c index e7742b1..459d155 100644 --- a/api/adapter.c +++ b/api/adapter.c @@ -33,14 +33,17 @@ AdapterGetDrvInfoDetail( DWORD Size = sizeof(SP_DRVINFO_DETAIL_DATA_W) + 0x100; for (;;) { - *DrvInfoDetailData = HeapAlloc(ModuleHeap, 0, Size); - if (!*DrvInfoDetailData) + SP_DRVINFO_DETAIL_DATA_W *p = HeapAlloc(ModuleHeap, 0, Size); + if (!p) return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY; - (*DrvInfoDetailData)->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W); - if (SetupDiGetDriverInfoDetailW(DevInfo, DevInfoData, DrvInfoData, *DrvInfoDetailData, Size, &Size)) + p->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W); + if (SetupDiGetDriverInfoDetailW(DevInfo, DevInfoData, DrvInfoData, p, Size, &Size)) + { + *DrvInfoDetailData = p; return ERROR_SUCCESS; + } DWORD Result = GetLastError(); - HeapFree(ModuleHeap, 0, *DrvInfoDetailData); + HeapFree(ModuleHeap, 0, p); if (Result != ERROR_INSUFFICIENT_BUFFER) return LOG_ERROR(L"Failed", Result); } @@ -57,13 +60,16 @@ GetDeviceRegistryProperty( { for (;;) { - *Buf = HeapAlloc(ModuleHeap, 0, *BufLen); - if (!*Buf) + BYTE *p = HeapAlloc(ModuleHeap, 0, *BufLen); + if (!p) return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY; - if (SetupDiGetDeviceRegistryPropertyW(DevInfo, DevInfoData, Property, ValueType, *Buf, *BufLen, BufLen)) + if (SetupDiGetDeviceRegistryPropertyW(DevInfo, DevInfoData, Property, ValueType, p, *BufLen, BufLen)) + { + *Buf = p; return ERROR_SUCCESS; + } DWORD Result = GetLastError(); - HeapFree(ModuleHeap, 0, *Buf); + HeapFree(ModuleHeap, 0, p); if (Result != ERROR_INSUFFICIENT_BUFFER) return LOG_ERROR(L"Querying property failed", Result); } @@ -506,8 +512,8 @@ CreateAdapterData( if (Key == INVALID_HANDLE_VALUE) return LOG_LAST_ERROR(L"Opening device registry key failed"); - *Adapter = HeapAlloc(ModuleHeap, 0, sizeof(WINTUN_ADAPTER)); - if (!*Adapter) + WINTUN_ADAPTER *a = HeapAlloc(ModuleHeap, 0, sizeof(WINTUN_ADAPTER)); + if (!a) { LOG(WINTUN_LOG_ERR, L"Out of memory"); Result = ERROR_OUTOFMEMORY; @@ -522,7 +528,7 @@ CreateAdapterData( LOG(WINTUN_LOG_ERR, L"Failed to query NetCfgInstanceId value"); goto cleanupAdapter; } - if (FAILED(CLSIDFromString(ValueStr, &(*Adapter)->CfgInstanceID))) + if (FAILED(CLSIDFromString(ValueStr, &a->CfgInstanceID))) { LOG(WINTUN_LOG_ERR, L"NetCfgInstanceId is not a GUID"); HeapFree(ModuleHeap, 0, ValueStr); @@ -532,7 +538,7 @@ CreateAdapterData( HeapFree(ModuleHeap, 0, ValueStr); /* Read the NetLuidIndex value. */ - Result = RegistryQueryDWORD(Key, L"NetLuidIndex", &(*Adapter)->LuidIndex, TRUE); + Result = RegistryQueryDWORD(Key, L"NetLuidIndex", &a->LuidIndex, TRUE); if (Result != ERROR_SUCCESS) { LOG(WINTUN_LOG_ERR, L"Failed to query NetLuidIndex value"); @@ -540,7 +546,7 @@ CreateAdapterData( } /* Read the NetLuidIndex value. */ - Result = RegistryQueryDWORD(Key, L"*IfType", &(*Adapter)->IfType, TRUE); + Result = RegistryQueryDWORD(Key, L"*IfType", &a->IfType, TRUE); if (Result != ERROR_SUCCESS) { LOG(WINTUN_LOG_ERR, L"Failed to query *IfType value"); @@ -548,24 +554,24 @@ CreateAdapterData( } DWORD Size; - if (!SetupDiGetDeviceInstanceIdW( - DevInfo, DevInfoData, (*Adapter)->DevInstanceID, _countof((*Adapter)->DevInstanceID), &Size)) + if (!SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, a->DevInstanceID, _countof(a->DevInstanceID), &Size)) { Result = LOG_LAST_ERROR(L"Failed to get device instance ID"); goto cleanupAdapter; } - if (wcsncpy_s((*Adapter)->Pool, _countof((*Adapter)->Pool), Pool, _TRUNCATE) == STRUNCATE) + if (wcsncpy_s(a->Pool, _countof(a->Pool), Pool, _TRUNCATE) == STRUNCATE) { LOG(WINTUN_LOG_ERR, L"Pool name too long"); Result = ERROR_INVALID_PARAMETER; goto cleanupAdapter; } + *Adapter = a; Result = ERROR_SUCCESS; cleanupAdapter: if (Result != ERROR_SUCCESS) - HeapFree(ModuleHeap, 0, *Adapter); + HeapFree(ModuleHeap, 0, a); cleanupKey: RegCloseKey(Key); return Result; @@ -884,7 +890,6 @@ cleanupQueriedStore: return Result; } - static BOOL IsOurDrvInfoDetail(_In_ const SP_DRVINFO_DETAIL_DATA_W *DrvInfoDetailData) { @@ -1167,7 +1172,8 @@ CreateAdapter( goto cleanupNetDevRegKey; } - Result = CreateAdapterData(Pool, DevInfo, &DevInfoData, Adapter); + WINTUN_ADAPTER *a; + Result = CreateAdapterData(Pool, DevInfo, &DevInfoData, &a); if (Result != ERROR_SUCCESS) { LOG(WINTUN_LOG_ERR, L"Failed to create adapter data"); @@ -1176,7 +1182,7 @@ CreateAdapter( HKEY TcpipAdapterRegKey; WCHAR TcpipAdapterRegPath[MAX_REG_PATH]; - Result = GetTcpipAdapterRegPath(*Adapter, TcpipAdapterRegPath); + Result = GetTcpipAdapterRegPath(a, TcpipAdapterRegPath); if (Result != ERROR_SUCCESS) goto cleanupAdapter; Result = RegistryOpenKeyWait( @@ -1200,7 +1206,7 @@ CreateAdapter( HKEY TcpipInterfaceRegKey; WCHAR TcpipInterfaceRegPath[MAX_REG_PATH]; - Result = GetTcpipInterfaceRegPath(*Adapter, TcpipInterfaceRegPath); + Result = GetTcpipInterfaceRegPath(a, TcpipInterfaceRegPath); if (Result != ERROR_SUCCESS) { LOG(WINTUN_LOG_ERR, L"Failed to determine interface-specific TCP/IP network registry key path"); @@ -1224,15 +1230,17 @@ CreateAdapter( if (Result != ERROR_SUCCESS) LOG_ERROR(L"Failed to set EnableDeadGWDetect", Result); - Result = WintunSetAdapterName(*Adapter, Name); - if (Result != ERROR_SUCCESS) + Result = WintunSetAdapterName(a, Name); + if (Result == ERROR_SUCCESS) + *Adapter = a; + else LOG_ERROR(L"Failed to set adapter name", Result); RegCloseKey(TcpipInterfaceRegKey); cleanupTcpipAdapterRegKey: RegCloseKey(TcpipAdapterRegKey); cleanupAdapter: if (Result != ERROR_SUCCESS) - HeapFree(ModuleHeap, 0, *Adapter); + HeapFree(ModuleHeap, 0, a); cleanupNetDevRegKey: RegCloseKey(NetDevRegKey); cleanupDevice: diff --git a/api/registry.c b/api/registry.c index 45e4b21..c0ebe93 100644 --- a/api/registry.c +++ b/api/registry.c @@ -181,13 +181,16 @@ RegistryQuery( { for (;;) { - *Buf = HeapAlloc(ModuleHeap, 0, *BufLen); - if (!*Buf) + BYTE *p = HeapAlloc(ModuleHeap, 0, *BufLen); + if (!p) return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY; - LSTATUS Result = RegQueryValueExW(Key, Name, NULL, ValueType, (BYTE *)*Buf, BufLen); + LSTATUS Result = RegQueryValueExW(Key, Name, NULL, ValueType, p, BufLen); if (Result == ERROR_SUCCESS) + { + *Buf = p; return ERROR_SUCCESS; - HeapFree(ModuleHeap, 0, *Buf); + } + HeapFree(ModuleHeap, 0, p); if (Result != ERROR_MORE_DATA) return Log ? LOG_ERROR(L"Querying value failed", Result) : Result; } diff --git a/api/session.c b/api/session.c index 6bb6f00..71849c3 100644 --- a/api/session.c +++ b/api/session.c @@ -67,8 +67,8 @@ typedef struct _TUN_SESSION WINTUN_STATUS WINAPI WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out_ TUN_SESSION **Session) { - *Session = HeapAlloc(ModuleHeap, HEAP_ZERO_MEMORY, sizeof(TUN_SESSION)); - if (!*Session) + TUN_SESSION *s = HeapAlloc(ModuleHeap, HEAP_ZERO_MEMORY, sizeof(TUN_SESSION)); + if (!s) return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY; const ULONG RingSize = TUN_RING_SIZE(Capacity); DWORD Result; @@ -84,25 +84,25 @@ WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out Result = ERROR_ACCESS_DENIED; goto cleanupAllocatedRegion; } - (*Session)->Descriptor.Send.RingSize = RingSize; - (*Session)->Descriptor.Send.Ring = (TUN_RING *)AllocatedRegion; - (*Session)->Descriptor.Send.TailMoved = CreateEventW(&SecurityAttributes, FALSE, FALSE, NULL); - if (!(*Session)->Descriptor.Send.TailMoved) + s->Descriptor.Send.RingSize = RingSize; + s->Descriptor.Send.Ring = (TUN_RING *)AllocatedRegion; + s->Descriptor.Send.TailMoved = CreateEventW(&SecurityAttributes, FALSE, FALSE, NULL); + if (!s->Descriptor.Send.TailMoved) { Result = LOG_LAST_ERROR(L"Failed to create send event"); goto cleanupToken; } - (*Session)->Descriptor.Receive.RingSize = RingSize; - (*Session)->Descriptor.Receive.Ring = (TUN_RING *)(AllocatedRegion + RingSize); - (*Session)->Descriptor.Receive.TailMoved = CreateEvent(&SecurityAttributes, FALSE, FALSE, NULL); - if (!(*Session)->Descriptor.Receive.TailMoved) + s->Descriptor.Receive.RingSize = RingSize; + s->Descriptor.Receive.Ring = (TUN_RING *)(AllocatedRegion + RingSize); + s->Descriptor.Receive.TailMoved = CreateEventW(&SecurityAttributes, FALSE, FALSE, NULL); + if (!s->Descriptor.Receive.TailMoved) { Result = LOG_LAST_ERROR(L"Failed to create receive event"); goto cleanupSendTailMoved; } - Result = WintunGetAdapterDeviceObject(Adapter, &(*Session)->Handle); + Result = WintunGetAdapterDeviceObject(Adapter, &s->Handle); if (Result != ERROR_SUCCESS) { LOG(WINTUN_LOG_ERR, L"Failed to open adapter device object"); @@ -110,36 +110,36 @@ WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity, _Out } DWORD BytesReturned; if (!DeviceIoControl( - (*Session)->Handle, - TUN_IOCTL_REGISTER_RINGS, - &(*Session)->Descriptor, - sizeof(TUN_REGISTER_RINGS), - NULL, - 0, - &BytesReturned, + s->Handle, + TUN_IOCTL_REGISTER_RINGS, + &s->Descriptor, + sizeof(TUN_REGISTER_RINGS), + NULL, + 0, + &BytesReturned, NULL)) { Result = LOG_LAST_ERROR(L"Failed to perform ioctl"); goto cleanupHandle; } RevertToSelf(); - (*Session)->Capacity = Capacity; - (void)InitializeCriticalSectionAndSpinCount(&(*Session)->Receive.Lock, LOCK_SPIN_COUNT); - (void)InitializeCriticalSectionAndSpinCount(&(*Session)->Send.Lock, LOCK_SPIN_COUNT); + s->Capacity = Capacity; + (void)InitializeCriticalSectionAndSpinCount(&s->Receive.Lock, LOCK_SPIN_COUNT); + (void)InitializeCriticalSectionAndSpinCount(&s->Send.Lock, LOCK_SPIN_COUNT); + *Session = s; return ERROR_SUCCESS; cleanupHandle: - CloseHandle((*Session)->Handle); + CloseHandle(s->Handle); cleanupReceiveTailMoved: - CloseHandle((*Session)->Descriptor.Receive.TailMoved); + CloseHandle(s->Descriptor.Receive.TailMoved); cleanupSendTailMoved: - CloseHandle((*Session)->Descriptor.Send.TailMoved); + CloseHandle(s->Descriptor.Send.TailMoved); cleanupToken: RevertToSelf(); cleanupAllocatedRegion: VirtualFree(AllocatedRegion, 0, MEM_RELEASE); cleanupRings: - HeapFree(ModuleHeap, 0, *Session); - *Session = NULL; + HeapFree(ModuleHeap, 0, s); return Result; } -- cgit v1.2.3-59-g8ed1b