git: 117c54a78ccd - main - split: Fix linecount parsing.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Wed, 06 Sep 2023 03:29:53 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=117c54a78ccd214c236806721f21da750e512d3e

commit 117c54a78ccd214c236806721f21da750e512d3e
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2023-09-06 03:28:30 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2023-09-06 03:29:31 +0000

    split: Fix linecount parsing.
    
    The “undocumented kludge” which unfortunately can't be dropped for backward compatibility reasons was prone to segfaulting and would improperly allow a new linecount when one was already set.  Fix these issues and add regression tests.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans
    Differential Revision:  https://reviews.freebsd.org/D41757
---
 usr.bin/split/split.c             | 22 ++++++++++------------
 usr.bin/split/tests/split_test.sh | 21 +++++++++++++++++++++
 2 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/usr.bin/split/split.c b/usr.bin/split/split.c
index 6894e9e63195..af1ed69c9482 100644
--- a/usr.bin/split/split.c
+++ b/usr.bin/split/split.c
@@ -91,7 +91,7 @@ main(int argc, char **argv)
 	setlocale(LC_ALL, "");
 
 	dflag = false;
-	while ((ch = getopt(argc, argv, "0123456789a:b:cdl:n:p:")) != -1)
+	while ((ch = getopt(argc, argv, "0::1::2::3::4::5::6::7::8::9::a:b:cdl:n:p:")) != -1)
 		switch (ch) {
 		case '0': case '1': case '2': case '3': case '4':
 		case '5': case '6': case '7': case '8': case '9':
@@ -99,17 +99,15 @@ main(int argc, char **argv)
 			 * Undocumented kludge: split was originally designed
 			 * to take a number after a dash.
 			 */
-			if (numlines == 0) {
-				p = argv[optind - 1];
-				if (p[0] == '-' && p[1] == ch && !p[2])
-					numlines = strtol(++p, &ep, 10);
-				else
-					numlines =
-					    strtol(argv[optind] + 1, &ep, 10);
-				if (numlines <= 0 || *ep)
-					errx(EX_USAGE,
-					    "%s: illegal line count", optarg);
-			}
+			if (numlines != 0)
+				usage();
+			numlines = ch - '0';
+			p = optarg ? optarg : "";
+			while (numlines >= 0 && *p >= '0' && *p <= '9')
+				numlines = numlines * 10 + *p++ - '0';
+			if (numlines <= 0 || *p != '\0')
+				errx(EX_USAGE, "%c%s: illegal line count", ch,
+				    optarg ? optarg : "");
 			break;
 		case 'a':		/* Suffix length */
 			if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
diff --git a/usr.bin/split/tests/split_test.sh b/usr.bin/split/tests/split_test.sh
index 6b59b2442017..c9b87c01618c 100755
--- a/usr.bin/split/tests/split_test.sh
+++ b/usr.bin/split/tests/split_test.sh
@@ -220,6 +220,25 @@ continue_body()
 	atf_check -o file:input cat xab
 }
 
+atf_test_case undocumented_kludge
+undocumented_kludge_body()
+{
+	seq 5000 >input
+	atf_check split -1000 input
+	atf_check -o file:xae seq 4001 5000
+	atf_check split -d1000 input
+	atf_check -o file:x04 seq 4001 5000
+}
+
+atf_test_case duplicate_linecount
+duplicate_linecount_body()
+{
+	atf_check -s exit:64 -e ignore split -5 -5 /dev/null
+	atf_check -s exit:64 -e ignore split -l5 -5 /dev/null
+	atf_check -s exit:64 -e ignore split -5 -l5 /dev/null
+	atf_check -s exit:64 -e ignore split -l5 -l5 /dev/null
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case bytes
@@ -231,4 +250,6 @@ atf_init_test_cases()
 	atf_add_test_case pattern
 	atf_add_test_case autoextend
 	atf_add_test_case continue
+	atf_add_test_case undocumented_kludge
+	atf_add_test_case duplicate_linecount
 }