Make the handling of invalid multibyte sequences more robust by using
mbrlen() instead of mblen().
This commit is contained in:
parent
232a681888
commit
d900c38411
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=131183
@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
int bflag;
|
int bflag;
|
||||||
int cflag;
|
int cflag;
|
||||||
@ -239,15 +240,18 @@ b_n_cut(FILE *fp, const char *fname)
|
|||||||
size_t col, i, lbuflen;
|
size_t col, i, lbuflen;
|
||||||
char *lbuf;
|
char *lbuf;
|
||||||
int canwrite, clen, warned;
|
int canwrite, clen, warned;
|
||||||
|
mbstate_t mbs;
|
||||||
|
|
||||||
|
memset(&mbs, 0, sizeof(mbs));
|
||||||
warned = 0;
|
warned = 0;
|
||||||
while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
|
while ((lbuf = fgetln(fp, &lbuflen)) != NULL) {
|
||||||
for (col = 0; lbuflen > 0; col += clen) {
|
for (col = 0; lbuflen > 0; col += clen) {
|
||||||
if ((clen = mblen(lbuf, lbuflen)) < 0) {
|
if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) {
|
||||||
if (!warned) {
|
if (!warned) {
|
||||||
warn("%s", fname);
|
warn("%s", fname);
|
||||||
warned = 1;
|
warned = 1;
|
||||||
}
|
}
|
||||||
|
memset(&mbs, 0, sizeof(mbs));
|
||||||
clen = 1;
|
clen = 1;
|
||||||
}
|
}
|
||||||
if (clen == 0 || *lbuf == '\n')
|
if (clen == 0 || *lbuf == '\n')
|
||||||
|
Loading…
Reference in New Issue
Block a user