Skip to content

Commit

Permalink
Merge pull request #5045 from ksmurchison/spool_less_strdup
Browse files Browse the repository at this point in the history
spool.c: use a struct buf in struct hdrcache_t for lowercasing header names
  • Loading branch information
ksmurchison authored Sep 18, 2024
2 parents a40227d + b8a126a commit c75865d
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion imap/caldav_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ EXPORTED int caldav_store_resource(struct transaction_t *txn, icalcomponent *ica
spool_replace_header(xstrdup("Content-Disposition"),
buf_release(&txn->buf), txn->req_hdrs);

spool_remove_header(xstrdup("Content-Description"), txn->req_hdrs);
spool_remove_header("Content-Description", txn->req_hdrs);

/* Store the resource */
ret = dav_store_resource(txn, icalcomponent_as_ical_string(store_ical), 0,
Expand Down
4 changes: 2 additions & 2 deletions imap/http_carddav.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ static int carddav_store_resource(struct transaction_t *txn,
spool_replace_header(xstrdup("Content-Disposition"),
buf_release(&txn->buf), txn->req_hdrs);

spool_remove_header(xstrdup("Content-Description"), txn->req_hdrs);
spool_remove_header("Content-Description", txn->req_hdrs);

/* Store the resource */
r = dav_store_resource(txn, buf_cstring(buf), 0,
Expand Down Expand Up @@ -833,7 +833,7 @@ static int carddav_store_resource(struct transaction_t *txn,
spool_replace_header(xstrdup("Content-Disposition"),
buf_release(&txn->buf), txn->req_hdrs);

spool_remove_header(xstrdup("Content-Description"), txn->req_hdrs);
spool_remove_header("Content-Description", txn->req_hdrs);

/* Store the resource */
int r = dav_store_resource(txn, buf_cstring(buf), 0,
Expand Down
4 changes: 2 additions & 2 deletions imap/jmap_sieve.c
Original file line number Diff line number Diff line change
Expand Up @@ -1773,14 +1773,14 @@ static int deleteheader(void *mc, const char *head, int index)
json_t *args = json_object();

if (index) {
spool_remove_header_instance(xstrdup(head), index, m->cache);
spool_remove_header_instance(head, index, m->cache);

json_object_set_new(args, "index", json_integer(abs(index)));
if (index < 0)
json_object_set_new(args, "last", json_true());
}
else {
spool_remove_header(xstrdup(head), m->cache);
spool_remove_header(head, m->cache);
}

json_array_append_new(m->actions,
Expand Down
4 changes: 2 additions & 2 deletions imap/lmtp_sieve.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ static int deleteheader(void *mc, const char *head, int index)

if (head == NULL) return SIEVE_FAIL;

if (!index) spool_remove_header(xstrdup(head), m->hdrcache);
else spool_remove_header_instance(xstrdup(head), index, m->hdrcache);
if (!index) spool_remove_header(head, m->hdrcache);
else spool_remove_header_instance(head, index, m->hdrcache);

return SIEVE_OK;
}
Expand Down
37 changes: 19 additions & 18 deletions imap/spool.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct hdrcache_t {
struct header_t *head; /* head of double-linked list of ordered headers */
struct header_t *tail; /* tail of double-linked list of ordered headers */
ptrarray_t getheader_cache; /* header bodies returned by spool_getheader() */
struct buf buf; /* buffer for lowercasing header names */
};

EXPORTED hdrcache_t spool_new_hdrcache(void)
Expand Down Expand Up @@ -311,7 +312,7 @@ static int parseheader(struct protstream *fin, FILE *fout,
}

static struct header_t *__spool_cache_header(char *name, char *body, char *raw,
hash_table *table)
hdrcache_t cache)
{
ptrarray_t *contents;
struct header_t *hdr = xzmalloc(sizeof(struct header_t));
Expand All @@ -321,20 +322,20 @@ static struct header_t *__spool_cache_header(char *name, char *body, char *raw,
hdr->raw = raw;

/* add header to hash table */
char *lcname = lcase(xstrdup(name));
buf_setcstr(&cache->buf, name);
const char *lcname = buf_lcase(&cache->buf);
hash_table *table = &cache->cache;
contents = (ptrarray_t *) hash_lookup(lcname, table);

if (!contents) contents = hash_insert(lcname, ptrarray_new(), table);
ptrarray_append(contents, hdr);

free(lcname);

return hdr;
}

EXPORTED void spool_prepend_header_raw(char *name, char *body, char *raw, hdrcache_t cache)
{
struct header_t *hdr = __spool_cache_header(name, body, raw, &cache->cache);
struct header_t *hdr = __spool_cache_header(name, body, raw, cache);

/* link header at head of list */
hdr->next = cache->head;
Expand All @@ -353,7 +354,7 @@ EXPORTED void spool_prepend_header(char *name, char *body, hdrcache_t cache)

EXPORTED void spool_append_header_raw(char *name, char *body, char *raw, hdrcache_t cache)
{
struct header_t *hdr = __spool_cache_header(name, body, raw, &cache->cache);
struct header_t *hdr = __spool_cache_header(name, body, raw, cache);

/* link header at tail of list */
hdr->prev = cache->tail;
Expand All @@ -371,15 +372,17 @@ EXPORTED void spool_append_header(char *name, char *body, hdrcache_t cache)

EXPORTED void spool_replace_header(char *name, char *body, hdrcache_t cache)
{
spool_remove_header(xstrdup(name), cache);
spool_remove_header(name, cache);
spool_append_header(name, body, cache);
}

static void __spool_remove_header(char *name, int first, int last,
static void __spool_remove_header(const char *name, int first, int last,
hdrcache_t cache)
{
buf_setcstr(&cache->buf, name);

ptrarray_t *contents =
(ptrarray_t *) hash_lookup(lcase(name), &cache->cache);
(ptrarray_t *) hash_lookup(buf_lcase(&cache->buf), &cache->cache);

if (contents) {
int idx;
Expand Down Expand Up @@ -409,16 +412,15 @@ static void __spool_remove_header(char *name, int first, int last,
free(hdr);
}
}

free(name);
}

EXPORTED void spool_remove_header(char *name, hdrcache_t cache)
EXPORTED void spool_remove_header(const char *name, hdrcache_t cache)
{
__spool_remove_header(name, 0, -1, cache);
}

EXPORTED void spool_remove_header_instance(char *name, int n, hdrcache_t cache)
EXPORTED void spool_remove_header_instance(const char *name, int n,
hdrcache_t cache)
{
if (!n) return;
if (n > 0) n--; /* normalize to zero */
Expand Down Expand Up @@ -454,19 +456,17 @@ EXPORTED int spool_fill_hdrcache(struct protstream *fin, FILE *fout,

EXPORTED const char **spool_getheader(hdrcache_t cache, const char *phead)
{
char *head;
const char *head;
ptrarray_t *contents;

assert(cache && phead);

head = xstrdup(phead);
lcase(head);
buf_setcstr(&cache->buf, phead);
head = buf_lcase(&cache->buf);

/* check the cache */
contents = (ptrarray_t *) hash_lookup(head, &cache->cache);

free(head);

if (contents && ptrarray_size(contents)) {
strarray_t *array = strarray_new();
/* build read-only array of header bodies */
Expand Down Expand Up @@ -514,6 +514,7 @@ EXPORTED void spool_free_hdrcache(hdrcache_t cache)
strarray_free(item);
}
ptrarray_fini(&cache->getheader_cache);
buf_free(&cache->buf);

free(cache);
}
Expand Down
4 changes: 2 additions & 2 deletions imap/spool.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ void spool_append_header_raw(char *name, char *body, char *raw, hdrcache_t cache
#define spool_cache_header(n, b, c) spool_append_header(n, b, c)
void spool_replace_header(char *name, char *newvalue, hdrcache_t cache);
/* remove all instances of header 'name' */
void spool_remove_header(char *name, hdrcache_t cache);
void spool_remove_header(const char *name, hdrcache_t cache);
/* remove nth instance of header 'name'. 1 = first, -1 = last */
void spool_remove_header_instance(char *name, int n, hdrcache_t cache);
void spool_remove_header_instance(const char *name, int n, hdrcache_t cache);
int spool_fill_hdrcache(struct protstream *fin, FILE *fout, hdrcache_t cache,
const char **skipheaders);
const char **spool_getheader(hdrcache_t cache, const char *phead);
Expand Down
4 changes: 2 additions & 2 deletions sieve/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ static int deleteheader(void *mc, const char *head, int index)

if (!index) {
printf("removing all headers '%s'\n", head);
spool_remove_header(xstrdup(head), m->cache);
spool_remove_header(head, m->cache);
}
else {
printf("removing header '%s[%d]'\n", head, index);
spool_remove_header_instance(xstrdup(head), index, m->cache);
spool_remove_header_instance(head, index, m->cache);
}

return SIEVE_OK;
Expand Down
4 changes: 2 additions & 2 deletions sieve/test_mailbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ static int deleteheader(void *mc, const char *head, int index)

if (!index) {
printf("removing all headers '%s'\n", head);
spool_remove_header(xstrdup(head), m->cache);
spool_remove_header(head, m->cache);
}
else {
printf("removing header '%s[%d]'\n", head, index);
spool_remove_header_instance(xstrdup(head), index, m->cache);
spool_remove_header_instance(head, index, m->cache);
}

return SIEVE_OK;
Expand Down

0 comments on commit c75865d

Please sign in to comment.