git: f0692bcb9aa0 - main - sysrc: Fix unlikely and rare edge-case

From: Devin Teske <dteske_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 19:45:07 UTC
The branch main has been updated by dteske:

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

commit f0692bcb9aa041aaa3ecf78207a0063c44279ed7
Author:     Devin Teske <dteske@FreeBSD.org>
AuthorDate: 2026-11-26 19:40:26 +0000
Commit:     Devin Teske <dteske@FreeBSD.org>
CommitDate: 2026-11-26 19:41:24 +0000

    sysrc: Fix unlikely and rare edge-case
    
    Fix 1 issue identified in post-commit review of eaf7113:
    
    POSTCR-1.1: with -F, the "unchanged" check compares a file path
    against the new value, so a value equal to the path is silently
    not written
    
    Add an ATF test to ensure we catch the edge-case possibility.
    
    Special note: To replicate this misbehavior you'd have to do
    something like: sysrc -f /etc/rc.conf foo=1 and then execute
    sysrc -F -f /etc/rc.conf foo=/etc/rc.conf
    
    Reported by:    llm-postcr
    Fixes:          eaf7113bef8f ("sysrc: Add "unchanged" messages")
---
 usr.sbin/sysrc/sysrc               | 30 +++++++++++-------------------
 usr.sbin/sysrc/tests/sysrc_test.sh | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 19 deletions(-)

diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
index daa78845ace8..22f5b7f40dbb 100644
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -16,7 +16,7 @@ pgm="${0##*/}" # Program basename
 #
 # Version information
 #
-SYSRC_VERSION="9.3.1 Nov-25,2026"
+SYSRC_VERSION="9.3.2 Nov-26,2026"
 
 #
 # Options
@@ -810,11 +810,12 @@ while [ $# -gt 0 ]; do
 		fi
 
 		#
-		# Determine both `before' value and appropriate `new' value
+		# Determine both `before' values and appropriate `new' value
 		#
+		before=$( f_sysrc_get_raw "$NAME" )
+		before_file=$( f_sysrc_find "$NAME" )
 		case "$mode" in
 		LISTAPPEND)
-			before=$( f_sysrc_get_raw "$NAME" )
 			add="${1#*=}"
 			delim="${add%"${add#?}"}" # first character
 			oldIFS="$IFS"
@@ -835,10 +836,8 @@ while [ $# -gt 0 ]; do
 			set +f # glob
 			new="${new#"$delim"}" IFS="$oldIFS"
 			unset add delim oldIFS a skip b
-			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
 			;;
 		LISTREMOVE)
-			before=$( f_sysrc_get_raw "$NAME" )
 			remove="${1#*=}"
 			delim="${remove%"${remove#?}"}" # first character
 			oldIFS="$IFS"
@@ -859,22 +858,14 @@ while [ $# -gt 0 ]; do
 			set +f # glob
 			new="${new#"$delim"}" IFS="$oldIFS"
 			unset remove delim oldIFS b add r
-			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
 			;;
 		STRAPPEND)
-			before=$( f_sysrc_get_raw "$NAME" )
 			add="${1#*=}"
 			new="$add"
 			[ "$before" ] && new="$before $add"
 			unset add
-			[ "$SHOW_FILE" ] && before=$( f_sysrc_find "$NAME" )
 			;;
 		*) # ASSIGN
-			if [ "$SHOW_FILE" ]; then
-				before=$( f_sysrc_find "$NAME" )
-			else
-				before=$( f_sysrc_get_raw "$NAME" )
-			fi
 			new="${1#*=}"
 		esac
 
@@ -891,16 +882,14 @@ while [ $# -gt 0 ]; do
 				status=$FAILURE
 				if [ "$VERBOSE" ]; then
 					warn "%s: %s: would change %s" \
-						"$( f_sysrc_find "$NAME" )" \
-						"$NAME" \
+						"$before_file" "$NAME" \
 						"from \`$before' to \`$new'"
 				elif [ ! "$QUIET" ]; then
 					warn "%s: value differs" "$NAME"
 				fi
 			elif [ "$VERBOSE" ]; then
 				warn "%s: %s: already set to \`%s'" \
-					"$( f_sysrc_find "$NAME" )" \
-					"$NAME" "$before"
+					"$before_file" "$NAME" "$before"
 			fi
 			shift 1
 			continue
@@ -909,6 +898,7 @@ while [ $# -gt 0 ]; do
 		# Empty append or remove changes nothing
 		case "$mode:${1#*=}" in
 		LISTAPPEND:|LISTREMOVE:|STRAPPEND:)
+			[ "$SHOW_FILE" ] && before="$before_file"
 			if [ "$QUIET" ]; then
 				: nothing
 			elif [ "$SHOW_EQUALS" ]; then
@@ -925,9 +915,10 @@ while [ $# -gt 0 ]; do
 
 		# An unset name is not already NULL
 		if [ "$before" = "$new" ] &&
-		   file=$( f_sysrc_find "$NAME" ) &&
-		   [ "$file" != "$RC_DEFAULTS" ]
+		   [ "$before_file" ] &&
+		   [ "$before_file" != "$RC_DEFAULTS" ]
 		then
+			[ "$SHOW_FILE" ] && before="$before_file"
 			if [ "$QUIET" ]; then
 				: nothing
 			elif [ "$SHOW_EQUALS" ]; then
@@ -954,6 +945,7 @@ while [ $# -gt 0 ]; do
 			if f_sysrc_set "$NAME" "$new"; then
 				if [ ! "$QUIET" ]; then
 					if [ "$SHOW_FILE" ]; then
+						before="$before_file"
 						after=$( f_sysrc_find "$NAME" )
 					else
 						after=$(
diff --git a/usr.sbin/sysrc/tests/sysrc_test.sh b/usr.sbin/sysrc/tests/sysrc_test.sh
index 60df7d5f34f3..b21fae3e057e 100644
--- a/usr.sbin/sysrc/tests/sysrc_test.sh
+++ b/usr.sbin/sysrc/tests/sysrc_test.sh
@@ -427,6 +427,43 @@ F_flag_body()
 	    -f ${sysrc2_conf} -F test_enable test_list
 }
 
+atf_test_case F_flag_assign
+F_flag_assign_head()
+{
+	atf_set "descr" "Under -F, compare values and display the file"
+}
+F_flag_assign_body()
+{
+	local msg
+
+	echo 'p="x"' > "${SYSRC_CONF}"
+
+	# Value matches. Show the file and do not rewrite it
+	atf_check -o inline:"p: ${SYSRC_CONF} (unchanged)\n" \
+	    sysrc -F -f "${SYSRC_CONF}" p=x
+	atf_check -o inline:'p="x"\n' cat "${SYSRC_CONF}"
+
+	# -c compares the value, not the path
+	atf_check sysrc -F -c -f "${SYSRC_CONF}" p=x
+	msg="sysrc: ${SYSRC_CONF}: p: already set to \`x'"
+	atf_check -e inline:"$msg\n" \
+	    sysrc -F -v -c -f "${SYSRC_CONF}" p=x
+	msg="sysrc: ${SYSRC_CONF}: p: would change from \`x' to \`y'"
+	atf_check -s exit:1 -e inline:"$msg\n" \
+	    sysrc -F -v -c -f "${SYSRC_CONF}" p=y
+
+	# A value equal to the path is a real assignment
+	msg="p: ${SYSRC_CONF} -> ${SYSRC_CONF}"
+	atf_check -o inline:"$msg\n" \
+	    sysrc -F -f "${SYSRC_CONF}" "p=${SYSRC_CONF}"
+	atf_check -o inline:"p=\"${SYSRC_CONF}\"\n" cat "${SYSRC_CONF}"
+
+	# Now the stored value is the path, so this is unchanged
+	atf_check -o inline:"p: ${SYSRC_CONF} (unchanged)\n" \
+	    sysrc -F -f "${SYSRC_CONF}" "p=${SYSRC_CONF}"
+	atf_check -o inline:"p=\"${SYSRC_CONF}\"\n" cat "${SYSRC_CONF}"
+}
+
 atf_test_case r_flag
 r_flag_head()
 {
@@ -615,5 +652,6 @@ atf_init_test_cases()
 	atf_add_test_case d_flag
 	atf_add_test_case f_flag
 	atf_add_test_case F_flag
+	atf_add_test_case F_flag_assign
 	atf_add_test_case r_flag
 }