Skip to content

Commit 8c391ce

Browse files
committed
Reworked readline code such that it checks that client is connected before attempting a read. Which solved the crash in issue #46
1 parent 80aa926 commit 8c391ce

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/tiny_websockets/network/generic_esp/generic_esp_clients.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ namespace websockets { namespace network {
4848
}
4949

5050
WSString readLine() override {
51-
int val;
52-
WSString line;
53-
do {
54-
yield();
55-
val = client.read();
56-
if(val < 0) continue;
57-
line += (char)val;
58-
} while(val != '\n');
59-
if(!available()) close();
51+
WSString line = "";
52+
53+
int ch = -1;
54+
while( ch != '\n' && available()) {
55+
ch = client.read();
56+
if (ch < 0) continue;
57+
line += (char) ch;
58+
}
59+
6060
return line;
6161
}
6262

src/websockets_client.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ namespace websockets {
277277
WSString line = "";
278278
while (true) {
279279
line = this->_client->readLine();
280+
281+
if (line.size() < 2) {
282+
close(CloseReason_ProtocolError);
283+
return false;
284+
}
285+
280286
if (line == "\r\n") break;
281287
// remove /r/n from line end
282288
line = line.substr(0, line.size() - 2);

0 commit comments

Comments
 (0)