git: 7c067d5b823d - stable/13 - renice: Avoid segfault if -n flag is given without argument.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Sat, 06 May 2023 14:42:44 UTC
The branch stable/13 has been updated by des:

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

commit 7c067d5b823d34d22bbaa9e1f1dd16fdb99fbc2d
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2023-04-17 19:49:13 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2023-05-06 13:14:01 +0000

    renice: Avoid segfault if -n flag is given without argument.
    
    Fixes:          65ee0a8495538
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans
    Differential Revision:  https://reviews.freebsd.org/D39623
    
    (cherry picked from commit b03311cadd3a6f931e176f97b97bd72be3ef96d4)
    
    renice: Clean up the tests.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans
    Differential Revision:  https://reviews.freebsd.org/D39629
    
    (cherry picked from commit d59dd30cf19bb6df5635b3c896c7c4262917a9b6)
    
    renice: Factor out common code in tests.
    
    Furthermore, prefer test -eq (which ignores whitespace) over a strict string equality test when comparing numerical values.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    thj
    Differential Revision:  https://reviews.freebsd.org/D39650
    
    (cherry picked from commit 1bc6dcc287fa80af74768c4e2b2a1d7511ba1729)
---
 usr.bin/renice/renice.c             |  2 +-
 usr.bin/renice/tests/renice_test.sh | 92 +++++++++++++++++++------------------
 2 files changed, 49 insertions(+), 45 deletions(-)

diff --git a/usr.bin/renice/renice.c b/usr.bin/renice/renice.c
index 890d7043ebbd..42e93cce240e 100644
--- a/usr.bin/renice/renice.c
+++ b/usr.bin/renice/renice.c
@@ -94,7 +94,7 @@ main(int argc, char *argv[])
 			}
 			if (strcmp(*argv, "-n") == 0) {
 				/* may occur only once, prior to priority */
-				if (haveprio || incr)
+				if (haveprio || incr || argc < 2)
 					usage();
 				incr = true;
 				(void)argc--, argv++;
diff --git a/usr.bin/renice/tests/renice_test.sh b/usr.bin/renice/tests/renice_test.sh
old mode 100644
new mode 100755
index 323fe44591c8..feb07a6af825
--- a/usr.bin/renice/tests/renice_test.sh
+++ b/usr.bin/renice/tests/renice_test.sh
@@ -1,28 +1,7 @@
-#!/bin/sh
-#-
-# Copyright (c) 2022 Klara, Inc.
-# All rights reserved.
 #
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-# 1. Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the above copyright
-#    notice, this list of conditions and the following disclaimer in the
-#    documentation and/or other materials provided with the distribution.
+# Copyright (c) 2022-2023 Klara, Inc.
 #
-# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-# SUCH DAMAGE.
+# SPDX-License-Identifier: BSD-2-Clause
 #
 
 # Name of user to use for -u tests when running as root.  Beware that
@@ -33,8 +12,16 @@ _renice() {
 	atf_check -o empty -e ignore -s exit:0 renice "$@"
 }
 
-# Set a process's nice number to an absolute value
+atf_check_nice_value() {
+	local pid=$1
+	local nice=$2
+	atf_check test "$(ps -o nice= -p $pid)" -eq "$nice"
+}
+
 atf_test_case renice_abs_pid
+renice_abs_pid_head() {
+	atf_set "descr" "Set a process's nice number to an absolute value"
+}
 renice_abs_pid_body() {
 	local pid nice incr
 	sleep 60 &
@@ -42,12 +29,14 @@ renice_abs_pid_body() {
 	nice="$(ps -o nice= -p $pid)"
 	incr=3
 	_renice $((nice+incr)) $pid
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	kill $pid
 }
 
-# Change a process's nice number by a relative value
 atf_test_case renice_rel_pid
+renice_rel_pid_head() {
+	atf_set "descr" "Change a process's nice number by a relative value"
+}
 renice_rel_pid_body() {
 	local pid nice incr
 	sleep 60 &
@@ -57,12 +46,14 @@ renice_rel_pid_body() {
 	_renice -n $incr $pid
 	_renice -p -n $incr $pid
 	_renice -n $incr -p $pid
-	atf_check_equal $((nice+incr+incr+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr+incr+incr))
 	kill $pid
 }
 
-# Set a process group's nice number to an absolute value
 atf_test_case renice_abs_pgid
+renice_abs_pgid_head() {
+	atf_set "descr" "Set a process group's nice number to an absolute value"
+}
 renice_abs_pgid_body() {
 	local pid pgid nice incr
 	# make sure target runs in a different pgrp than ours
@@ -71,12 +62,14 @@ renice_abs_pgid_body() {
 	nice="$(ps -o nice= -p $pid)"
 	incr=3
 	_renice $((nice+incr)) -g $pgid
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	kill $pid
 }
 
-# Change a process group's nice number by a relative value
 atf_test_case renice_rel_pgid
+renice_rel_pgid_head() {
+	atf_set "descr" "Change a process group's nice number by a relative value"
+}
 renice_rel_pgid_body() {
 	local pid pgid nice incr
 	# make sure target runs in a different pgrp than ours
@@ -86,13 +79,13 @@ renice_rel_pgid_body() {
 	incr=3
 	_renice -g -n $incr $pgid
 	_renice -n $incr -g $pgid
-	atf_check_equal $((nice+incr+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr+incr))
 	kill $pid
 }
 
-# Set a user's processes' nice numbers to an absolute value
 atf_test_case renice_abs_user
 renice_abs_user_head() {
+	atf_set "descr" "Set a user's processes' nice numbers to an absolute value"
 	atf_set "require.user" "root"
 }
 renice_abs_user_body() {
@@ -101,13 +94,13 @@ renice_abs_user_body() {
 	nice="$(ps -o nice= -p $pid)"
 	incr=3
 	_renice $((nice+incr)) -u $TEST_USER
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	kill $pid
 }
 
-# Change a user's processes' nice numbers by a relative value
 atf_test_case renice_rel_user
 renice_rel_user_head() {
+	atf_set "descr" "Change a user's processes' nice numbers by a relative value"
 	atf_set "require.user" "root"
 }
 renice_rel_user_body() {
@@ -117,12 +110,14 @@ renice_rel_user_body() {
 	incr=3
 	_renice -u -n $incr $TEST_USER
 	_renice -n $incr -u $TEST_USER
-	atf_check_equal $((nice+incr+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr+incr))
 	kill $pid
 }
 
-# Test various delimiter positions
 atf_test_case renice_delim
+renice_delim_head() {
+	atf_set "descr" "Test various delimiter positions"
+}
 renice_delim_body() {
 	local pid nice incr
 	sleep 60 &
@@ -132,32 +127,40 @@ renice_delim_body() {
 	# without -p
 	: $((incr=incr+1))
 	_renice -- $((nice+incr)) $pid
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	: $((incr=incr+1))
 	_renice $((nice+incr)) -- $pid
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	: $((incr=incr+1))
 	_renice $((nice+incr)) $pid --
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	# with -p
 	: $((incr=incr+1))
 	_renice -p -- $((nice+incr)) $pid
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	: $((incr=incr+1))
 	_renice -p $((nice+incr)) -- $pid
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	: $((incr=incr+1))
 	_renice -p $((nice+incr)) $pid --
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	: $((incr=incr+1))
 	_renice $((nice+incr)) -p -- $pid
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	: $((incr=incr+1))
 	_renice $((nice+incr)) -p $pid --
-	atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)"
+	atf_check_nice_value $pid $((nice+incr))
 	kill $pid
 }
 
+atf_test_case renice_incr_noarg
+renice_incr_noarg_head() {
+	atf_set "descr" "Do not segfault if -n is given without an argument"
+}
+renice_incr_noarg_body() {
+	atf_check -o empty -e ignore -s exit:1 renice -n
+}
+
 atf_init_test_cases() {
 	atf_add_test_case renice_abs_pid
 	atf_add_test_case renice_rel_pid
@@ -166,4 +169,5 @@ atf_init_test_cases() {
 	atf_add_test_case renice_abs_user
 	atf_add_test_case renice_rel_user
 	atf_add_test_case renice_delim
+	atf_add_test_case renice_incr_noarg
 }