git: 489c9df19851 - stable/13 - sort: replace home made line reader by getdelim(3)

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Wed, 19 Oct 2022 08:01:26 UTC
The branch stable/13 has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=489c9df19851aa052553a79de65e37ea1471014e

commit 489c9df19851aa052553a79de65e37ea1471014e
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2022-10-12 13:31:50 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2022-10-19 07:59:28 +0000

    sort: replace home made line reader by getdelim(3)
    
    The previous code had bug when reading lines with an unexpected
    encoding, returning without the full line being captured.
    This result in sort complaining with "sort: Illegal byte sequence"
    
    Using getdelim(3) instead of the home made code, fixes the situation.
    
    PR:             241679
    Reported by:    Ronald F. Guilmette <rfg-freebsd@tristatelogic.com>
    MFC After:      1 week
    Reviewed by:    markj, imp
    Differential Revision:  https://reviews.freebsd.org/D36948
    
    (cherry picked from commit b58094c0d98e5d0ab8abdcdb655ac902ae8ad66e)
---
 usr.bin/sort/bwstring.c | 109 ------------------------------------------------
 usr.bin/sort/file.c     |  18 +++++---
 2 files changed, 12 insertions(+), 115 deletions(-)

diff --git a/usr.bin/sort/bwstring.c b/usr.bin/sort/bwstring.c
index 2f2737e94314..52173002986f 100644
--- a/usr.bin/sort/bwstring.c
+++ b/usr.bin/sort/bwstring.c
@@ -470,115 +470,6 @@ bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended)
 	}
 }
 
-/*
- * Allocate and read a binary string from file.
- * The strings are nl-ended or zero-ended, depending on the sort setting.
- */
-struct bwstring *
-bwsfgetln(FILE *f, size_t *len, bool zero_ended, struct reader_buffer *rb)
-{
-	wint_t eols;
-
-	eols = zero_ended ? btowc('\0') : btowc('\n');
-
-	if (!zero_ended && (mb_cur_max > 1)) {
-		wchar_t *ret;
-
-		ret = fgetwln(f, len);
-
-		if (ret == NULL) {
-			if (!feof(f))
-				err(2, NULL);
-			return (NULL);
-		}
-		if (*len > 0) {
-			if (ret[*len - 1] == (wchar_t)eols)
-				--(*len);
-		}
-		return (bwssbdup(ret, *len));
-
-	} else if (!zero_ended && (mb_cur_max == 1)) {
-		char *ret;
-
-		ret = fgetln(f, len);
-
-		if (ret == NULL) {
-			if (!feof(f))
-				err(2, NULL);
-			return (NULL);
-		}
-		if (*len > 0) {
-			if (ret[*len - 1] == '\n')
-				--(*len);
-		}
-		return (bwscsbdup((unsigned char *)ret, *len));
-
-	} else {
-		*len = 0;
-
-		if (feof(f))
-			return (NULL);
-
-		if (2 >= rb->fgetwln_z_buffer_size) {
-			rb->fgetwln_z_buffer_size += 256;
-			rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
-			    sizeof(wchar_t) * rb->fgetwln_z_buffer_size);
-		}
-		rb->fgetwln_z_buffer[*len] = 0;
-
-		if (mb_cur_max == 1)
-			while (!feof(f)) {
-				int c;
-
-				c = fgetc(f);
-
-				if (c == EOF) {
-					if (*len == 0)
-						return (NULL);
-					goto line_read_done;
-				}
-				if (c == eols)
-					goto line_read_done;
-
-				if (*len + 1 >= rb->fgetwln_z_buffer_size) {
-					rb->fgetwln_z_buffer_size += 256;
-					rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
-					    SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
-				}
-
-				rb->fgetwln_z_buffer[*len] = c;
-				rb->fgetwln_z_buffer[++(*len)] = 0;
-			}
-		else
-			while (!feof(f)) {
-				wint_t c;
-
-				c = fgetwc(f);
-
-				if (c == WEOF) {
-					if (*len == 0)
-						return (NULL);
-					goto line_read_done;
-				}
-				if (c == eols)
-					goto line_read_done;
-
-				if (*len + 1 >= rb->fgetwln_z_buffer_size) {
-					rb->fgetwln_z_buffer_size += 256;
-					rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
-					    SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
-				}
-
-				rb->fgetwln_z_buffer[*len] = c;
-				rb->fgetwln_z_buffer[++(*len)] = 0;
-			}
-
-line_read_done:
-		/* we do not count the last 0 */
-		return (bwssbdup(rb->fgetwln_z_buffer, *len));
-	}
-}
-
 int
 bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2,
     size_t offset, size_t len)
diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c
index 1fb02642b5c9..ffcd71e9f991 100644
--- a/usr.bin/sort/file.c
+++ b/usr.bin/sort/file.c
@@ -75,7 +75,7 @@ struct file_reader
 	struct reader_buffer	 rb;
 	FILE			*file;
 	char			*fname;
-	unsigned char		*buffer;
+	char			*buffer;
 	unsigned char		*mmapaddr;
 	unsigned char		*mmapptr;
 	size_t			 bsz;
@@ -713,7 +713,7 @@ file_reader_readline(struct file_reader *fr)
 		}
 
 	} else if (fr->file != stdin) {
-		unsigned char *strend;
+		char *strend;
 		size_t bsz1, remsz, search_start;
 
 		search_start = 0;
@@ -785,10 +785,16 @@ file_reader_readline(struct file_reader *fr)
 		fr->strbeg = (strend - fr->buffer) + 1;
 
 	} else {
-		size_t len = 0;
-
-		ret = bwsfgetln(fr->file, &len, sort_opts_vals.zflag,
-		    &(fr->rb));
+		int delim = sort_opts_vals.zflag ? '\0' : '\n';
+		ssize_t len = getdelim(&fr->buffer, &fr->bsz, delim, fr->file);
+		if (len < 0) {
+			if (!feof(fr->file))
+				err(2, NULL);
+			return (NULL);
+		}
+		if (len > 0 && fr->buffer[len - 1] == delim)
+			len--;
+		ret = bwscsbdup(fr->buffer, len);
 	}
 
 	return (ret);