bluerhinos / phpMQTT

a simple php class to connect/publish/subscribe to a MQTT broker
Other
770 stars 461 forks source link

Uninitialized string offset: 0 in xxx on line (209) #140

Open itdontgo opened 1 year ago

itdontgo commented 1 year ago

Occasionally when the server is under load you will get this error. Obviously the fread is returning a blank string and you should return false if it's blank. The connection has failed anyway. It's probably because it's not blocking and it's just chance that there is data to return when you do the read hence the intermittent failure. We have set the stream to blocking and left a 5s timeout. I'll update if this resolves the issue but even if not... return false on any operation to the socket/read/write during connection. It's probably a problem more on Windows servers as you can't rely on querying the socket.

    if(!stream_set_timeout($this->socket, 5)){return false;}
    if(!stream_set_blocking($this->socket, 1)){return false;} //blocking

To avoid the error we added 'if's:

    if(!fwrite($this->socket, $head, 2)){return false;}
    if(!fwrite($this->socket, $buffer)){return false;}

    $string = $this->read(4);
    if(!$string){return false;}

error here if (ord($string[0]) >> 4 === 2 && $string[3] === chr(0)) {