When the last line of a file is missing a newline in -f mode, pass a

length argument to mbrtowc() that accounts for the terminating newline
character we add automatically. Failing to do this caused the loop to
unexpectedly run out of characters and incorrectly signal an "Illegal byte
sequence" error.
This commit is contained in:
tjr 2004-11-05 10:45:23 +00:00
parent 9428ecfc37
commit 0473e0491d

View File

@ -371,10 +371,11 @@ f_cut(FILE *fp, const char *fname)
wchar_t sep;
int output;
char *lbuf, *mlbuf;
size_t clen, lbuflen;
size_t clen, lbuflen, reallen;
mlbuf = NULL;
for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) {
reallen = lbuflen;
/* Assert EOL has a newline. */
if (*(lbuf + lbuflen - 1) != '\n') {
/* Can't have > 1 line with no trailing newline. */
@ -384,10 +385,11 @@ f_cut(FILE *fp, const char *fname)
memcpy(mlbuf, lbuf, lbuflen);
*(mlbuf + lbuflen) = '\n';
lbuf = mlbuf;
reallen++;
}
output = 0;
for (isdelim = 0, p = lbuf;; p += clen) {
clen = mbrtowc(&ch, p, lbuf + lbuflen - p, NULL);
clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL);
if (clen == (size_t)-1 || clen == (size_t)-2) {
warnc(EILSEQ, "%s", fname);
free(mlbuf);
@ -413,7 +415,7 @@ f_cut(FILE *fp, const char *fname)
for (i = 0; dcharmb[i] != '\0'; i++)
putchar(dcharmb[i]);
for (;;) {
clen = mbrtowc(&ch, p, lbuf + lbuflen - p,
clen = mbrtowc(&ch, p, lbuf + reallen - p,
NULL);
if (clen == (size_t)-1 || clen == (size_t)-2) {
warnc(EILSEQ, "%s", fname);