git: c97460c4d97d - main - sysrc: Fix check flag logic for append and subtract

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 28 Apr 2025 18:21:12 UTC
The branch main has been updated by markj:

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

commit c97460c4d97db1bd46dab9f2bdbc90cd1ed7757f
Author:     Michal Scigocki <michal.os@hotmail.com>
AuthorDate: 2025-04-14 07:01:06 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-04-28 18:18:08 +0000

    sysrc: Fix check flag logic for append and subtract
    
    When using sysrc with the check flag (-c), the append (+=) and subtract
    (-=) operations result in incorrect return values because on the check
    path the necessary union/difference calculation logic is not performed.
    
    However, the correct union/difference calculation is already performed
    when running without the check flag. We fix the issue on the check path
    by using the results from the existing union/difference calculation in
    the check logic to get the correct return values.
    
    PR:             279200
    Reviewed by:    markj
    MFC after:      1 month
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1664
---
 usr.sbin/sysrc/sysrc | 52 +++++++++++++++++++++++++---------------------------
 1 file changed, 25 insertions(+), 27 deletions(-)

diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
index 1766cf7ab835..625ff5ca4efd 100644
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -764,33 +764,6 @@ while [ $# -gt 0 ]; do
 			continue
 		fi
 
-		#
-		# If `-c' is passed, simply compare and move on
-		#
-		if [ "$CHECK_ONLY" ]; then
-			if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
-				status=$FAILURE
-				[ "$VERBOSE" ] &&
-					echo "$NAME: not currently set"
-				shift 1
-				continue
-			fi
-			value=$( f_sysrc_get "$NAME" )
-			if [ "$value" != "${1#*=}" ]; then
-				status=$FAILURE
-				if [ "$VERBOSE" ]; then
-					echo -n "$( f_sysrc_find "$NAME" ): "
-					echo -n "$NAME: would change from "
-					echo "\`$value' to \`${1#*=}'"
-				fi
-			elif [ "$VERBOSE" ]; then
-				echo -n "$( f_sysrc_find "$NAME" ): "
-				echo "$NAME: already set to \`$value'"
-			fi
-			shift 1
-			continue
-		fi
-
 		#
 		# Determine both `before' value and appropriate `new' value
 		#
@@ -848,6 +821,31 @@ while [ $# -gt 0 ]; do
 			new="${1#*=}"
 		esac
 
+		#
+		# If `-c' is passed, simply compare and move on
+		#
+		if [ "$CHECK_ONLY" ]; then
+			if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
+				status=$FAILURE
+				[ "$VERBOSE" ] &&
+					echo "$NAME: not currently set"
+				shift 1
+				continue
+			elif [ "$new" != "$before" ]; then
+				status=$FAILURE
+				if [ "$VERBOSE" ]; then
+					echo -n "$( f_sysrc_find "$NAME" ): "
+					echo -n "$NAME: would change from "
+					echo "\`$before' to \`$new'"
+				fi
+			elif [ "$VERBOSE" ]; then
+				echo -n "$( f_sysrc_find "$NAME" ): "
+				echo "$NAME: already set to \`$before'"
+			fi
+			shift 1
+			continue
+		fi
+
 		#
 		# If `-N' is passed, simplify the output
 		#