Skip to content

Commit aa5a7e9

Browse files
committed
Free memory when reading pending data fails
1 parent c45afbe commit aa5a7e9

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

c_src/ex_dtls/native.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,14 @@ static UnifexPayload **to_payload_array(struct Datagram *dgram_list, int len) {
540540
itr = dgram_list;
541541
struct Datagram *next = dgram_list->next;
542542

543-
while (next != NULL) {
543+
if (next == NULL) {
544544
free(itr);
545-
itr = next;
546-
next = itr->next;
545+
} else {
546+
while (next != NULL) {
547+
free(itr);
548+
itr = next;
549+
next = itr->next;
550+
}
547551
}
548552

549553
return payloads;
@@ -569,8 +573,31 @@ static void read_pending_data(UnifexPayload ***payloads, int *size,
569573
int read_bytes = BIO_read(wbio, payload->data, pending_data_len);
570574
if (read_bytes <= 0) {
571575
DEBUG("WBIO: read error");
572-
// TODO cleanup
576+
free(dgram);
577+
unifex_payload_release(payload);
578+
free(payload);
579+
580+
struct Datagram *ptr = dgram_list;
581+
582+
if (ptr != NULL) {
583+
if (ptr->next == NULL) {
584+
unifex_payload_release(ptr->packet);
585+
free(ptr->packet);
586+
free(ptr);
587+
} else {
588+
struct Datagram *next = ptr->next;
589+
while (next != NULL) {
590+
unifex_payload_release(ptr->packet);
591+
free(ptr->packet);
592+
free(ptr);
593+
ptr = next;
594+
next = ptr->next;
595+
}
596+
}
597+
}
598+
573599
*size = 0;
600+
*payloads = NULL;
574601
return;
575602
} else {
576603
DEBUG("WBIO: read: %d bytes", read_bytes);

0 commit comments

Comments
 (0)