git: 42b5d30b9dfe - stable/12 - iconv: only conditionally use ICONV_SET_DISCARD_ILSEQ

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Sun, 18 Sep 2022 06:27:15 UTC
The branch stable/12 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=42b5d30b9dfe090435acb878cf500f0fa306a197

commit 42b5d30b9dfe090435acb878cf500f0fa306a197
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2022-02-22 05:05:28 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2022-09-17 19:30:09 +0000

    iconv: only conditionally use ICONV_SET_DISCARD_ILSEQ
    
    If the -c flag is used, then we can set it with ICONV_SET_DISCARD_ILSEQ;
    otherwise, leave it alone.  The user may have specified //IGNORE in the
    'to' codeset specification, there's no reason we can't allow that but
    we'll currently turn it off.
    
    Reviewed by:    thj
    Sponsored by: Klara, Inc.
    
    (cherry picked from commit ea0f37dec65daf2b7e05712738cd1720aae129eb)
---
 usr.bin/iconv/iconv.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/usr.bin/iconv/iconv.c b/usr.bin/iconv/iconv.c
index 7e911b4432e5..ba099f8af520 100644
--- a/usr.bin/iconv/iconv.c
+++ b/usr.bin/iconv/iconv.c
@@ -77,9 +77,16 @@ do_conv(FILE *fp, iconv_t cd, bool silent, bool hide_invalid)
 	unsigned long long invalids;
 	size_t inbytes, outbytes, ret;
 
-	int arg = (int)hide_invalid;
-	if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&arg) == -1)
-		err(EXIT_FAILURE, "iconvctl(DISCARD_ILSEQ, %d)", arg);
+	/*
+	 * Don't touch ICONV_SET_DISCARD_ILSEQ if -c wasn't specified.  It may
+	 * be that the user has specified //IGNORE in the -t specification, and
+	 * we don't want to clobber that.
+	 */
+	if (hide_invalid) {
+		int arg = (int)hide_invalid;
+		if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&arg) == -1)
+			err(EXIT_FAILURE, "iconvctl(DISCARD_ILSEQ, %d)", arg);
+	}
 
 	invalids = 0;
 	while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {