git: cac751fb5f09 - stable/13 - elfctl: fix operations with multiple features on multiple files

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Fri, 18 Feb 2022 01:51:00 UTC
The branch stable/13 has been updated by emaste:

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

commit cac751fb5f099568d2ed13779ca7b7c22221a605
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2022-02-15 03:44:01 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2022-02-18 00:31:57 +0000

    elfctl: fix operations with multiple features on multiple files
    
    Previously an invocation like
    
      elfctl -e +feature1,feature2 file1 file2
    
    would set both feature flags in file 1 but only feature1 in file2 (due
    to the string being modified by strsep()).
    
    Reported by:    jrm
    Tested by:      jrm
    MFC after:      3 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D34283
    
    (cherry picked from commit 82b611ed18e6bfaeadf152ef9216230548c25a8a)
---
 usr.bin/elfctl/elfctl.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/usr.bin/elfctl/elfctl.c b/usr.bin/elfctl/elfctl.c
index 993349d4ec4c..7cd851f20170 100644
--- a/usr.bin/elfctl/elfctl.c
+++ b/usr.bin/elfctl/elfctl.c
@@ -51,7 +51,7 @@
 
 __FBSDID("$FreeBSD$");
 
-static bool convert_to_feature_val(char *, uint32_t *);
+static bool convert_to_feature_val(const char *, uint32_t *);
 static bool edit_file_features(Elf *, int, int, char *, bool);
 static bool get_file_features(Elf *, int, int, uint32_t *, uint64_t *, bool);
 static void print_features(void);
@@ -222,9 +222,9 @@ usage(void)
 }
 
 static bool
-convert_to_feature_val(char *feature_str, uint32_t *feature_val)
+convert_to_feature_val(const char *feature_str, uint32_t *feature_val)
 {
-	char *feature;
+	char *feature, *feature_tmp;
 	int i, len;
 	uint32_t input;
 	char operation;
@@ -236,8 +236,10 @@ convert_to_feature_val(char *feature_str, uint32_t *feature_val)
 	if (operation != '+' && operation != '-' && operation != '=')
 		errx(1, "'%c' not an operator - use '+', '-', '='", operation);
 
+	if ((feature_tmp = strdup(feature_str)) == NULL)
+		err(1, "strdup");
 	len = nitems(featurelist);
-	while ((feature = strsep(&feature_str, ",")) != NULL) {
+	while ((feature = strsep(&feature_tmp, ",")) != NULL) {
 		for (i = 0; i < len; ++i) {
 			if (strcmp(featurelist[i].alias, feature) == 0) {
 				input |= featurelist[i].value;
@@ -267,13 +269,16 @@ convert_to_feature_val(char *feature_str, uint32_t *feature_val)
 					errno = ERANGE;
 				if (errno != 0) {
 					warn("%s invalid", feature);
+					free(feature_tmp);
 					return (false);
 				}
 				input |= val;
 			} else {
 				warnx("%s is not a valid feature", feature);
-				if (!iflag)
+				if (!iflag) {
+					free(feature_tmp);
 					return (false);
+				}
 			}
 		}
 	}
@@ -285,6 +290,7 @@ convert_to_feature_val(char *feature_str, uint32_t *feature_val)
 	} else if (operation == '-') {
 		*feature_val &= ~input;
 	}
+	free(feature_tmp);
 	return (true);
 }