Gert Doering has submitted this change. ( 
https://gerrit.openvpn.net/c/openvpn/+/2380?usp=email )

Change subject: tapctl: Various code cleanups
......................................................................

tapctl: Various code cleanups

- Switch to msglvl_t for x_msg() argument to make
  code more in sync with main openvpn source.
  Fixes one sign conversion warning.
- Remove exessive comments about error handling for Reg*
- Remove remaining #ifndef UNICODE handling. UNICODE is
  always enforced (see basic.h).
- Remove unnecessary casts for malloc return value.

Change-Id: Ia23cdfc2736c2ef1bef044b6c1ad31e56609ca8f
Signed-off-by: Frank Lichtenheld <[email protected]>
Acked-by: Razvan Cojocaru <[email protected]>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2380
Message-Id: <[email protected]>
URL: 
https://www.mail-archive.com/[email protected]/msg42234.html
Signed-off-by: Gert Doering <[email protected]>
---
M src/tapctl/error.h
M src/tapctl/tap.c
2 files changed, 23 insertions(+), 38 deletions(-)




diff --git a/src/tapctl/error.h b/src/tapctl/error.h
index 4424f56..f1848b6 100644
--- a/src/tapctl/error.h
+++ b/src/tapctl/error.h
@@ -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);
 }
diff --git a/src/tapctl/tap.c b/src/tapctl/tap.c
index a3d83e9..76ec569 100644
--- a/src/tapctl/tap.c
+++ b/src/tapctl/tap.c
@@ -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;
         }

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/2380?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia23cdfc2736c2ef1bef044b6c1ad31e56609ca8f
Gerrit-Change-Number: 2380
Gerrit-PatchSet: 2
Gerrit-Owner: Frank Lichtenheld <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to