diff options
author | tjr <tjr@FreeBSD.org> | 2004-11-05 10:45:23 +0000 |
---|---|---|
committer | tjr <tjr@FreeBSD.org> | 2004-11-05 10:45:23 +0000 |
commit | 0473e0491d39671840bd745d328f7d816113176a (patch) | |
tree | 032710cccc0519247a199420226398dfab469f4d /usr.bin | |
parent | 9428ecfc37e4b963361e090d5216345172fe9bb0 (diff) | |
download | FreeBSD-src-0473e0491d39671840bd745d328f7d816113176a.zip FreeBSD-src-0473e0491d39671840bd745d328f7d816113176a.tar.gz |
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.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/cut/cut.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/usr.bin/cut/cut.c b/usr.bin/cut/cut.c index 2e334f6..a370e60 100644 --- a/usr.bin/cut/cut.c +++ b/usr.bin/cut/cut.c @@ -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); |