From: Todd C. Miller <Todd.Miller@courtesan.com> Date: Mon, 13 Jan 2014 16:52:41 +0000 (-0700) Subject: Do not leak old istack if realloc fails; found by cppcheck. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b75bb6991fb3dae273a2b9f068e2a7ca875c88c1;p=sudo Do not leak old istack if realloc fails; found by cppcheck. Also modify yyless() to avoid a harmless cppcheck warning every time it is used. --- diff --git a/plugins/sudoers/toke.c b/plugins/sudoers/toke.c index e31badf10..074e54a99 100644 --- a/plugins/sudoers/toke.c +++ b/plugins/sudoers/toke.c @@ -148,7 +148,7 @@ extern FILE *yyin, *yyout; /* Undo effects of setting up yytext. */ \ *yy_cp = yy_hold_char; \ YY_RESTORE_YY_MORE_OFFSET \ - yy_c_buf_p = yy_cp = yy_bp + n - YY_MORE_ADJ; \ + yy_cp = yy_bp + n - YY_MORE_ADJ; \ YY_DO_BEFORE_ACTION; /* set up yytext again */ \ } \ while ( 0 ) @@ -4147,18 +4147,21 @@ _push_include(char *path, bool isdir) /* push current state onto stack */ if (idepth >= istacksize) { + struct include_stack *new_istack; + if (idepth > MAX_SUDOERS_DEPTH) { sudoerserror(N_("too many levels of includes")); debug_return_bool(false); } istacksize += SUDOERS_STACK_INCREMENT; - istack = (struct include_stack *) realloc(istack, + new_istack = (struct include_stack *) realloc(istack, sizeof(*istack) * istacksize); - if (istack == NULL) { + if (new_istack == NULL) { warning(NULL); sudoerserror(NULL); debug_return_bool(false); } + istack = new_istack; } SLIST_INIT(&istack[idepth].more); if (isdir) { diff --git a/plugins/sudoers/toke.l b/plugins/sudoers/toke.l index aa459d905..438bb12a7 100644 --- a/plugins/sudoers/toke.l +++ b/plugins/sudoers/toke.l @@ -888,18 +888,21 @@ _push_include(char *path, bool isdir) /* push current state onto stack */ if (idepth >= istacksize) { + struct include_stack *new_istack; + if (idepth > MAX_SUDOERS_DEPTH) { sudoerserror(N_("too many levels of includes")); debug_return_bool(false); } istacksize += SUDOERS_STACK_INCREMENT; - istack = (struct include_stack *) realloc(istack, + new_istack = (struct include_stack *) realloc(istack, sizeof(*istack) * istacksize); - if (istack == NULL) { + if (new_istack == NULL) { warning(NULL); sudoerserror(NULL); debug_return_bool(false); } + istack = new_istack; } SLIST_INIT(&istack[idepth].more); if (isdir) {