Fix problem with offset in wpa_receive_from not being updated correctly
In cases where a large amount of data is to be transferred over a non-UDP socket, the offset would not be updated correctly.
This patch makes sure that the offset is always updated according to the amount read.
@@ -564,27 +564,28 @@ int wpa_ctrl_recvfrom(int sock, char *buf, size_t len
ssize_t amount = 0;
size_t recv_len;
char *pos;
- int res;
+
#ifdef CONFIG_CTRL_IFACE_UDP
UNUSED(flags);
#endif
- while ((amount >= 0) && ((amount + offset) < CTRL_HEADER_SIZE)) {
+ while ((offset += amount) < CTRL_HEADER_SIZE) {
#ifdef CONFIG_CTRL_IFACE_UDP
amount = recvfrom(sock, buf + offset, CTRL_HEADER_SIZE - offset, 0, from, fromlen);
#else
amount = read(sock, buf + offset, CTRL_HEADER_SIZE - offset);
#endif
- }
if (amount < 0) {
return amount;
- } else {
+ }
+ }
+
msg_size = wpa_ctrl_get_header(buf);
offset = 0;
amount = 0;
buf[CTRL_HEADER_SIZE] = '\0';
wpa_printf(MSG_EXCESSIVE, "received message size: %d ('%s')", msg_size, buf);
- }
- while ((amount >= 0) && ((amount + offset) < msg_size)) {
+
+ while ((offset += amount) < msg_size) {
if (msg_size > len) { // This is BAD!!! read out the message to keep alignment.
pos = buf;
} else {
@@ -594,18 +595,16 @@ int wpa_ctrl_recvfrom(int sock, char *buf, size_t len
#ifdef CONFIG_CTRL_IFACE_UDP
amount = recvfrom(sock, pos, recv_len, 0, from, fromlen);
#else
- amount += read(sock, pos, recv_len);
+ amount = read(sock, pos, recv_len);
#endif
+ if (amount < 0) {
+ return amount;
+ }
}
if (msg_size > len) {
return -1; // Buffer too small for the message, return error
}
- if (amount >= 0) {
- res = amount + offset;
- } else {
- res = amount;
- }
- return res;
+ return msg_size;
}
static int _wpa_ctrl_sendto(int sock, const char *buf, size_t len
