Add simple reference-counted string allocator and use it for passing

around references to the sudoers path.  This lets us avoid making
copies of the sudoers path for the errorfile as well as each Defaults
entry.
This commit is contained in:
Todd C. Miller
2016-11-11 16:18:27 -07:00
parent b5e3a7aef2
commit 90995c0acf
10 changed files with 177 additions and 78 deletions

View File

@@ -702,11 +702,8 @@ sudoerserror(const char *s)
/* Save the line the first error occurred on. */
if (errorlineno == -1) {
errorlineno = sudolineno;
free(errorfile);
errorfile = strdup(sudoers);
if (errorfile == NULL)
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
rcstr_delref(errorfile);
errorfile = rcstr_addref(sudoers);
}
if (sudoers_warnings && s != NULL) {
LEXTRACE("<*> ");
@@ -744,13 +741,7 @@ new_default(char *var, char *val, short op)
d->op = op;
/* d->binding = NULL */
d->lineno = last_token == COMMENT ? sudolineno - 1 : sudolineno;
d->file = strdup(sudoers);
if (d->file == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to allocate memory");
free(d);
debug_return_ptr(NULL);
}
d->file = rcstr_addref(sudoers);
HLTQ_INIT(d, entries);
debug_return_ptr(d);
@@ -981,10 +972,9 @@ init_parser(const char *path, bool quiet)
free_members(d->binding);
free(d->binding);
}
/* no need to free sd_un */
rcstr_delref(d->file);
free(d->var);
free(d->val);
free(d->file);
free(d);
}
TAILQ_INIT(&defaults);
@@ -996,9 +986,9 @@ init_parser(const char *path, bool quiet)
ret = false;
}
free(sudoers);
rcstr_delref(sudoers);
if (path != NULL) {
if ((sudoers = strdup(path)) == NULL) {
if ((sudoers = rcstr_dup(path)) == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
ret = false;
}
@@ -1008,13 +998,13 @@ init_parser(const char *path, bool quiet)
parse_error = false;
errorlineno = -1;
free(errorfile);
rcstr_delref(errorfile);
errorfile = NULL;
sudoers_warnings = !quiet;
debug_return_bool(ret);
}
#line 965 "gram.c"
#line 955 "gram.c"
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
#if defined(__cplusplus) || defined(__STDC__)
static int yygrowstack(void)
@@ -2097,7 +2087,7 @@ case 115:
}
}
break;
#line 2048 "gram.c"
#line 2038 "gram.c"
}
yyssp -= yym;
yystate = *yyssp;