瀏覽代碼

Fix stack-use-after-scope in ua_network_tcp.c

Jannis Voelker 6 年之前
父節點
當前提交
4f9c773fee
共有 1 個文件被更改,包括 8 次插入9 次删除
  1. 8 9
      arch/ua_network_tcp.c

+ 8 - 9
arch/ua_network_tcp.c

@@ -320,20 +320,19 @@ ServerNetworkLayerTCP_start(UA_ServerNetworkLayer *nl, const UA_String *customHo
 
     /* Get the discovery url from the hostname */
     UA_String du = UA_STRING_NULL;
+    char discoveryUrlBuffer[256];
+    char hostnameBuffer[256];
     if (customHostname->length) {
-        char discoveryUrl[256];
-        du.length = (size_t)UA_snprintf(discoveryUrl, 255, "opc.tcp://%.*s:%d/",
+        du.length = (size_t)UA_snprintf(discoveryUrlBuffer, 255, "opc.tcp://%.*s:%d/",
                                      (int)customHostname->length,
                                      customHostname->data,
                                      layer->port);
-        du.data = (UA_Byte*)discoveryUrl;
+        du.data = (UA_Byte*)discoveryUrlBuffer;
     }else{
-        char hostname[256];
-        if(UA_gethostname(hostname, 255) == 0) {
-            char discoveryUrl[256];
-            du.length = (size_t)UA_snprintf(discoveryUrl, 255, "opc.tcp://%s:%d/",
-                                         hostname, layer->port);
-            du.data = (UA_Byte*)discoveryUrl;
+        if(UA_gethostname(hostnameBuffer, 255) == 0) {
+            du.length = (size_t)UA_snprintf(discoveryUrlBuffer, 255, "opc.tcp://%s:%d/",
+                                         hostnameBuffer, layer->port);
+            du.data = (UA_Byte*)discoveryUrlBuffer;
         } else {
             UA_LOG_ERROR(layer->logger, UA_LOGCATEGORY_NETWORK, "Could not get the hostname");
         }