Commit c036ae838ca20f8cbc0899c3277c1199c7067ffc

Authored by Wiebe Cazemier
1 parent b118b047

Buffer fixes

Showing 2 changed files with 25 additions and 16 deletions
client.cpp
@@ -19,16 +19,25 @@ Client::~Client() @@ -19,16 +19,25 @@ Client::~Client()
19 epoll_ctl(threadData->epollfd, EPOLL_CTL_DEL, fd, NULL); // NOTE: the last NULL can cause crash on old kernels 19 epoll_ctl(threadData->epollfd, EPOLL_CTL_DEL, fd, NULL); // NOTE: the last NULL can cause crash on old kernels
20 close(fd); 20 close(fd);
21 free(readbuf); 21 free(readbuf);
  22 + free(writebuf);
22 } 23 }
23 24
24 // false means any kind of error we want to get rid of the client for. 25 // false means any kind of error we want to get rid of the client for.
25 bool Client::readFdIntoBuffer() 26 bool Client::readFdIntoBuffer()
26 { 27 {
27 - int read_size = getReadBufMaxWriteSize();  
28 -  
29 int n; 28 int n;
30 - while ((n = read(fd, &readbuf[wi], read_size)) != 0) 29 + while ((n = read(fd, &readbuf[wi], getReadBufMaxWriteSize())) != 0)
31 { 30 {
  31 + if (n > 0)
  32 + {
  33 + wi += n;
  34 +
  35 + if (getReadBufBytesUsed() >= readBufsize)
  36 + {
  37 + growReadBuffer();
  38 + }
  39 + }
  40 +
32 if (n < 0) 41 if (n < 0)
33 { 42 {
34 if (errno == EINTR) 43 if (errno == EINTR)
@@ -38,15 +47,6 @@ bool Client::readFdIntoBuffer() @@ -38,15 +47,6 @@ bool Client::readFdIntoBuffer()
38 else 47 else
39 return false; 48 return false;
40 } 49 }
41 -  
42 - wi += n;  
43 -  
44 - if (getReadBufBytesUsed() >= readBufsize)  
45 - {  
46 - growReadBuffer();  
47 - }  
48 -  
49 - read_size = getReadBufMaxWriteSize();  
50 } 50 }
51 51
52 if (n == 0) // client disconnected. 52 if (n == 0) // client disconnected.
@@ -84,6 +84,8 @@ bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also @@ -84,6 +84,8 @@ bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also
84 int n; 84 int n;
85 while ((n = write(fd, &writebuf[wri], getWriteBufBytesUsed())) != 0) 85 while ((n = write(fd, &writebuf[wri], getWriteBufBytesUsed())) != 0)
86 { 86 {
  87 + if (n > 0)
  88 + wri += n;
87 if (n < 0) 89 if (n < 0)
88 { 90 {
89 if (errno == EINTR) 91 if (errno == EINTR)
@@ -93,8 +95,6 @@ bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also @@ -93,8 +95,6 @@ bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also
93 else 95 else
94 return false; 96 return false;
95 } 97 }
96 -  
97 - wri += n;  
98 } 98 }
99 99
100 if (wri == wwi) 100 if (wri == wwi)
@@ -148,6 +148,9 @@ bool Client::bufferToMqttPackets(std::vector&lt;MqttPacket&gt; &amp;packetQueueIn) @@ -148,6 +148,9 @@ bool Client::bufferToMqttPackets(std::vector&lt;MqttPacket&gt; &amp;packetQueueIn)
148 packetQueueIn.push_back(std::move(packet)); 148 packetQueueIn.push_back(std::move(packet));
149 149
150 ri += packet_length; 150 ri += packet_length;
  151 +
  152 + if (ri > wi)
  153 + throw std::runtime_error("hier");
151 } 154 }
152 else 155 else
153 break; 156 break;
client.h
@@ -46,7 +46,7 @@ class Client @@ -46,7 +46,7 @@ class Client
46 46
47 size_t getReadBufMaxWriteSize() 47 size_t getReadBufMaxWriteSize()
48 { 48 {
49 - size_t available = readBufsize - getReadBufBytesUsed(); 49 + size_t available = readBufsize - wi;
50 return available; 50 return available;
51 } 51 }
52 52
@@ -54,12 +54,14 @@ class Client @@ -54,12 +54,14 @@ class Client
54 { 54 {
55 const size_t newBufSize = readBufsize * 2; 55 const size_t newBufSize = readBufsize * 2;
56 readbuf = (char*)realloc(readbuf, newBufSize); 56 readbuf = (char*)realloc(readbuf, newBufSize);
  57 + if (readbuf == NULL)
  58 + throw std::runtime_error("Memory allocation failure in growReadBuffer()");
57 readBufsize = newBufSize; 59 readBufsize = newBufSize;
58 } 60 }
59 61
60 size_t getWriteBufMaxWriteSize() 62 size_t getWriteBufMaxWriteSize()
61 { 63 {
62 - size_t available = writeBufsize - getWriteBufBytesUsed(); 64 + size_t available = writeBufsize - wwi;
63 return available; 65 return available;
64 } 66 }
65 67
@@ -75,6 +77,10 @@ class Client @@ -75,6 +77,10 @@ class Client
75 77
76 const size_t newBufSize = writeBufsize + add_size; 78 const size_t newBufSize = writeBufsize + add_size;
77 writebuf = (char*)realloc(writebuf, newBufSize); 79 writebuf = (char*)realloc(writebuf, newBufSize);
  80 +
  81 + if (writebuf == NULL)
  82 + throw std::runtime_error("Memory allocation failure in growWriteBuffer()");
  83 +
78 writeBufsize = newBufSize; 84 writeBufsize = newBufSize;
79 } 85 }
80 86