Skip to content

Commit d709bc0

Browse files
Copilotithewei
andcommitted
Fix potential bugs: asn1_encode constant, mqtt_head_unpack bounds check, mqtt_next_mid wrap, unpack underflow, on_close null check
Co-authored-by: ithewei <26049660+ithewei@users.noreply.github.com>
1 parent 1ef15a2 commit d709bc0

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

base/hmath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static inline int asn1_encode(long long value, unsigned char* buf) {
9292
*p = (unsigned char)value;
9393
return 3;
9494
}
95-
else if (value < 16777126)
95+
else if (value < 16777216)
9696
{
9797
*p = 0x83;
9898
p++;

event/unpack.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ int hio_unpack_by_length_field(hio_t* io, void* buf, int readbytes) {
157157
hio_close(io);
158158
return -1;
159159
}
160-
package_len = head_len + body_len + setting->length_adjustment;
160+
int signed_package_len = (int)head_len + (int)body_len + setting->length_adjustment;
161+
if (signed_package_len <= 0 || signed_package_len > (int)setting->package_max_length) {
162+
hloge("Invalid package length %d!", signed_package_len);
163+
io->error = ERR_OVER_LIMIT;
164+
hio_close(io);
165+
return -1;
166+
}
167+
package_len = (unsigned int)signed_package_len;
161168
if (remain >= package_len) {
162169
hio_read_cb(io, (void*)p, package_len);
163170
handled += package_len;

mqtt/mqtt_client.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
static unsigned short mqtt_next_mid() {
1010
static unsigned short s_mid = 0;
11-
return ++s_mid;
11+
if (++s_mid == 0) s_mid = 1;
12+
return s_mid;
1213
}
1314

1415
static int mqtt_client_send(mqtt_client_t* cli, const void* buf, int len) {
@@ -231,6 +232,7 @@ static void mqtt_client_add_reconnect_timer(mqtt_client_t* cli) {
231232

232233
static void on_close(hio_t* io) {
233234
mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(io);
235+
if (cli == NULL) return;
234236
cli->connected = 0;
235237
if (cli->cb) {
236238
cli->head.type = MQTT_TYPE_DISCONNECT;

mqtt/mqtt_protocol.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ int mqtt_head_pack(mqtt_head_t* head, unsigned char buf[]) {
1111
}
1212

1313
int mqtt_head_unpack(mqtt_head_t* head, const unsigned char* buf, int len) {
14+
if (len < 2) return 0;
1415
head->type = (buf[0] >> 4) & 0x0F;
1516
head->dup = (buf[0] >> 3) & 0x01;
1617
head->qos = (buf[0] >> 1) & 0x03;

0 commit comments

Comments
 (0)