env: fix address violation with '\v' in -S

Problem reported by Frank Busse (Bug#47412).
* src/env.c (C_ISSPACE_CHARS): New macro.
(shortopts, build_argv, main): Treate all C-locale space
characters like space and tab, for compatibility with FreeBSD.
(validate_split_str, build_argv, parse_split_string):
Use the C locale, not the current locale, to determine whether a
byte is a space character.
This commit is contained in:
Paul Eggert committed 2021-03-26 13:50:16 -07:00
1 parent 4698e284f3
commit ec6904f0ed
1 file changed
+12 -11
+12 -11
View File
@@ -73,7 +73,10 @@ static bool sig_mask_changed;
/* Whether to list non default handling. */ /* Whether to list non default handling. */
static bool report_signal_handling; static bool report_signal_handling;
static char const shortopts[] = "+C:iS:u:v0 \t"; /* isspace characters in the C locale. */
#define C_ISSPACE_CHARS " \t\n\v\f\r"
static char const shortopts[] = "+C:iS:u:v0" C_ISSPACE_CHARS;
/* For long options that have no equivalent short option, use a /* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */ non-character as a pseudo short option, starting with CHAR_MAX + 1. */
@@ -277,7 +280,7 @@ validate_split_str (const char* str, size_t* /*out*/ bufsize,
size_t buflen; size_t buflen;
int cnt = 1; int cnt = 1;
assert (str && str[0] && !isspace (str[0])); /* LCOV_EXCL_LINE */ assert (str && str[0] && !c_isspace (str[0])); /* LCOV_EXCL_LINE */
dq = sq = sp = false; dq = sq = sp = false;
buflen = strlen (str)+1; buflen = strlen (str)+1;
@@ -286,7 +289,7 @@ validate_split_str (const char* str, size_t* /*out*/ bufsize,
{ {
const char next = *(str+1); const char next = *(str+1);
if (isspace (*str) && !dq && !sq) if (c_isspace (*str) && !dq && !sq)
{ {
sp = true; sp = true;
} }
@@ -392,7 +395,7 @@ build_argv (const char* str, int extra_argc)
} \ } \
} while (0) } while (0)
assert (str && str[0] && !isspace (str[0])); /* LCOV_EXCL_LINE */ assert (str && str[0] && !c_isspace (str[0])); /* LCOV_EXCL_LINE */
validate_split_str (str, &buflen, &newargc); validate_split_str (str, &buflen, &newargc);
@@ -433,13 +436,12 @@ build_argv (const char* str, int extra_argc)
++str; ++str;
continue; continue;
case ' ': case ' ': case '\t': case '\n': case '\v': case '\f': case '\r':
case '\t': /* Start a new argument if outside quotes. */
/* space/tab outside quotes starts a new argument. */
if (sq || dq) if (sq || dq)
break; break;
sep = true; sep = true;
str += strspn (str, " \t"); /* skip whitespace. */ str += strspn (str, C_ISSPACE_CHARS);
continue; continue;
case '#': case '#':
@@ -540,7 +542,7 @@ parse_split_string (const char* str, int /*out*/ *orig_optind,
char **newargv, **nextargv; char **newargv, **nextargv;
while (isspace (*str)) while (c_isspace (*str))
str++; str++;
if (*str == '\0') if (*str == '\0')
return; return;
@@ -848,8 +850,7 @@ main (int argc, char **argv)
case 'S': case 'S':
parse_split_string (optarg, &optind, &argc, &argv); parse_split_string (optarg, &optind, &argc, &argv);
break; break;
case ' ': case ' ': case '\t': case '\n': case '\v': case '\f': case '\r':
case '\t':
/* These are undocumented options. Attempt to detect /* These are undocumented options. Attempt to detect
incorrect shebang usage with extraneous space, e.g.: incorrect shebang usage with extraneous space, e.g.:
#!/usr/bin/env -i command #!/usr/bin/env -i command