Skip to content

Commit

Permalink
pop3-migration: Avoid unnecessarily using stream's hdr_size.
Browse files Browse the repository at this point in the history
Shouldn't change any behavior, except reduce CPU usage a little bit.
  • Loading branch information
sirainen committed Sep 14, 2016
1 parent 9e904c2 commit 061046c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
19 changes: 5 additions & 14 deletions src/plugins/pop3-migration/pop3-migration-plugin.c
Expand Up @@ -173,25 +173,21 @@ pop3_header_filter_callback(struct header_filter_istream *input ATTR_UNUSED,
}

int pop3_migration_get_hdr_sha1(uint32_t mail_seq, struct istream *input,
uoff_t hdr_size,
unsigned char sha1_r[STATIC_ARRAY SHA1_RESULTLEN],
bool *have_eoh_r)
{
struct istream *input2;
const unsigned char *data;
size_t size;
struct sha1_ctxt sha1_ctx;
struct pop3_hdr_context hdr_ctx;

memset(&hdr_ctx, 0, sizeof(hdr_ctx));
input2 = i_stream_create_limit(input, hdr_size);
/* hide headers that might change or be different in IMAP vs. POP3 */
input = i_stream_create_header_filter(input2,
input = i_stream_create_header_filter(input, HEADER_FILTER_HIDE_BODY |
HEADER_FILTER_EXCLUDE | HEADER_FILTER_NO_CR,
hdr_hash_skip_headers,
N_ELEMENTS(hdr_hash_skip_headers),
pop3_header_filter_callback, &hdr_ctx);
i_stream_unref(&input2);

sha1_init(&sha1_ctx);
while (i_stream_read_more(input, &data, &size) > 0) {
Expand Down Expand Up @@ -231,21 +227,18 @@ static int
get_hdr_sha1(struct mail *mail, unsigned char sha1_r[STATIC_ARRAY SHA1_RESULTLEN])
{
struct istream *input;
struct message_size hdr_size;
const char *errstr;
enum mail_error error;
bool have_eoh;
int ret;

if (mail_get_hdr_stream(mail, &hdr_size, &input) < 0) {
if (mail_get_hdr_stream(mail, NULL, &input) < 0) {
errstr = mailbox_get_last_error(mail->box, &error);
i_error("pop3_migration: Failed to get header for msg %u: %s",
mail->seq, errstr);
return error == MAIL_ERROR_EXPUNGED ? 0 : -1;
}
if (pop3_migration_get_hdr_sha1(mail->seq, input,
hdr_size.physical_size,
sha1_r, &have_eoh) < 0)
if (pop3_migration_get_hdr_sha1(mail->seq, input, sha1_r, &have_eoh) < 0)
return -1;
if (have_eoh) {
struct index_mail *imail = (struct index_mail *)mail;
Expand Down Expand Up @@ -274,15 +267,13 @@ get_hdr_sha1(struct mail *mail, unsigned char sha1_r[STATIC_ARRAY SHA1_RESULTLEN
So we'll try to avoid this by falling back to full FETCH BODY[]
(and/or RETR) and we'll parse the header ourself from it. This
should work around any similar bugs in all IMAP/POP3 servers. */
if (mail_get_stream_because(mail, &hdr_size, NULL, "pop3-migration", &input) < 0) {
if (mail_get_stream_because(mail, NULL, NULL, "pop3-migration", &input) < 0) {
errstr = mailbox_get_last_error(mail->box, &error);
i_error("pop3_migration: Failed to get body for msg %u: %s",
mail->seq, errstr);
return error == MAIL_ERROR_EXPUNGED ? 0 : -1;
}
ret = pop3_migration_get_hdr_sha1(mail->seq, input,
hdr_size.physical_size,
sha1_r, &have_eoh);
ret = pop3_migration_get_hdr_sha1(mail->seq, input, sha1_r, &have_eoh);
if (ret == 0) {
if (!have_eoh)
i_warning("pop3_migration: Truncated email with UID %u stored as truncated", mail->uid);
Expand Down
1 change: 0 additions & 1 deletion src/plugins/pop3-migration/pop3-migration-plugin.h
Expand Up @@ -7,7 +7,6 @@ void pop3_migration_plugin_init(struct module *module);
void pop3_migration_plugin_deinit(void);

int pop3_migration_get_hdr_sha1(uint32_t mail_seq, struct istream *input,
uoff_t hdr_size,
unsigned char sha1_r[STATIC_ARRAY SHA1_RESULTLEN],
bool *have_eoh_r);

Expand Down
3 changes: 1 addition & 2 deletions src/plugins/pop3-migration/test-pop3-migration-plugin.c
Expand Up @@ -31,8 +31,7 @@ static void test_pop3_migration_get_hdr_sha1(void)
for (i = 0; i < N_ELEMENTS(tests); i++) {
input = i_stream_create_from_data(tests[i].input,
strlen(tests[i].input));
test_assert_idx(pop3_migration_get_hdr_sha1(1, input, strlen(tests[i].input),
digest, &have_eoh) == 0, i);
test_assert_idx(pop3_migration_get_hdr_sha1(1, input, digest, &have_eoh) == 0, i);
test_assert_idx(strcasecmp(binary_to_hex(digest, sizeof(digest)), tests[i].sha1) == 0, i);
test_assert_idx(tests[i].have_eoh == have_eoh, i);
i_stream_unref(&input);
Expand Down

0 comments on commit 061046c

Please sign in to comment.