paho.mqtt.c icon indicating copy to clipboard operation
paho.mqtt.c copied to clipboard

when using an address without :// mqttclient_create does not cause an error

Open michieldhonteriks opened this issue 1 year ago • 5 comments

Describe the bug i mistyped the address as tcp:ip:port instead of tcp://ip:port but it did not cause an error while creating the client and only a generic error while connecting. the problem lies in the implementation where there is no error or default handling when the :// part of the string cannot be found.

To Reproduce change the address in the publish example and remove the // after tcp run the code

Expected behavior the client creation function returns an error stating the address is invalid

** Environment (please complete the following information):**

  • OS: raspbian 12 (based on debian bookworm)
  • Hardware: Raspberry pi 3 B

michieldhonteriks avatar May 07 '24 08:05 michieldhonteriks

Which version of the library are you using?

jumoog avatar May 07 '24 09:05 jumoog

libpaho-mqtt3c.so.1.3

michieldhonteriks avatar May 07 '24 09:05 michieldhonteriks

There is only a 1.3.0 version but no 1.3. Try the latest version

jumoog avatar May 07 '24 11:05 jumoog

i tried it on 1.3.13 and it returns 0 on MQTTClient_create and -1 on MQTTClient_connect when the address starts with tcp: it functions propperly and connects when the address starts with tcp:// in the source code there is already a check for this on line 397 of MQTTClient.c but when this check fails the function continues.

michieldhonteriks avatar May 07 '24 12:05 michieldhonteriks

The connect function (in getaddrinfo) validates the address format, so the idea was not to (imperfectly) duplicate function which already exists in getaddrinfo. The switch on tcp://, ssl://, ws:// is mainly there to select the right protocol to use - an empty string defaults to TCP.

With your erroneous string, the protocol trace looks like this:

Trace : 5, 19700101 010000.000 getaddrinfo failed for addr tcp:test.mosquitto.org with rc 8 Trace : 5, 19700101 010000.000 tcp:test.mosquitto.org is not a valid IP address

One request on the todo list for a while is improved connection errors #937, which although are available in the trace or error callbacks, could be surfaced more readily.

icraggs avatar Jun 03 '24 12:06 icraggs