Browse code

plugins: Clean up -Wconversion warnings

Most of the are actually the same ones copied to every
single plugin.

Some drive-by fixes of other warnings and some
conversion cleanups that had no warnings because
they were suppressed by casts.

Change-Id: Id61df43bd79fc794a55e107daa0218c8441c2b2c
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <20250725124415.20645-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg32330.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Frank Lichtenheld authored on 2025/07/25 21:44:09
Showing 10 changed files
... ...
@@ -86,9 +86,8 @@ get_env(const char *name, const char *envp[])
86 86
 {
87 87
     if (envp)
88 88
     {
89
-        int i;
90
-        const int namelen = strlen(name);
91
-        for (i = 0; envp[i]; ++i)
89
+        const size_t namelen = strlen(name);
90
+        for (int i = 0; envp[i]; ++i)
92 91
         {
93 92
             if (!strncmp(envp[i], name, namelen))
94 93
             {
... ...
@@ -309,7 +308,7 @@ cc_handle_deferred_v1(int seconds, const char *name, const char **envp)
309 309
 
310 310
     /* do mighty complicated work that will really take time here... */
311 311
     plugin_log(PLOG_NOTE, MODULE, "in async/deferred handler, sleep(%d)", seconds);
312
-    sleep(seconds);
312
+    sleep((unsigned int)seconds);
313 313
 
314 314
     /* write config options to openvpn */
315 315
     int ret = write_cc_options_file(name, envp);
... ...
@@ -124,9 +124,8 @@ get_env(const char *name, const char *envp[])
124 124
 {
125 125
     if (envp)
126 126
     {
127
-        int i;
128
-        const int namelen = strlen(name);
129
-        for (i = 0; envp[i]; ++i)
127
+        const size_t namelen = strlen(name);
128
+        for (int i = 0; envp[i]; ++i)
130 129
         {
131 130
             if (!strncmp(envp[i], name, namelen))
132 131
             {
... ...
@@ -170,7 +169,7 @@ atoi_null0(const char *str)
170 170
 
171 171
 /* Require a minimum OpenVPN Plugin API */
172 172
 OPENVPN_EXPORT int
173
-openvpn_plugin_min_version_required_v1()
173
+openvpn_plugin_min_version_required_v1(void)
174 174
 {
175 175
     return OPENVPN_PLUGIN_VERSION_MIN;
176 176
 }
... ...
@@ -349,9 +348,9 @@ auth_user_pass_verify(struct plugin_context *context,
349 349
      */
350 350
 
351 351
     /* do mighty complicated work that will really take time here... */
352
-    plog(context, PLOG_NOTE, "in async/deferred handler, usleep(%d)",
353
-         context->test_deferred_auth*1000);
354
-    usleep(context->test_deferred_auth*1000);
352
+    useconds_t wait_time = (useconds_t)context->test_deferred_auth*1000;
353
+    plog(context, PLOG_NOTE, "in async/deferred handler, usleep(%u)", wait_time);
354
+    usleep(wait_time);
355 355
 
356 356
     /* now signal success state to openvpn */
357 357
     int fd = open(auth_control_file, O_WRONLY);
... ...
@@ -69,9 +69,8 @@ get_env(const char *name, const char *envp[])
69 69
 {
70 70
     if (envp)
71 71
     {
72
-        int i;
73
-        const int namelen = strlen(name);
74
-        for (i = 0; envp[i]; ++i)
72
+        const size_t namelen = strlen(name);
73
+        for (int i = 0; envp[i]; ++i)
75 74
         {
76 75
             if (!strncmp(envp[i], name, namelen))
77 76
             {
... ...
@@ -52,9 +52,8 @@ get_env(const char *name, const char *envp[])
52 52
 {
53 53
     if (envp)
54 54
     {
55
-        int i;
56
-        const int namelen = strlen(name);
57
-        for (i = 0; envp[i]; ++i)
55
+        const size_t namelen = strlen(name);
56
+        for (int i = 0; envp[i]; ++i)
58 57
         {
59 58
             if (!strncmp(envp[i], name, namelen))
60 59
             {
... ...
@@ -55,9 +55,8 @@ get_env(const char *name, const char *envp[])
55 55
 {
56 56
     if (envp)
57 57
     {
58
-        int i;
59
-        const int namelen = strlen(name);
60
-        for (i = 0; envp[i]; ++i)
58
+        const size_t namelen = strlen(name);
59
+        for (int i = 0; envp[i]; ++i)
61 60
         {
62 61
             if (!strncmp(envp[i], name, namelen))
63 62
             {
... ...
@@ -59,9 +59,8 @@ get_env(const char *name, const char *envp[])
59 59
 {
60 60
     if (envp)
61 61
     {
62
-        int i;
63
-        const int namelen = strlen(name);
64
-        for (i = 0; envp[i]; ++i)
62
+        const size_t namelen = strlen(name);
63
+        for (int i = 0; envp[i]; ++i)
65 64
         {
66 65
             if (!strncmp(envp[i], name, namelen))
67 66
             {
... ...
@@ -175,7 +174,7 @@ openvpn_plugin_func_v1(openvpn_plugin_handle_t handle, const int type, const cha
175 175
 
176 176
     /* test the BASE64 encode function */
177 177
     char *buf = NULL;
178
-    int r = ovpn_base64_encode(clcert_cn, strlen(clcert_cn), &buf);
178
+    int r = ovpn_base64_encode(clcert_cn, (int)strlen(clcert_cn), &buf);
179 179
     ovpn_log(PLOG_NOTE, PLUGIN_NAME, "BASE64 encoded '%s' (return value %i):  '%s'",
180 180
              clcert_cn, r, buf);
181 181
 
... ...
@@ -54,9 +54,8 @@ get_env(const char *name, const char *envp[])
54 54
 {
55 55
     if (envp)
56 56
     {
57
-        int i;
58
-        const int namelen = strlen(name);
59
-        for (i = 0; envp[i]; ++i)
57
+        const size_t namelen = strlen(name);
58
+        for (int i = 0; envp[i]; ++i)
60 59
         {
61 60
             if (!strncmp(envp[i], name, namelen))
62 61
             {
... ...
@@ -165,31 +165,30 @@ send_control(int fd, int code)
165 165
     }
166 166
 }
167 167
 
168
-static int
169
-recv_string(int fd, char *buffer, int len)
168
+static ssize_t
169
+recv_string(int fd, char *buffer, size_t len)
170 170
 {
171 171
     if (len > 0)
172 172
     {
173
-        ssize_t size;
174 173
         memset(buffer, 0, len);
175
-        size = read(fd, buffer, len);
174
+        ssize_t size = read(fd, buffer, len);
176 175
         buffer[len-1] = 0;
177 176
         if (size >= 1)
178 177
         {
179
-            return (int)size;
178
+            return size;
180 179
         }
181 180
     }
182 181
     return -1;
183 182
 }
184 183
 
185
-static int
184
+static ssize_t
186 185
 send_string(int fd, const char *string)
187 186
 {
188
-    const int len = strlen(string) + 1;
187
+    const size_t len = strlen(string) + 1;
189 188
     const ssize_t size = write(fd, string, len);
190 189
     if (size == len)
191 190
     {
192
-        return (int) size;
191
+        return size;
193 192
     }
194 193
     else
195 194
     {
... ...
@@ -645,27 +644,26 @@ openvpn_plugin_abort_v1(openvpn_plugin_handle_t handle)
645 645
  * PAM conversation function
646 646
  */
647 647
 static int
648
-my_conv(int n, const struct pam_message **msg_array,
648
+my_conv(int num_msg, const struct pam_message **msg_array,
649 649
         struct pam_response **response_array, void *appdata_ptr)
650 650
 {
651 651
     const struct user_pass *up = ( const struct user_pass *) appdata_ptr;
652 652
     struct pam_response *aresp;
653
-    int i;
654 653
     int ret = PAM_SUCCESS;
655 654
 
656 655
     *response_array = NULL;
657 656
 
658
-    if (n <= 0 || n > PAM_MAX_NUM_MSG)
657
+    if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG)
659 658
     {
660 659
         return (PAM_CONV_ERR);
661 660
     }
662
-    if ((aresp = calloc(n, sizeof *aresp)) == NULL)
661
+    if ((aresp = calloc((size_t)num_msg, sizeof *aresp)) == NULL)
663 662
     {
664 663
         return (PAM_BUF_ERR);
665 664
     }
666 665
 
667 666
     /* loop through each PAM-module query */
668
-    for (i = 0; i < n; ++i)
667
+    for (int i = 0; i < num_msg; ++i)
669 668
     {
670 669
         const struct pam_message *msg = msg_array[i];
671 670
         aresp[i].resp_retcode = 0;
... ...
@@ -683,9 +681,9 @@ my_conv(int n, const struct pam_message **msg_array,
683 683
         {
684 684
             /* use name/value list match method */
685 685
             const struct name_value_list *list = up->name_value_list;
686
-            int j;
687 686
 
688 687
             /* loop through name/value pairs */
688
+            int j; /* checked after loop */
689 689
             for (j = 0; j < list->len; ++j)
690 690
             {
691 691
                 const char *match_name = list->data[j].name;
... ...
@@ -79,7 +79,7 @@ searchandreplace(const char *tosearch, const char *searchfor, const char *replac
79 79
 
80 80
     while (scratch)
81 81
     {
82
-        strncat(temp, searching, scratch-searching);
82
+        strncat(temp, searching, (size_t)(scratch-searching));
83 83
         strcat(temp, replacewith);
84 84
 
85 85
         searching = scratch+strlen(searchfor);
... ...
@@ -93,9 +93,8 @@ get_env(const char *name, const char *envp[])
93 93
 {
94 94
     if (envp)
95 95
     {
96
-        int i;
97
-        const int namelen = strlen(name);
98
-        for (i = 0; envp[i]; ++i)
96
+        const size_t namelen = strlen(name);
97
+        for (int i = 0; envp[i]; ++i)
99 98
         {
100 99
             if (!strncmp(envp[i], name, namelen))
101 100
             {
... ...
@@ -88,9 +88,8 @@ get_env(const char *name, const char *envp[])
88 88
 {
89 89
     if (envp)
90 90
     {
91
-        int i;
92
-        const int namelen = strlen(name);
93
-        for (i = 0; envp[i]; ++i)
91
+        const size_t namelen = strlen(name);
92
+        for (int i = 0; envp[i]; ++i)
94 93
         {
95 94
             if (!strncmp(envp[i], name, namelen))
96 95
             {
... ...
@@ -108,10 +107,10 @@ get_env(const char *name, const char *envp[])
108 108
 /*
109 109
  * Return the length of a string array
110 110
  */
111
-static int
111
+static size_t
112 112
 string_array_len(const char *array[])
113 113
 {
114
-    int i = 0;
114
+    size_t i = 0;
115 115
     if (array)
116 116
     {
117 117
         while (array[i])
... ...
@@ -141,14 +140,14 @@ recv_control(int fd)
141 141
     }
142 142
 }
143 143
 
144
-static int
144
+static ssize_t
145 145
 send_control(int fd, int code)
146 146
 {
147 147
     unsigned char c = (unsigned char) code;
148 148
     const ssize_t size = write(fd, &c, sizeof(c));
149 149
     if (size == sizeof(c))
150 150
     {
151
-        return (int) size;
151
+        return size;
152 152
     }
153 153
     else
154 154
     {
... ...
@@ -281,7 +280,6 @@ OPENVPN_EXPORT openvpn_plugin_handle_t
281 281
 openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *envp[])
282 282
 {
283 283
     struct down_root_context *context;
284
-    int i = 0;
285 284
 
286 285
     /*
287 286
      * Allocate our context
... ...
@@ -320,7 +318,7 @@ openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *
320 320
     }
321 321
 
322 322
     /* Ignore argv[0], as it contains just the plug-in file name */
323
-    for (i = 1; i < string_array_len(argv); i++)
323
+    for (int i = 1; i < string_array_len(argv); i++)
324 324
     {
325 325
         context->command[i-1] = (char *) argv[i];
326 326
     }