Skip to content

Commit 4dd99b3

Browse files
committed
Added checks to make sure various strings aren’t too long
1 parent 0dc0f0f commit 4dd99b3

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

mqtt-sn.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ void mqtt_sn_send_connect(int sock, const char* client_id, uint16_t keepalive)
165165
{
166166
connect_packet_t packet;
167167

168+
// Check that it isn't too long
169+
if (client_id && strlen(client_id) > 23) {
170+
fprintf(stderr, "Error: client id is too long\n");
171+
exit(EXIT_FAILURE);
172+
}
173+
168174
// Create the CONNECT packet
169175
packet.type = MQTT_SN_TYPE_CONNECT;
170176
packet.flags = MQTT_SN_FLAG_CLEAN;
@@ -196,11 +202,18 @@ void mqtt_sn_send_connect(int sock, const char* client_id, uint16_t keepalive)
196202
void mqtt_sn_send_register(int sock, const char* topic_name)
197203
{
198204
register_packet_t packet;
205+
size_t topic_name_len = strlen(topic_name);
206+
207+
if (topic_name_len > MQTT_SN_MAX_TOPIC_LENGTH) {
208+
fprintf(stderr, "Error: topic name is too long\n");
209+
exit(EXIT_FAILURE);
210+
}
211+
199212
packet.type = MQTT_SN_TYPE_REGISTER;
200213
packet.topic_id = 0;
201214
packet.message_id = htons(next_message_id++);
202215
strncpy(packet.topic_name, topic_name, sizeof(packet.topic_name));
203-
packet.length = 0x06 + strlen(packet.topic_name);
216+
packet.length = 0x06 + topic_name_len;
204217

205218
if (debug)
206219
fprintf(stderr, "Sending REGISTER packet...\n");
@@ -242,8 +255,13 @@ static uint8_t mqtt_sn_get_qos_flag(int8_t qos)
242255
void mqtt_sn_send_publish(int sock, uint16_t topic_id, uint8_t topic_type, const char* data, int8_t qos, uint8_t retain)
243256
{
244257
publish_packet_t packet;
245-
//size_t len =
246-
258+
size_t data_len = strlen(data);
259+
260+
if (data_len > sizeof(packet.data)) {
261+
fprintf(stderr, "Error: payload is too big\n");
262+
exit(EXIT_FAILURE);
263+
}
264+
247265
packet.type = MQTT_SN_TYPE_PUBLISH;
248266
packet.flags = 0x00;
249267
if (retain)
@@ -253,7 +271,7 @@ void mqtt_sn_send_publish(int sock, uint16_t topic_id, uint8_t topic_type, const
253271
packet.topic_id = htons(topic_id);
254272
packet.message_id = htons(next_message_id++);
255273
strncpy(packet.data, data, sizeof(packet.data));
256-
packet.length = 0x07 + strlen(data);
274+
packet.length = 0x07 + data_len;
257275

258276
if (debug)
259277
fprintf(stderr, "Sending PUBLISH packet...\n");
@@ -264,18 +282,20 @@ void mqtt_sn_send_publish(int sock, uint16_t topic_id, uint8_t topic_type, const
264282
void mqtt_sn_send_subscribe_topic_name(int sock, const char* topic_name, uint8_t qos)
265283
{
266284
subscribe_packet_t packet;
285+
size_t topic_name_len = strlen(topic_name);
286+
267287
packet.type = MQTT_SN_TYPE_SUBSCRIBE;
268288
packet.flags = 0x00;
269289
packet.flags += mqtt_sn_get_qos_flag(qos);
270-
if (strlen(topic_name) == 2) {
290+
if (topic_name_len == 2) {
271291
packet.flags += MQTT_SN_TOPIC_TYPE_SHORT;
272292
} else {
273293
packet.flags += MQTT_SN_TOPIC_TYPE_NORMAL;
274294
}
275295
packet.message_id = htons(next_message_id++);
276296
strncpy(packet.topic_name, topic_name, sizeof(packet.topic_name));
277297
packet.topic_name[sizeof(packet.topic_name)-1] = '\0';
278-
packet.length = 0x05 + strlen(topic_name);
298+
packet.length = 0x05 + topic_name_len;
279299

280300
if (debug)
281301
fprintf(stderr, "Sending SUBSCRIBE packet...\n");

0 commit comments

Comments
 (0)