From 73c4aaa2c09272581233625b2b5d45c1f37402f6 Mon Sep 17 00:00:00 2001 From: Stefan Eissing Date: Thu, 25 Jan 2024 15:33:54 +0100 Subject: [PATCH] mqtt: use stack scratch buffer for recv+publish - instead of data->state.buffer Closes #12792 --- lib/mqtt.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/mqtt.c b/lib/mqtt.c index 3f7eb761c2..704ffaf60d 100644 --- a/lib/mqtt.c +++ b/lib/mqtt.c @@ -624,7 +624,6 @@ static CURLcode mqtt_read_publish(struct Curl_easy *data, bool *done) struct connectdata *conn = data->conn; curl_socket_t sockfd = conn->sock[FIRSTSOCKET]; ssize_t nread; - unsigned char *pkt = (unsigned char *)data->state.buffer; size_t remlen; struct mqtt_conn *mqtt = &conn->proto.mqtt; struct MQTT *mq = data->req.p.mqtt; @@ -676,10 +675,11 @@ MQTT_SUBACK_COMING: FALLTHROUGH(); case MQTT_PUB_REMAIN: { /* read rest of packet, but no more. Cap to buffer size */ + char buffer[4*1024]; size_t rest = mq->npacket; - if(rest > (size_t)data->set.buffer_size) - rest = (size_t)data->set.buffer_size; - result = Curl_read(data, sockfd, (char *)pkt, rest, &nread); + if(rest > sizeof(buffer)) + rest = sizeof(buffer); + result = Curl_read(data, sockfd, buffer, rest, &nread); if(result) { if(CURLE_AGAIN == result) { infof(data, "EEEE AAAAGAIN"); @@ -692,14 +692,12 @@ MQTT_SUBACK_COMING: goto end; } - mq->npacket -= nread; - /* if QoS is set, message contains packet id */ - - result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)pkt, nread); + result = Curl_client_write(data, CLIENTWRITE_BODY, buffer, nread); if(result) goto end; + mq->npacket -= nread; if(!mq->npacket) /* no more PUBLISH payload, back to subscribe wait state */ mqstate(data, MQTT_FIRST, MQTT_PUBWAIT);