From: Pietro Cerutti Date: Tue, 27 Aug 2019 11:12:48 +0000 (+0000) Subject: Keep spam member of struct Envelope by value X-Git-Tag: 2019-10-25~64^2~4 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ce99404976faa7a51818bf9838deb19800b589a5;p=neomutt Keep spam member of struct Envelope by value --- diff --git a/email/envelope.c b/email/envelope.c index 068de6232..d89332dc5 100644 --- a/email/envelope.c +++ b/email/envelope.c @@ -30,6 +30,7 @@ #include "config.h" #include #include +#include #include "mutt/mutt.h" #include "address/lib.h" #include "envelope.h" @@ -113,7 +114,7 @@ void mutt_env_free(struct Envelope **p) FREE(&(*p)->x_comment_to); #endif - mutt_buffer_free(&(*p)->spam); + mutt_buffer_dealloc(&(*p)->spam); mutt_list_free(&(*p)->references); mutt_list_free(&(*p)->in_reply_to); @@ -153,13 +154,20 @@ void mutt_env_merge(struct Envelope *base, struct Envelope **extra) #define MOVE_STAILQ(member) \ if (STAILQ_EMPTY(&base->member)) \ { \ - STAILQ_SWAP(&base->member, &((*extra))->member, ListNode); \ + STAILQ_SWAP(&base->member, &(*extra)->member, ListNode); \ } #define MOVE_ADDRESSLIST(member) \ if (TAILQ_EMPTY(&base->member)) \ { \ - TAILQ_SWAP(&base->member, &((*extra))->member, Address, entries); \ + TAILQ_SWAP(&base->member, &(*extra)->member, Address, entries); \ + } + +#define MOVE_BUFFER(member) \ + if (mutt_buffer_len(&base->member) == 0) \ + { \ + memcpy(&base->member, &(*extra)->member, sizeof(struct Buffer)); \ + mutt_buffer_init(&(*extra)->member); \ } MOVE_ADDRESSLIST(return_path); @@ -200,11 +208,14 @@ void mutt_env_merge(struct Envelope *base, struct Envelope **extra) } /* spam and user headers should never be hashed, and the new envelope may * have better values. Use new versions regardless. */ - mutt_buffer_free(&base->spam); + mutt_buffer_dealloc(&base->spam); mutt_list_free(&base->userhdrs); - MOVE_ELEM(spam); + MOVE_BUFFER(spam); MOVE_STAILQ(userhdrs); #undef MOVE_ELEM +#undef MOVE_STAILQ +#undef MOVE_ADDRESSLIST +#undef MOVE_BUFFER mutt_env_free(extra); } diff --git a/email/envelope.h b/email/envelope.h index 0824694ad..5d013c096 100644 --- a/email/envelope.h +++ b/email/envelope.h @@ -77,7 +77,7 @@ struct Envelope char *followup_to; ///< List of 'followup-to' fields char *x_comment_to; ///< List of 'X-comment-to' fields #endif - struct Buffer *spam; ///< Spam header + struct Buffer spam; ///< Spam header struct ListHead references; ///< message references (in reverse order) struct ListHead in_reply_to; ///< in-reply-to header content struct ListHead userhdrs; ///< user defined headers diff --git a/email/parse.c b/email/parse.c index aad0640a6..4ccce4964 100644 --- a/email/parse.c +++ b/email/parse.c @@ -1187,35 +1187,35 @@ struct Envelope *mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hd if (!mutt_regexlist_match(&NoSpamList, line)) { /* if spam tag already exists, figure out how to amend it */ - if (env->spam && (*buf != '\0')) + if ((mutt_buffer_len(&env->spam) != 0) && (*buf != '\0')) { /* If C_SpamSeparator defined, append with separator */ if (C_SpamSeparator) { - mutt_buffer_addstr(env->spam, C_SpamSeparator); - mutt_buffer_addstr(env->spam, buf); + mutt_buffer_addstr(&env->spam, C_SpamSeparator); + mutt_buffer_addstr(&env->spam, buf); } else /* overwrite */ { - mutt_buffer_reset(env->spam); - mutt_buffer_addstr(env->spam, buf); + mutt_buffer_reset(&env->spam); + mutt_buffer_addstr(&env->spam, buf); } } /* spam tag is new, and match expr is non-empty; copy */ - else if (!env->spam && (*buf != '\0')) + else if ((mutt_buffer_len(&env->spam) == 0) && (*buf != '\0')) { - env->spam = mutt_buffer_from(buf); + mutt_buffer_addstr(&env->spam, buf); } /* match expr is empty; plug in null string if no existing tag */ - else if (!env->spam) + else if (mutt_buffer_len(&env->spam) == 0) { - env->spam = mutt_buffer_from(""); + mutt_buffer_addstr(&env->spam, ""); } - if (env->spam && env->spam->data) - mutt_debug(LL_DEBUG5, "spam = %s\n", env->spam->data); + if (mutt_buffer_len(&env->spam) != 0) + mutt_debug(LL_DEBUG5, "spam = %s\n", env->spam.data); } } diff --git a/hcache/serialize.c b/hcache/serialize.c index 94da047e4..4f48f392f 100644 --- a/hcache/serialize.c +++ b/hcache/serialize.c @@ -328,7 +328,7 @@ unsigned char *serial_dump_buffer(struct Buffer *b, unsigned char *d, int *off, * @param[out] off Offset into the blob * @param[in] convert If true, the strings will be converted from utf-8 */ -void serial_restore_buffer(struct Buffer **b, const unsigned char *d, int *off, bool convert) +void serial_restore_buffer(struct Buffer *b, const unsigned char *d, int *off, bool convert) { unsigned int used; unsigned int offset; @@ -338,13 +338,11 @@ void serial_restore_buffer(struct Buffer **b, const unsigned char *d, int *off, return; } - *b = mutt_mem_malloc(sizeof(struct Buffer)); - - serial_restore_char(&(*b)->data, d, off, convert); + serial_restore_char(&b->data, d, off, convert); serial_restore_int(&offset, d, off); - (*b)->dptr = (*b)->data + offset; + b->dptr = b->data + offset; serial_restore_int(&used, d, off); - (*b)->dsize = used; + b->dsize = used; } /** @@ -501,7 +499,7 @@ unsigned char *serial_dump_envelope(struct Envelope *env, unsigned char *d, d = serial_dump_char(env->x_label, d, off, convert); d = serial_dump_char(env->organization, d, off, convert); - d = serial_dump_buffer(env->spam, d, off, convert); + d = serial_dump_buffer(&env->spam, d, off, convert); d = serial_dump_stailq(&env->references, d, off, false); d = serial_dump_stailq(&env->in_reply_to, d, off, false); diff --git a/hcache/serialize.h b/hcache/serialize.h index e3f317b8e..495c2e195 100644 --- a/hcache/serialize.h +++ b/hcache/serialize.h @@ -51,7 +51,7 @@ unsigned char *serial_dump_stailq(struct ListHead *l, unsigned char *d, int *off void serial_restore_address(struct Address **a, const unsigned char *d, int *off, bool convert); void serial_restore_body(struct Body *c, const unsigned char *d, int *off, bool convert); -void serial_restore_buffer(struct Buffer **b, const unsigned char *d, int *off, bool convert); +void serial_restore_buffer(struct Buffer *b, const unsigned char *d, int *off, bool convert); void serial_restore_char(char **c, const unsigned char *d, int *off, bool convert); void serial_restore_envelope(struct Envelope *e, const unsigned char *d, int *off, bool convert); void serial_restore_int(unsigned int *i, const unsigned char *d, int *off); diff --git a/hdrline.c b/hdrline.c index 711f79b4a..996d93418 100644 --- a/hdrline.c +++ b/hdrline.c @@ -955,12 +955,9 @@ static const char *index_format_str(char *buf, size_t buflen, size_t col, int co case 'H': /* (Hormel) spam score */ if (optional) - optional = e->env->spam; + optional = mutt_buffer_len(&e->env->spam) != 0; - if (e->env->spam) - mutt_format_s(buf, buflen, prec, NONULL(e->env->spam->data)); - else - mutt_format_s(buf, buflen, prec, ""); + mutt_format_s(buf, buflen, prec, mutt_b2s(&e->env->spam)); break; case 'i': @@ -1276,7 +1273,7 @@ static const char *index_format_str(char *buf, size_t buflen, size_t col, int co case 'y': if (optional) - optional = e->env->x_label; + optional = (e->env->x_label != NULL); colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_LABEL); mutt_format_s(buf + colorlen, buflen - colorlen, prec, NONULL(e->env->x_label)); diff --git a/pattern.c b/pattern.c index 5823119fe..9b403725c 100644 --- a/pattern.c +++ b/pattern.c @@ -2240,8 +2240,7 @@ int mutt_pattern_exec(struct Pattern *pat, PatternExecFlags flags, case MUTT_PAT_HORMEL: if (!e->env) return 0; - return pat->pat_not ^ (e->env->spam && e->env->spam->data && - patmatch(pat, e->env->spam->data)); + return pat->pat_not ^ (e->env->spam.data && patmatch(pat, e->env->spam.data)); case MUTT_PAT_DUPLICATED: return pat->pat_not ^ (e->thread && e->thread->duplicate_thread); case MUTT_PAT_MIMEATTACH: diff --git a/sort.c b/sort.c index bc295e45c..fb5c41b69 100644 --- a/sort.c +++ b/sort.c @@ -239,8 +239,8 @@ static int compare_spam(const void *a, const void *b) /* Firstly, require spam attributes for both msgs */ /* to compare. Determine which msgs have one. */ - ahas = (*ppa)->env && (*ppa)->env->spam; - bhas = (*ppb)->env && (*ppb)->env->spam; + ahas = (*ppa)->env && mutt_buffer_len(&(*ppa)->env->spam) != 0; + bhas = (*ppb)->env && mutt_buffer_len(&(*ppb)->env->spam) != 0; /* If one msg has spam attr but other does not, sort the one with first. */ if (ahas && !bhas) @@ -259,14 +259,14 @@ static int compare_spam(const void *a, const void *b) /* preliminary numeric examination */ difference = - (strtod((*ppa)->env->spam->data, &aptr) - strtod((*ppb)->env->spam->data, &bptr)); + (strtod((*ppa)->env->spam.data, &aptr) - strtod((*ppb)->env->spam.data, &bptr)); /* map double into comparison (-1, 0, or 1) */ result = ((difference < 0.0) ? -1 : (difference > 0.0) ? 1 : 0); /* If either aptr or bptr is equal to data, there is no numeric */ /* value for that spam attribute. In this case, compare lexically. */ - if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data)) + if ((aptr == (*ppa)->env->spam.data) || (bptr == (*ppb)->env->spam.data)) return SORT_CODE(strcmp(aptr, bptr)); /* Otherwise, we have numeric value for both attrs. If these values */ diff --git a/test/Makefile.autosetup b/test/Makefile.autosetup index d9679bf6a..cdd1c9597 100644 --- a/test/Makefile.autosetup +++ b/test/Makefile.autosetup @@ -46,7 +46,6 @@ BUFFER_OBJS = test/buffer/mutt_buffer_addch.o \ test/buffer/mutt_buffer_concat_path.o \ test/buffer/mutt_buffer_fix_dptr.o \ test/buffer/mutt_buffer_free.o \ - test/buffer/mutt_buffer_from.o \ test/buffer/mutt_buffer_init.o \ test/buffer/mutt_buffer_is_empty.o \ test/buffer/mutt_buffer_len.o \ diff --git a/test/buffer/mutt_buffer_from.c b/test/buffer/mutt_buffer_from.c deleted file mode 100644 index eec560bac..000000000 --- a/test/buffer/mutt_buffer_from.c +++ /dev/null @@ -1,59 +0,0 @@ -/** - * @file - * Test code for mutt_buffer_from() - * - * @authors - * Copyright (C) 2019 Richard Russon - * - * @copyright - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#define TEST_NO_MAIN -#include "acutest.h" -#include "config.h" -#include "mutt/mutt.h" - -void test_mutt_buffer_from(void) -{ - // struct Buffer *mutt_buffer_from(const char *seed); - - { - TEST_CHECK(!mutt_buffer_from(NULL)); - } - - { - struct Buffer *buf = NULL; - const char *str = ""; - - TEST_CHECK((buf = mutt_buffer_from(str)) != NULL); - TEST_CHECK(buf->data == NULL); - TEST_CHECK(buf->dptr == NULL); - TEST_CHECK(buf->dsize == 0); - - mutt_buffer_free(&buf); - } - - { - struct Buffer *buf = NULL; - const char *str = "test"; - - TEST_CHECK((buf = mutt_buffer_from(str)) != NULL); - TEST_CHECK(buf->data != NULL); - TEST_CHECK(buf->dptr != NULL); - TEST_CHECK(buf->dsize != 0); - - mutt_buffer_free(&buf); - } -} diff --git a/test/main.c b/test/main.c index 6eab1010a..c870f9fbc 100644 --- a/test/main.c +++ b/test/main.c @@ -71,7 +71,6 @@ NEOMUTT_TEST_ITEM(test_mutt_buffer_concat_path) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_fix_dptr) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_free) \ - NEOMUTT_TEST_ITEM(test_mutt_buffer_from) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_init) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_is_empty) \ NEOMUTT_TEST_ITEM(test_mutt_buffer_len) \