@@ -25,11 +25,13 @@
#include <stdbool.h>
#include <stdlib.h>
+typedef unsigned int msglvl_t;
+
/*
* These globals should not be accessed directly,
* but rather through macros or inline functions defined below.
*/
-extern unsigned int x_debug_level;
+extern msglvl_t x_debug_level;
extern int x_msg_line_num;
/* msg() flags */
@@ -58,7 +60,7 @@
/** Check muting filter */
-bool dont_mute(unsigned int flags);
+bool dont_mute(msglvl_t flags);
/* Macro to ensure (and teach static analysis tools) we exit on fatal errors */
#ifdef _MSC_VER
@@ -84,9 +86,9 @@
EXIT_FATAL(flags); \
} while (false)
-void x_msg(const unsigned int flags, const char *format, ...); /* should be called via msg above */
+void x_msg(const msglvl_t flags, const char *format, ...); /* should be called via msg above */
-void x_msg_va(const unsigned int flags, const char *format, va_list arglist);
+void x_msg_va(const msglvl_t flags, const char *format, va_list arglist);
/* Inline functions */
@@ -98,7 +100,7 @@
/** Return true if flags represent */
static inline bool
-msg_test(unsigned int flags)
+msg_test(msglvl_t flags)
{
return check_debug_level(flags);
}
@@ -394,8 +394,9 @@
DWORD dwResult = RegQueryValueEx(hKey, szName, NULL, &dwValueType, NULL, &dwSize);
if (dwResult != ERROR_SUCCESS)
{
- SetLastError(dwResult); /* MSDN does not mention RegQueryValueEx() to set GetLastError().
- But we do have an error code. Set last error manually. */
+ /* MSDN does not mention RegQueryValueEx() to set GetLastError(). But
+ we do have an error code. Set last error manually. */
+ SetLastError(dwResult);
msg(M_NONFATAL | M_ERRNO, "%s: enumerating \"%ls\" registry value failed", __FUNCTION__,
szName);
return dwResult;
@@ -407,7 +408,7 @@
case REG_EXPAND_SZ:
{
/* Read value. */
- LPWSTR szValue = (LPWSTR)malloc(dwSize);
+ LPWSTR szValue = malloc(dwSize);
if (szValue == NULL)
{
msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwSize);
@@ -417,9 +418,7 @@
dwResult = RegQueryValueEx(hKey, szName, NULL, NULL, (LPBYTE)szValue, &dwSize);
if (dwResult != ERROR_SUCCESS)
{
- SetLastError(
- dwResult); /* MSDN does not mention RegQueryValueEx() to set GetLastError(). But
- we do have an error code. Set last error manually. */
+ SetLastError(dwResult);
msg(M_NONFATAL | M_ERRNO, "%s: reading \"%ls\" registry value failed", __FUNCTION__,
szName);
free(szValue);
@@ -429,15 +428,9 @@
if (dwValueType == REG_EXPAND_SZ)
{
/* Expand the environment strings. */
- DWORD
- dwSizeExp = dwSize * 2, dwCountExp =
-#ifdef UNICODE
- dwSizeExp / sizeof(WCHAR);
-#else
- dwSizeExp / sizeof(WCHAR)
- - 1; /* Note: ANSI version requires one extra char. */
-#endif
- LPWSTR szValueExp = (LPWSTR)malloc(dwSizeExp);
+ DWORD dwSizeExp = dwSize * 2;
+ DWORD dwCountExp = dwSizeExp / sizeof(WCHAR);
+ LPWSTR szValueExp = malloc(dwSizeExp);
if (szValueExp == NULL)
{
free(szValue);
@@ -465,14 +458,9 @@
{
/* Retry with a bigger buffer. */
free(szValueExp);
-#ifdef UNICODE
dwSizeExp = dwCountExpResult * sizeof(WCHAR);
-#else
- /* Note: ANSI version requires one extra char. */
- dwSizeExp = (dwCountExpResult + 1) * sizeof(WCHAR);
-#endif
dwCountExp = dwCountExpResult;
- szValueExp = (LPWSTR)malloc(dwSizeExp);
+ szValueExp = malloc(dwSizeExp);
if (szValueExp == NULL)
{
free(szValue);
@@ -555,9 +543,7 @@
continue;
}
- SetLastError(
- dwResult); /* MSDN does not mention RegQueryValueEx() to set GetLastError(). But we
- do have an error code. Set last error manually. */
+ SetLastError(dwResult);
msg(M_NONFATAL | M_ERRNO, "%s: querying \"NetCfgInstanceId\" registry value failed",
__FUNCTION__);
break;
@@ -973,7 +959,7 @@
tap_set_adapter_name(_In_ LPCGUID pguidAdapter, _In_ LPCWSTR szName, _In_ BOOL bSilent)
{
DWORD dwResult;
- int msg_flag = bSilent ? M_WARN : M_NONFATAL;
+ msglvl_t msg_flag = bSilent ? M_WARN : M_NONFATAL;
msg_flag |= M_ERRNO;
if (pguidAdapter == NULL || szName == NULL)
@@ -999,8 +985,7 @@
dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0, KEY_QUERY_VALUE, &hKey);
if (dwResult != ERROR_SUCCESS)
{
- SetLastError(dwResult); /* MSDN does not mention RegOpenKeyEx() to set GetLastError(). But
- we do have an error code. Set last error manually. */
+ SetLastError(dwResult);
msg(msg_flag, "%s: RegOpenKeyEx(HKLM, \"%ls\") failed", __FUNCTION__, szRegKey);
goto cleanup_szAdapterId;
}
@@ -1167,8 +1152,7 @@
dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegKey, 0, KEY_READ, &hKey);
if (dwResult != ERROR_SUCCESS)
{
- SetLastError(dwResult); /* MSDN does not mention RegOpenKeyEx() to set GetLastError().
- But we do have an error code. Set last error manually. */
+ SetLastError(dwResult);
msg(M_WARN | M_ERRNO, "%s: RegOpenKeyEx(HKLM, \"%ls\") failed", __FUNCTION__, szRegKey);
goto cleanup_szAdapterId;
}
@@ -1187,12 +1171,11 @@
/* Append to the list. */
size_t hwid_size = (wcszlen(szzDeviceHardwareIDs) + 1) * sizeof(WCHAR);
size_t name_size = (wcslen(szName) + 1) * sizeof(WCHAR);
- struct tap_adapter_node *node = (struct tap_adapter_node *)malloc(
- sizeof(struct tap_adapter_node) + hwid_size + name_size);
+ size_t alloc_size = sizeof(struct tap_adapter_node) + hwid_size + name_size;
+ struct tap_adapter_node *node = malloc(alloc_size);
if (node == NULL)
{
- msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__,
- sizeof(struct tap_adapter_node) + hwid_size + name_size);
+ msg(M_FATAL, "%s: malloc(%zu) failed", __FUNCTION__, alloc_size);
dwResult = ERROR_OUTOFMEMORY;
goto cleanup_szName;
}