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
This commit is contained in:
Ed Maste 2022-02-14 22:44:01 -05:00
parent 0c2832ee4f
commit 82b611ed18

View File

@ -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);
}