git: d3cbb4745a13 - stable/12 - elfctl: fix operations with multiple features on multiple files

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

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

commit d3cbb4745a13d5155597e66281efa05cb6a7c72a
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:33:47 +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 b4482f82e7ef..eeade6ac371c 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);
@@ -226,9 +226,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;
@@ -240,8 +240,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;
@@ -271,13 +273,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);
+				}
 			}
 		}
 	}
@@ -289,6 +294,7 @@ convert_to_feature_val(char *feature_str, uint32_t *feature_val)
 	} else if (operation == '-') {
 		*feature_val &= ~input;
 	}
+	free(feature_tmp);
 	return (true);
 }