Browse code

Handle NULL returns from calloc() in sample plugins.

This is basic housekeeping, adding NULL checks to context initialization
of the sample plugin collection which are missing it. Realistically,
this can never happen, but since these are supposed to be "good examples",
not checking calloc() return isn't one.

Trac: #587

Reported-By: Dogbert (in Trac)
Signed-off-by: Gert Doering <gert@greenie.muc.de>
Acked-by: David Sommerseth <davids@openvpn.net>
Message-Id: <20200909104837.6123-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg20922.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Gert Doering authored on 2020/09/09 19:48:37
Showing 5 changed files
... ...
@@ -141,6 +141,11 @@ openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *
141 141
      * Allocate our context
142 142
      */
143 143
     context = (struct plugin_context *) calloc(1, sizeof(struct plugin_context));
144
+    if (context == NULL)
145
+    {
146
+        printf("PLUGIN: allocating memory for context failed\n");
147
+        return NULL;
148
+    }
144 149
 
145 150
     context->test_deferred_auth = atoi_null0(get_env("test_deferred_auth", envp));
146 151
     printf("TEST_DEFERRED_AUTH %d\n", context->test_deferred_auth);
... ...
@@ -92,6 +92,12 @@ openvpn_plugin_open_v3(const int version,
92 92
 {
93 93
     struct plugin *plugin = calloc(1, sizeof(*plugin));
94 94
 
95
+    if (plugin == NULL)
96
+    {
97
+        printf("PLUGIN: allocating memory for context failed\n");
98
+        return OPENVPN_PLUGIN_FUNC_ERROR;
99
+    }
100
+
95 101
     plugin->type = get_env("remote_1", args->envp) ? CLIENT : SERVER;
96 102
     plugin->log  = args->callbacks->plugin_log;
97 103
 
... ...
@@ -78,6 +78,11 @@ openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *
78 78
      * Allocate our context
79 79
      */
80 80
     context = (struct plugin_context *) calloc(1, sizeof(struct plugin_context));
81
+    if (context == NULL)
82
+    {
83
+        printf("PLUGIN: allocating memory for context failed\n");
84
+        return NULL;
85
+    }
81 86
 
82 87
     /*
83 88
      * Set the username/password we will require.
... ...
@@ -113,6 +113,11 @@ openvpn_plugin_open_v3(const int v3structver,
113 113
 
114 114
     /* Allocate our context */
115 115
     context = (struct plugin_context *) calloc(1, sizeof(struct plugin_context));
116
+    if (context == NULL)
117
+    {
118
+        printf("PLUGIN: allocating memory for context failed\n");
119
+        return OPENVPN_PLUGIN_FUNC_ERROR;
120
+    }
116 121
 
117 122
     /* Set the username/password we will require. */
118 123
     context->username = "foo";
... ...
@@ -80,6 +80,11 @@ openvpn_plugin_open_v1(unsigned int *type_mask, const char *argv[], const char *
80 80
      * Allocate our context
81 81
      */
82 82
     context = (struct plugin_context *) calloc(1, sizeof(struct plugin_context));
83
+    if (context == NULL)
84
+    {
85
+        printf("PLUGIN: allocating memory for context failed\n");
86
+        return NULL;
87
+    }
83 88
 
84 89
     /*
85 90
      * Set the username/password we will require.