open62541 / open62541

Open source implementation of OPC UA (OPC Unified Architecture) aka IEC 62541 licensed under Mozilla Public License v2.0
http://open62541.org
Mozilla Public License 2.0
2.59k stars 1.24k forks source link

Log message "Server url is invalid: %.*s" causes segmentation fault when connecting to empty URL #4741

Open pettineh opened 2 years ago

pettineh commented 2 years ago

Description

Calling _UA_Clientconnect function with completely empty address seems to cause a segmentation fault in the logging implementation. When the logger gets a message with content: "Server url is invalid: %.*s", the segmentation fault occurs when the va_list variables are being accessed. My guess is that the char pointer pointing to the message url is invalid when it is passed to the logger.

Background Information / Reproduction Steps

Now all this happens when the address given to _UA_Clientconnect function is completely empty null-terminated char array. Adding even a single whitespace character to the array fixes the issue.

To reproduce the issue, these are the lines of code I execute inside my OPC UA client implementing class: UA_Client client = UA_Client_new(); UA_ClientConfig config = UA_Client_getConfig(client); UA_ClientConfig_setDefault(config); config->logger.log = &myLogHandler; config->logger.context = this; char url[] = ""; UA_Client_connect(client, url); // Causes error log message: "Server url is invalid: %.*s"

Inside myLogHandler callback I call the strlen for the first variable returned by the va_list object holding the string arguments. This causes segfault when the log message is the one mentioned above.

Checklist

Please provide the following information:

kimim commented 2 years ago

compilation warning on my machine, maybe you need to pass url other than &url here.

tutorial_client_firststeps.c: In function 'main':
tutorial_client_firststeps.c:19:54: warning: passing argument 2 of 'UA_Client_connect' from incompatible pointer type [-Wincompatible-pointer-types]
   19 |     UA_StatusCode retval = UA_Client_connect(client, &url);
      |                                                      ^~~~
      |                                                      |
      |                                                      char (*)[1]
In file included from tutorial_client_firststeps.c:11:
open62541.h:28385:50: note: expected 'const char *' but argument is of type 'char (*)[1]'
28385 | UA_Client_connect(UA_Client *client, const char *endpointUrl);
      |                                      ~~~~~~~~~~~~^~~~~~~~~~~
pettineh commented 2 years ago

compilation warning on my machine, maybe you need to pass url other than &url here.

tutorial_client_firststeps.c: In function 'main':
tutorial_client_firststeps.c:19:54: warning: passing argument 2 of 'UA_Client_connect' from incompatible pointer type [-Wincompatible-pointer-types]
   19 |     UA_StatusCode retval = UA_Client_connect(client, &url);
      |                                                      ^~~~
      |                                                      |
      |                                                      char (*)[1]
In file included from tutorial_client_firststeps.c:11:
open62541.h:28385:50: note: expected 'const char *' but argument is of type 'char (*)[1]'
28385 | UA_Client_connect(UA_Client *client, const char *endpointUrl);
      |                                      ~~~~~~~~~~~~^~~~~~~~~~~

My bad. For the code example I simplified the code a bit. I originally passed the char * to the function. I will edit the code example in the original post.

kimim commented 2 years ago

If everything is fine, could you close this issue?

pettineh commented 2 years ago

@kimim Now you misunderstood, the issue still exists. The code example I gave was just bad.