Skip to content

Commit 4797b62

Browse files
Fix memory leaks
1 parent 76089b3 commit 4797b62

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

examples/client_server.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
int main()
1414
{
15-
// Create a v1 PROXY protocol header
16-
pp_info_t pp_info_in = {
15+
/* Create a v1 PROXY protocol header */
16+
pp_info_t pp_info_in_v1 = {
1717
.address_family = ADDR_FAMILY_INET,
1818
.transport_protocol = TRANSPORT_PROTOCOL_STREAM,
1919
.src_addr = "172.22.32.1",
@@ -23,14 +23,16 @@ int main()
2323
};
2424
uint16_t pp1_hdr_len;
2525
uint32_t error;
26-
uint8_t *pp1_hdr = pp_create_hdr(1, &pp_info_in, &pp1_hdr_len, &error);
26+
uint8_t *pp1_hdr = pp_create_hdr(1, &pp_info_in_v1, &pp1_hdr_len, &error);
27+
/* Clear the pp_info passed in pp_create_hdr(). Not really needed for v1 but good to do out of principle */
28+
pp_info_clear(&pp_info_in_v1);
2729
if (error != ERR_NULL)
2830
{
2931
fprintf(stderr, "pp_create_hdr() failed: %s", pp_strerror(error));
3032
return EXIT_FAILURE;
3133
}
3234

33-
// Parse a v1 PROXY protocol header
35+
/* Parse a v1 PROXY protocol header */
3436
pp_info_t pp_info_out;
3537
int32_t rc = pp_parse_hdr(pp1_hdr, pp1_hdr_len, &pp_info_out);
3638
free(pp1_hdr);
@@ -51,9 +53,10 @@ int main()
5153
pp_info_out.src_addr, pp_info_out.dst_addr,
5254
pp_info_out.src_port, pp_info_out.dst_port);
5355
}
56+
/* ALWAYS clear the pp_info after a call to pp_parse_hdr() */
5457
pp_info_clear(&pp_info_out);
5558

56-
// Create a v2 PROXY protocol header with some TLVs
59+
/* Create a v2 PROXY protocol header with some TLVs */
5760
pp_info_t pp_info_in_v2 = {
5861
.address_family = ADDR_FAMILY_INET,
5962
.transport_protocol = TRANSPORT_PROTOCOL_STREAM,
@@ -71,15 +74,20 @@ int main()
7174
}
7275
}
7376
};
77+
/* Add SSL TLVs */
7478
pp_info_add_ssl(&pp_info_in_v2, "TLSv1.2", "ECDHE-RSA-AES128-GCM-SHA256", "SHA256", "RSA2048", "example.com", 11);
79+
/* Add Azure Link ID TLV */
7580
pp_info_add_azure_linkid(&pp_info_in_v2, 1234);
7681
uint8_t *pp2_hdr = pp_create_hdr(2, &pp_info_in_v2, &pp1_hdr_len, &error);
82+
/* IMPORTANT: Clear the pp_info passed in pp_create_hdr() because TLVs were created. Otherwise memory will be leaked */
83+
pp_info_clear(&pp_info_in_v2);
7784
if (error != ERR_NULL)
7885
{
7986
fprintf(stderr, "pp_create_hdr() failed: %s", pp_strerror(error));
8087
return EXIT_FAILURE;
8188
}
8289

90+
/* Parse a v2 PROXY protocol header */
8391
rc = pp_parse_hdr(pp2_hdr, pp1_hdr_len, &pp_info_out);
8492
free(pp2_hdr);
8593
if (!rc)
@@ -106,7 +114,7 @@ int main()
106114
"\tSSL cipher: %s\n"
107115
"\tSSL sig_alg: %s\n"
108116
"\tSSL key_alg: %s\n"
109-
"\tSSL CN: %*s\n"
117+
"\tSSL CN: %.*s\n"
110118
"%s %s %hu %hu\n",
111119
rc, linkid,
112120
pp_info_out.pp2_info.crc32c == 1 ? "verified" : "not present",
@@ -118,6 +126,7 @@ int main()
118126
pp_info_out.src_addr, pp_info_out.dst_addr,
119127
pp_info_out.src_port, pp_info_out.dst_port);
120128
}
129+
/* ALWAYS clear the pp_info after a call to pp_parse_hdr() */
121130
pp_info_clear(&pp_info_out);
122131

123132
return EXIT_SUCCESS;

src/proxy_protocol.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ uint8_t pp_info_add_aws_vpce_id(pp_info_t *pp_info, const char *vpce_id)
351351
pp2_tlv_aws_t *pp2_tlv_aws = malloc(length);
352352
pp2_tlv_aws->type = PP2_SUBTYPE_AWS_VPCE_ID;
353353
memcpy(pp2_tlv_aws->value, vpce_id, strlen(vpce_id));
354-
return tlv_array_append_tlv_new(&pp_info->pp2_info.tlv_array, PP2_TYPE_AWS, length, pp2_tlv_aws);
354+
uint8_t rc = tlv_array_append_tlv_new(&pp_info->pp2_info.tlv_array, PP2_TYPE_AWS, length, pp2_tlv_aws);
355+
free(pp2_tlv_aws);
356+
return rc;
355357
}
356358

357359
uint8_t pp_info_add_azure_linkid(pp_info_t *pp_info, uint32_t linkid)
@@ -360,7 +362,9 @@ uint8_t pp_info_add_azure_linkid(pp_info_t *pp_info, uint32_t linkid)
360362
pp2_tlv_azure_t *pp2_tlv_azure = malloc(length);
361363
pp2_tlv_azure->type = PP2_SUBTYPE_AZURE_PRIVATEENDPOINT_LINKID;
362364
pp2_tlv_azure->linkid = linkid;
363-
return tlv_array_append_tlv_new(&pp_info->pp2_info.tlv_array, PP2_TYPE_AZURE, length, pp2_tlv_azure);
365+
uint8_t rc = tlv_array_append_tlv_new(&pp_info->pp2_info.tlv_array, PP2_TYPE_AZURE, length, pp2_tlv_azure);
366+
free(pp2_tlv_azure);
367+
return rc;
364368
}
365369

366370
static void tlv_array_clear(tlv_array_t *tlv_array)

tests/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ int main()
575575
}
576576

577577
uint8_t *pp_hdr = pp_create_hdr(tests[i].version, &tests[i].pp_info_in, &pp_hdr_len, &error);
578+
pp_info_clear(&tests[i].pp_info_in);
578579
if (!pp_hdr || error != ERR_NULL)
579580
{
580581
printf("FAILED\n");

0 commit comments

Comments
 (0)