git: 05e79d2c0f76 - main - soelim: fix path and whitespace handling

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 07:40:28 UTC
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=05e79d2c0f765f1004517acb3e1f7e890b56d8c7

commit 05e79d2c0f765f1004517acb3e1f7e890b56d8c7
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2026-11-18 06:17:11 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2026-11-18 07:40:21 +0000

    soelim: fix path and whitespace handling
    
    Reject truncated path
    Skip all white space following the file name
---
 usr.bin/soelim/soelim.c             | 32 ++++++++++++++++++--------------
 usr.bin/soelim/tests/soelim_test.sh | 18 ++++++++++++++++++
 2 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/usr.bin/soelim/soelim.c b/usr.bin/soelim/soelim.c
index fe363b9e8827..ad22901b1f7a 100644
--- a/usr.bin/soelim/soelim.c
+++ b/usr.bin/soelim/soelim.c
@@ -58,6 +58,7 @@ soelim_fopen(const char *name)
 	FILE *f;
 	char path[PATH_MAX];
 	size_t i;
+	int len;
 
 	if (strcmp(name, "-") == 0)
 		return (stdin);
@@ -71,8 +72,10 @@ soelim_fopen(const char *name)
 	}
 
 	for (i = 0; i < includes->sl_cur; i++) {
-		snprintf(path, sizeof(path), "%s/%s", includes->sl_str[i],
-		    name);
+		len = snprintf(path, sizeof(path), "%s/%s",
+		    includes->sl_str[i], name);
+		if (len < 0 || (size_t)len >= sizeof(path))
+			continue;
 		if ((f = fopen(path, "r")) != NULL)
 			return (f);
 	}
@@ -86,9 +89,8 @@ static int
 soelim_file(FILE *f, const char *name, int flag)
 {
 	char *line = NULL;
-	char *walk, *cp;
+	char *walk, *cp, *end;
 	size_t linecap = 0;
-	ssize_t linelen;
 	int ret;
 
 	if (f == NULL)
@@ -96,30 +98,32 @@ soelim_file(FILE *f, const char *name, int flag)
 
 	include_depth++;
 	ret = 0;
-	while ((linelen = getline(&line, &linecap, f)) > 0) {
+	while (getline(&line, &linecap, f) > 0) {
 		if (strncmp(line, ".so", 3) != 0) {
-			printf("%s", line);
+			fputs(line, stdout);
 			continue;
 		}
 
 		walk = line + 3;
-		if (!isspace(*walk) && ((flag & C_OPTION) == 0)) {
-			printf("%s", line);
+		if (!isspace((unsigned char)*walk) &&
+		    ((flag & C_OPTION) == 0)) {
+			fputs(line, stdout);
 			continue;
 		}
 
-		while (isspace(*walk))
+		while (isspace((unsigned char)*walk))
 			walk++;
 
 		cp = walk;
-		while (*cp != '\0' && !isspace(*cp))
+		while (*cp != '\0' && !isspace((unsigned char)*cp))
 			cp++;
-		*cp = 0;
-		if (cp < line + linelen)
+		end = cp;
+		while (isspace((unsigned char)*cp))
 			cp++;
+		*end = '\0';
 
 		if (*walk == '\0') {
-			printf("%s", line);
+			fputs(line, stdout);
 			continue;
 		}
 		if (include_depth >= MAX_INCLUDE_DEPTH) {
@@ -132,7 +136,7 @@ soelim_file(FILE *f, const char *name, int flag)
 			break;
 		}
 		if (*cp != '\0')
-			printf("%s", cp);
+			fputs(cp, stdout);
 	}
 	include_depth--;
 
diff --git a/usr.bin/soelim/tests/soelim_test.sh b/usr.bin/soelim/tests/soelim_test.sh
index bf126d35260b..b768717aaa28 100755
--- a/usr.bin/soelim/tests/soelim_test.sh
+++ b/usr.bin/soelim/tests/soelim_test.sh
@@ -136,9 +136,27 @@ errors_body()
 		soelim self
 }
 
+atf_test_case whitespace
+whitespace_head()
+{
+	atf_set "descr" "white space after the file name"
+}
+
+whitespace_body()
+{
+	printf 'included\n' > inc
+	printf '.so inc  \nnext\n' > trailing
+	atf_check \
+		-o inline:"included\nnext\n" \
+		-e empty \
+		-s exit:0 \
+		soelim trailing
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case stdin
 	atf_add_test_case files
 	atf_add_test_case errors
+	atf_add_test_case whitespace
 }