Skip to content

Commit

Permalink
fix(user-agent): cleanup lingering header node
Browse files Browse the repository at this point in the history
Completely remove lingering header node, previous node should point to
the next
  • Loading branch information
lcsmuller committed Aug 5, 2023
1 parent c4926ad commit 07ff7da
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions core/user-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ ua_conn_add_header(struct ua_conn *conn,
const char value[])
{
size_t fieldlen = strlen(field);
struct curl_slist *node;
struct curl_slist *node, *prev;
char buf[4096];
size_t buflen;
char *ptr;
Expand All @@ -224,15 +224,25 @@ ua_conn_add_header(struct ua_conn *conn,
ASSERT_S(buflen < sizeof(buf), "Out of bounds write attempt");

/* check for match in existing fields */
for (node = conn->header; node != NULL; node = node->next) {
for (prev = NULL, node = conn->header; node != NULL;
prev = node, node = node->next)
{
if (!(ptr = strchr(node->data, ':')))
ERR("Missing ':' in header:\n\t%s", node->data);

if (fieldlen == (size_t)(ptr - node->data)
&& 0 == strncasecmp(node->data, field, fieldlen))
{
if (strlen(node->data) < buflen) {
if (prev) prev->next = node->next;
// XXX: since libcurl uses custom mallocs that offsets the data
// with some metadata, we rely on `curl_free()` to ensure
// proper behavior
curl_free(node->data);
curl_free(node);
// XXX: this process can be optimized by completely replacing
// libcurl's `curl_slist_xxx()` functions with our own
// (see above comment)
curl_slist_append(conn->header, buf);
}
else {
Expand Down

0 comments on commit 07ff7da

Please sign in to comment.