Skip to content

Commit

Permalink
mdeliver: ignore last empty line of mbox entries
Browse files Browse the repository at this point in the history
https://www.loc.gov/preservation/digital/formats/fdd/fdd000383.shtml
> Each message is immediately prefaced by a separation line and
> terminated by an empty line.

Bug discovered by skarnet.

Fixes #207.
  • Loading branch information
leahneukirchen committed May 9, 2021
1 parent cb47f81 commit 5ab11c7
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions mdeliver.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ deliver(char *infilename)

int in_header = 1;
int is_old = 0;
int prev_line_empty = 0;
int this_line_empty = 0; // only for mbox parsing
while (1) {
errno = 0;
ssize_t rd = getdelim(&line, &linelen, '\n', infile);
Expand All @@ -158,8 +160,12 @@ deliver(char *infilename)
char *line_start = line;

if (line[0] == '\n' && (!line[1] ||
(line[1] == '\r' && !line[2])))
(line[1] == '\r' && !line[2]))) {
this_line_empty = Mflag ? 1 : 0;
in_header = 0;
} else {
this_line_empty = 0;
}

if (Mflag && strncmp("From ", line, 5) == 0)
break;
Expand Down Expand Up @@ -189,8 +195,15 @@ deliver(char *infilename)
}
}

if (fwrite(line_start, 1, rd, outfile) != (size_t)rd)
goto fail;
// print delayed empty line
if (prev_line_empty)
if (fputc('\n', outfile) == EOF)
goto fail;
if (!this_line_empty)
if (fwrite(line_start, 1, rd, outfile) != (size_t)rd)
goto fail;

prev_line_empty = this_line_empty;
}
if (fflush(outfile) == EOF)
goto fail;
Expand Down

0 comments on commit 5ab11c7

Please sign in to comment.