Skip to content

Commit

Permalink
sh: Constify internal representation in expand.c.
Browse files Browse the repository at this point in the history
Forbid (temporary or permanent) modifications of the strings in NARG nodes
during expansion.

Tilde expansion now needs to copy the username for the terminating '\0'.
  • Loading branch information
jillest committed Jan 24, 2016
1 parent 1f19a8f commit ecb3839
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions bin/sh/expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ struct worddest {
static char *expdest; /* output of current string */
static struct nodelist *argbackq; /* list of back quote expressions */

static char *argstr(char *, int, struct worddest *);
static char *exptilde(char *, int);
static char *expari(char *, int, struct worddest *);
static const char *argstr(const char *, int, struct worddest *);
static const char *exptilde(const char *, int);
static const char *expari(const char *, int, struct worddest *);
static void expbackq(union node *, int, int, struct worddest *);
static void subevalvar_trim(char *, int, int, int);
static int subevalvar_misc(char *, const char *, int, int, int);
static char *evalvar(char *, int, struct worddest *);
static void subevalvar_trim(const char *, int, int, int);
static int subevalvar_misc(const char *, const char *, int, int, int);
static const char *evalvar(const char *, int, struct worddest *);
static int varisset(const char *, int);
static void strtodest(const char *, int, int, int, struct worddest *);
static void reprocess(int, int, int, int, struct worddest *);
Expand Down Expand Up @@ -262,8 +262,8 @@ expandarg(union node *arg, struct arglist *arglist, int flag)
*
* If EXP_SPLIT is set, dst receives any complete words produced.
*/
static char *
argstr(char *p, int flag, struct worddest *dst)
static const char *
argstr(const char *p, int flag, struct worddest *dst)
{
char c;
int quotes = flag & (EXP_GLOB | EXP_CASE); /* do CTLESC */
Expand Down Expand Up @@ -352,12 +352,15 @@ argstr(char *p, int flag, struct worddest *dst)
* Perform tilde expansion, placing the result in the stack string and
* returning the next position in the input string to process.
*/
static char *
exptilde(char *p, int flag)
static const char *
exptilde(const char *p, int flag)
{
char c, *startp = p;
char c;
const char *startp = p;
const char *user;
struct passwd *pw;
char *home;
int len;

for (;;) {
c = *p;
Expand All @@ -377,14 +380,17 @@ exptilde(char *p, int flag)
case '\0':
case '/':
case CTLENDVAR:
*p = '\0';
if (*(startp+1) == '\0') {
len = p - startp - 1;
STPUTBIN(startp + 1, len, expdest);
STACKSTRNUL(expdest);
user = expdest - len;
if (*user == '\0') {
home = lookupvar("HOME");
} else {
pw = getpwnam(startp+1);
pw = getpwnam(user);
home = pw != NULL ? pw->pw_dir : NULL;
}
*p = c;
STADJUST(-len, expdest);
if (home == NULL || *home == '\0')
return (startp);
strtodest(home, flag, VSNORMAL, 1, NULL);
Expand All @@ -398,8 +404,8 @@ exptilde(char *p, int flag)
/*
* Expand arithmetic expression.
*/
static char *
expari(char *p, int flag, struct worddest *dst)
static const char *
expari(const char *p, int flag, struct worddest *dst)
{
char *q, *start;
arith_t result;
Expand Down Expand Up @@ -532,7 +538,7 @@ recordleft(const char *str, const char *loc, char *startp)
}

static void
subevalvar_trim(char *p, int strloc, int subtype, int startloc)
subevalvar_trim(const char *p, int strloc, int subtype, int startloc)
{
char *startp;
char *loc = NULL;
Expand Down Expand Up @@ -606,7 +612,7 @@ subevalvar_trim(char *p, int strloc, int subtype, int startloc)


static int
subevalvar_misc(char *p, const char *var, int subtype, int startloc,
subevalvar_misc(const char *p, const char *var, int subtype, int startloc,
int varflags)
{
char *startp;
Expand Down Expand Up @@ -645,12 +651,12 @@ subevalvar_misc(char *p, const char *var, int subtype, int startloc,
* input string.
*/

static char *
evalvar(char *p, int flag, struct worddest *dst)
static const char *
evalvar(const char *p, int flag, struct worddest *dst)
{
int subtype;
int varflags;
char *var;
const char *var;
const char *val;
int patloc;
int c;
Expand Down

0 comments on commit ecb3839

Please sign in to comment.