Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Aug 2022 16:43:45 GMT
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 814bd1ed438f - main - tools: test: iconv: fix open_2 to not segfault
Message-ID:  <202208111643.27BGhjC4082611@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=814bd1ed438f7dfc5bedcb1f3e772a46fe7026bb

commit 814bd1ed438f7dfc5bedcb1f3e772a46fe7026bb
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2022-01-11 23:41:10 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2022-08-11 16:33:28 +0000

    tools: test: iconv: fix open_2 to not segfault
    
    Record error condition when iconv_open() fails rather than leaving a
    bogus iconv_t that iconv_close() can later choke on; this is one failure
    mode.
    
    If we opened MAX_LIMIT files with success, we need to rewind one so that
    we don't iconv_close() one past the end of cd; this is the second
    failure mode.
    
    Sponsored by:   Klara, Inc.
---
 tools/test/iconv/posix/posix.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/tools/test/iconv/posix/posix.c b/tools/test/iconv/posix/posix.c
index 7faed92416a3..3e813226fa5e 100644
--- a/tools/test/iconv/posix/posix.c
+++ b/tools/test/iconv/posix/posix.c
@@ -27,8 +27,8 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <sys/param.h>
 #include <sys/endian.h>
-#include <sys/types.h>
 
 #include <err.h>
 #include <errno.h>
@@ -66,18 +66,22 @@ static int
 open_2(void)
 {
 	iconv_t cd[MAX_LIMIT];
-	int i, ret;
+	size_t i;
+	int ret;
 
 	errno = 0;
+	ret = 1;
 	for (i = 0; i < MAX_LIMIT; i++) {
 		cd[i] = iconv_open("ASCII", "UTF8");
-		if (cd[i] == (iconv_t)-1)
+		if (cd[i] == (iconv_t)-1) {
+			if (errno == ENFILE || errno == EMFILE)
+				ret = 0;
+			cd[i] = NULL;
 			break;
+		}
 	}
 
-	ret = (cd[i] == (iconv_t)-1) && ((errno == ENFILE) ||
-	    (errno == EMFILE))  ? 0 : 1;
-	for (; i > 0; i--)
+	for (i = MIN(i, nitems(cd) - 1); i > 0; i--)
 		iconv_close(cd[i]);
 	return (ret);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202208111643.27BGhjC4082611>