fixed a memory leak bug in parse_expr_basic() in parse.c

touched up qse_sed_comp() a little.
ehnanced the regression test script
This commit is contained in:
hyung-hwan 2009-09-10 21:05:13 +00:00
parent 417d901dad
commit 07d4a531f8
13 changed files with 125 additions and 84 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: parse.c 267 2009-08-25 09:50:07Z hyunghwan.chung $ * $Id: parse.c 285 2009-09-10 03:05:13Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -3193,14 +3193,17 @@ static qse_awk_nde_t* parse_expr_basic (
if (MATCH(awk,TOK_QUEST)) if (MATCH(awk,TOK_QUEST))
{ {
qse_awk_nde_cnd_t* tmp; qse_awk_loc_t eloc;
qse_awk_nde_cnd_t* cnd;
if (get_token(awk) <= -1) return QSE_NULL;
if (get_token(awk) <= -1)
{ {
qse_awk_loc_t eloc = awk->tok.loc; qse_awk_clrpt (awk, nde);
n1 = parse_expr_dc (awk, &eloc); return QSE_NULL;
} }
eloc = awk->tok.loc;
n1 = parse_expr_dc (awk, &eloc);
if (n1 == QSE_NULL) if (n1 == QSE_NULL)
{ {
qse_awk_clrpt (awk, nde); qse_awk_clrpt (awk, nde);
@ -3209,15 +3212,20 @@ static qse_awk_nde_t* parse_expr_basic (
if (!MATCH(awk,TOK_COLON)) if (!MATCH(awk,TOK_COLON))
{ {
qse_awk_clrpt (awk, nde);
qse_awk_clrpt (awk, n1);
SETERR_TOK (awk, QSE_AWK_ECOLON); SETERR_TOK (awk, QSE_AWK_ECOLON);
return QSE_NULL; return QSE_NULL;
} }
if (get_token(awk) <= -1) return QSE_NULL; if (get_token(awk) <= -1)
{ {
qse_awk_loc_t eloc = awk->tok.loc; qse_awk_clrpt (awk, nde);
n2 = parse_expr_dc (awk, &eloc); qse_awk_clrpt (awk, n1);
return QSE_NULL;
} }
eloc = awk->tok.loc;
n2 = parse_expr_dc (awk, &eloc);
if (n2 == QSE_NULL) if (n2 == QSE_NULL)
{ {
qse_awk_clrpt (awk, nde); qse_awk_clrpt (awk, nde);
@ -3225,9 +3233,9 @@ static qse_awk_nde_t* parse_expr_basic (
return QSE_NULL; return QSE_NULL;
} }
tmp = (qse_awk_nde_cnd_t*) QSE_AWK_ALLOC ( cnd = (qse_awk_nde_cnd_t*) QSE_AWK_ALLOC (
awk, QSE_SIZEOF(qse_awk_nde_cnd_t)); awk, QSE_SIZEOF(qse_awk_nde_cnd_t));
if (tmp == QSE_NULL) if (cnd == QSE_NULL)
{ {
qse_awk_clrpt (awk, nde); qse_awk_clrpt (awk, nde);
qse_awk_clrpt (awk, n1); qse_awk_clrpt (awk, n1);
@ -3236,14 +3244,14 @@ static qse_awk_nde_t* parse_expr_basic (
return QSE_NULL; return QSE_NULL;
} }
tmp->type = QSE_AWK_NDE_CND; cnd->type = QSE_AWK_NDE_CND;
tmp->loc = *xloc; cnd->loc = *xloc;
tmp->next = QSE_NULL; cnd->next = QSE_NULL;
tmp->test = nde; cnd->test = nde;
tmp->left = n1; cnd->left = n1;
tmp->right = n2; cnd->right = n2;
nde = (qse_awk_nde_t*)tmp; nde = (qse_awk_nde_t*)cnd;
} }
return nde; return nde;
@ -3344,6 +3352,7 @@ static qse_awk_nde_t* parse_binary (
while (1) while (1)
{ {
qse_awk_loc_t rloc;
const binmap_t* p = binmap; const binmap_t* p = binmap;
qse_bool_t matched = QSE_FALSE; qse_bool_t matched = QSE_FALSE;
@ -3369,10 +3378,8 @@ static qse_awk_nde_t* parse_binary (
} }
while (skipnl && MATCH(awk,TOK_NEWLINE)); while (skipnl && MATCH(awk,TOK_NEWLINE));
{ rloc = awk->tok.loc;
qse_awk_loc_t nloc = awk->tok.loc; right = next_level_func (awk, &rloc);
right = next_level_func (awk, &nloc);
}
if (right == QSE_NULL) if (right == QSE_NULL)
{ {
qse_awk_clrpt (awk, left); qse_awk_clrpt (awk, left);
@ -3597,10 +3604,16 @@ static qse_awk_nde_t* parse_concat (
while (1) while (1)
{ {
qse_awk_loc_t rloc;
if (MATCH(awk,TOK_PERIOD)) if (MATCH(awk,TOK_PERIOD))
{ {
if (!(awk->option & QSE_AWK_EXPLICIT)) break; if (!(awk->option & QSE_AWK_EXPLICIT)) break;
if (get_token(awk) <= -1) return QSE_NULL; if (get_token(awk) <= -1)
{
qse_awk_clrpt (awk, left);
return QSE_NULL;
}
} }
else if (MATCH(awk,TOK_LPAREN) || else if (MATCH(awk,TOK_LPAREN) ||
MATCH(awk,TOK_DOLLAR) || MATCH(awk,TOK_DOLLAR) ||
@ -3616,10 +3629,8 @@ static qse_awk_nde_t* parse_concat (
} }
else break; else break;
{ rloc = awk->tok.loc;
qse_awk_loc_t rloc = awk->tok.loc;
right = parse_additive (awk, &rloc); right = parse_additive (awk, &rloc);
}
if (right == QSE_NULL) if (right == QSE_NULL)
{ {
qse_awk_clrpt (awk, left); qse_awk_clrpt (awk, left);
@ -3630,8 +3641,8 @@ static qse_awk_nde_t* parse_concat (
awk, QSE_SIZEOF(qse_awk_nde_exp_t)); awk, QSE_SIZEOF(qse_awk_nde_exp_t));
if (nde == QSE_NULL) if (nde == QSE_NULL)
{ {
qse_awk_clrpt (awk, left);
qse_awk_clrpt (awk, right); qse_awk_clrpt (awk, right);
qse_awk_clrpt (awk, left);
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc); SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc);
return QSE_NULL; return QSE_NULL;
} }
@ -3683,6 +3694,7 @@ static qse_awk_nde_t* parse_unary (
{ {
qse_awk_nde_exp_t* nde; qse_awk_nde_exp_t* nde;
qse_awk_nde_t* left; qse_awk_nde_t* left;
qse_awk_loc_t uloc;
int opcode; int opcode;
opcode = (MATCH(awk,TOK_PLUS))? QSE_AWK_UNROP_PLUS: opcode = (MATCH(awk,TOK_PLUS))? QSE_AWK_UNROP_PLUS:
@ -3704,10 +3716,8 @@ static qse_awk_nde_t* parse_unary (
if (get_token(awk) <= -1) return QSE_NULL; if (get_token(awk) <= -1) return QSE_NULL;
awk->parse.depth.cur.expr++; awk->parse.depth.cur.expr++;
{ uloc = awk->tok.loc;
qse_awk_loc_t uloc = awk->tok.loc;
left = parse_unary (awk, &uloc); left = parse_unary (awk, &uloc);
}
awk->parse.depth.cur.expr--; awk->parse.depth.cur.expr--;
if (left == QSE_NULL) return QSE_NULL; if (left == QSE_NULL) return QSE_NULL;
@ -3716,7 +3726,6 @@ static qse_awk_nde_t* parse_unary (
if (nde == QSE_NULL) if (nde == QSE_NULL)
{ {
qse_awk_clrpt (awk, left); qse_awk_clrpt (awk, left);
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc); SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc);
return QSE_NULL; return QSE_NULL;
} }
@ -3748,6 +3757,7 @@ static qse_awk_nde_t* parse_unary_exp (
{ {
qse_awk_nde_exp_t* nde; qse_awk_nde_exp_t* nde;
qse_awk_nde_t* left; qse_awk_nde_t* left;
qse_awk_loc_t uloc;
int opcode; int opcode;
opcode = (MATCH(awk,TOK_PLUS))? QSE_AWK_UNROP_PLUS: opcode = (MATCH(awk,TOK_PLUS))? QSE_AWK_UNROP_PLUS:
@ -3768,10 +3778,8 @@ static qse_awk_nde_t* parse_unary_exp (
if (get_token(awk) <= -1) return QSE_NULL; if (get_token(awk) <= -1) return QSE_NULL;
awk->parse.depth.cur.expr++; awk->parse.depth.cur.expr++;
{ uloc = awk->tok.loc;
qse_awk_loc_t uloc = awk->tok.loc;
left = parse_unary (awk, &uloc); left = parse_unary (awk, &uloc);
}
awk->parse.depth.cur.expr--; awk->parse.depth.cur.expr--;
if (left == QSE_NULL) return QSE_NULL; if (left == QSE_NULL) return QSE_NULL;
@ -3800,6 +3808,7 @@ static qse_awk_nde_t* parse_increment (
qse_awk_nde_exp_t* nde; qse_awk_nde_exp_t* nde;
qse_awk_nde_t* left; qse_awk_nde_t* left;
int type, opcode, opcode1, opcode2; int type, opcode, opcode1, opcode2;
qse_awk_loc_t ploc;
/* check for prefix increment operator */ /* check for prefix increment operator */
opcode1 = MATCH(awk,TOK_PLUSPLUS)? QSE_AWK_INCOP_PLUS: opcode1 = MATCH(awk,TOK_PLUSPLUS)? QSE_AWK_INCOP_PLUS:
@ -3811,10 +3820,8 @@ static qse_awk_nde_t* parse_increment (
if (get_token(awk) <= -1) return QSE_NULL; if (get_token(awk) <= -1) return QSE_NULL;
} }
{ ploc = awk->tok.loc;
qse_awk_loc_t ploc = awk->tok.loc;
left = parse_primary (awk, &ploc); left = parse_primary (awk, &ploc);
}
if (left == QSE_NULL) return QSE_NULL; if (left == QSE_NULL) return QSE_NULL;
/* check for postfix increment operator */ /* check for postfix increment operator */
@ -4438,8 +4445,8 @@ static qse_awk_nde_t* parse_primary_ident (
awk, QSE_SIZEOF(qse_awk_nde_var_t)); awk, QSE_SIZEOF(qse_awk_nde_var_t));
if (nde == QSE_NULL) if (nde == QSE_NULL)
{ {
QSE_AWK_FREE (awk, namedup);
SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc); SETERR_LOC (awk, QSE_AWK_ENOMEM, xloc);
QSE_AWK_FREE (awk, namedup);
return QSE_NULL; return QSE_NULL;
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sed.c 280 2009-09-07 13:34:49Z hyunghwan.chung $ * $Id: sed.c 285 2009-09-10 03:05:13Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -203,7 +203,7 @@ void qse_sed_setmaxdepth (qse_sed_t* sed, int ids, qse_size_t depth)
static qse_cint_t getnextsc (qse_sed_t* sed) static qse_cint_t getnextsc (qse_sed_t* sed)
{ {
if (++sed->src.cur < sed->src.end) if (sed->src.cur < sed->src.end)
{ {
if (sed->src.cc == QSE_T('\n')) if (sed->src.cc == QSE_T('\n'))
{ {
@ -211,7 +211,8 @@ static qse_cint_t getnextsc (qse_sed_t* sed)
sed->src.loc.col = 1; sed->src.loc.col = 1;
} }
else sed->src.loc.col++; else sed->src.loc.col++;
sed->src.cc = *(sed)->src.cur;
sed->src.cc = *sed->src.cur++;
/* TODO: support different line end convension */ /* TODO: support different line end convension */
/*if (sed->src.cc == QSE_T('\n')) sed->src.loc.lin++;*/ /*if (sed->src.cc == QSE_T('\n')) sed->src.loc.lin++;*/
} }
@ -1225,20 +1226,21 @@ static int get_command (qse_sed_t* sed, qse_sed_cmd_t* cmd)
int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen) int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
{ {
qse_cint_t c; qse_cint_t c;
qse_sed_cmd_t* cmd; qse_sed_cmd_t* cmd = QSE_NULL;
/* store the source code pointers */ /* store the source code pointers */
sed->src.ptr = sptr; sed->src.ptr = sptr;
sed->src.end = sptr + slen; sed->src.end = sptr + slen;
sed->src.cur = sptr; sed->src.cur = sptr;
sed->src.loc.lin = 1; sed->src.loc.lin = 1;
sed->src.loc.col = 1; sed->src.loc.col = 0;
sed->src.cc = (slen > 0)? (*sptr): QSE_CHAR_EOF; sed->src.cc = QSE_CHAR_EOF;
c = NXTSC (sed);
/* free all the commands previously compiled */ /* free all the commands previously compiled */
free_all_command_blocks (sed); free_all_command_blocks (sed);
QSE_ASSERT (sed->cmd.lb == &sed->cmd.fb && sed->cmd.lb->len == 0); QSE_ASSERT (sed->cmd.lb == &sed->cmd.fb && sed->cmd.lb->len == 0);
cmd = &sed->cmd.lb->buf[sed->cmd.lb->len];
/* clear the label table */ /* clear the label table */
qse_map_clear (&sed->tmp.labs); qse_map_clear (&sed->tmp.labs);
@ -1251,15 +1253,13 @@ int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
{ {
int n; int n;
c = CURSC (sed);
/* skip white spaces and comments*/ /* skip white spaces and comments*/
while (IS_WSPACE(c)) c = NXTSC (sed); while (IS_WSPACE(c)) c = NXTSC (sed);
if (c == QSE_T('#')) if (c == QSE_T('#'))
{ {
do c = NXTSC (sed); do c = NXTSC (sed);
while (!IS_LINTERM(c) && c != QSE_CHAR_EOF) ; while (!IS_LINTERM(c) && c != QSE_CHAR_EOF) ;
NXTSC (sed); c = NXTSC (sed);
continue; continue;
} }
@ -1269,15 +1269,20 @@ int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
if (c == QSE_T(';')) if (c == QSE_T(';'))
{ {
/* semicolon without a address-command pair */ /* semicolon without a address-command pair */
NXTSC (sed); c = NXTSC (sed);
continue; continue;
} }
/* initialize the current command */ /* initialize the current command */
cmd = &sed->cmd.lb->buf[sed->cmd.lb->len];
QSE_MEMSET (cmd, 0, QSE_SIZEOF(*cmd)); QSE_MEMSET (cmd, 0, QSE_SIZEOF(*cmd));
/* process the first address */ /* process the first address */
if (get_address (sed, &cmd->a1) == QSE_NULL) return -1; if (get_address (sed, &cmd->a1) == QSE_NULL)
{
cmd = QSE_NULL;
goto oops;
}
c = CURSC (sed); c = CURSC (sed);
if (cmd->a1.type != QSE_SED_ADR_NONE) if (cmd->a1.type != QSE_SED_ADR_NONE)
@ -1295,8 +1300,7 @@ int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
if (get_address (sed, &cmd->a2) == QSE_NULL) if (get_address (sed, &cmd->a2) == QSE_NULL)
{ {
QSE_ASSERT (cmd->a2.type == QSE_SED_ADR_NONE); QSE_ASSERT (cmd->a2.type == QSE_SED_ADR_NONE);
free_address (sed, cmd); goto oops;
return -1;
} }
if (delim == QSE_T(',')) if (delim == QSE_T(','))
@ -1304,8 +1308,7 @@ int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
if (cmd->a2.type == QSE_SED_ADR_NONE) if (cmd->a2.type == QSE_SED_ADR_NONE)
{ {
SETERR0 (sed, QSE_SED_EA2MOI, &sed->src.loc); SETERR0 (sed, QSE_SED_EA2MOI, &sed->src.loc);
free_address(sed, cmd); goto oops;
return -1;
} }
} }
else if ((sed->option&QSE_SED_STARTSTEP) && else if ((sed->option&QSE_SED_STARTSTEP) &&
@ -1315,8 +1318,7 @@ int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
cmd->a2.type != QSE_SED_ADR_LINE) cmd->a2.type != QSE_SED_ADR_LINE)
{ {
SETERR0 (sed, QSE_SED_EA2MOI, &sed->src.loc); SETERR0 (sed, QSE_SED_EA2MOI, &sed->src.loc);
free_address(sed, cmd); goto oops;
return -1;
} }
cmd->a2.type = QSE_SED_ADR_STEP; cmd->a2.type = QSE_SED_ADR_STEP;
@ -1343,18 +1345,20 @@ int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
} }
n = get_command (sed, cmd); n = get_command (sed, cmd);
if (n <= -1) if (n <= -1) goto oops;
{
free_address (sed, cmd);
return -1;
}
c = CURSC (sed);
/* cmd's end of life */
cmd = QSE_NULL;
/* increment the total numbers of complete commands */
sed->cmd.lb->len++; sed->cmd.lb->len++;
if (sed->cmd.lb->len >= QSE_COUNTOF(sed->cmd.lb->buf)) if (sed->cmd.lb->len >= QSE_COUNTOF(sed->cmd.lb->buf))
{ {
if (add_command_block (sed) <= -1) return -1; /* increase a command buffer block as necessary */
if (add_command_block (sed) <= -1) goto oops;
} }
cmd = &sed->cmd.lb->buf[sed->cmd.lb->len];
} }
if (sed->tmp.grp.level != 0) if (sed->tmp.grp.level != 0)
@ -1364,6 +1368,10 @@ int qse_sed_comp (qse_sed_t* sed, const qse_char_t* sptr, qse_size_t slen)
} }
return 0; return 0;
oops:
if (cmd != QSE_NULL) free_address (sed, cmd);
return -1;
} }
static int read_char (qse_sed_t* sed, qse_char_t* c) static int read_char (qse_sed_t* sed, qse_char_t* c)

View File

@ -242,7 +242,25 @@ leakcheck)
exit 1 exit 1
} }
run_scripts "${bin_valgrind} --leak-check=full --show-reachable=yes --track-fds=yes" 2>&1 > "${OUTFILE}.test" run_scripts "${bin_valgrind} --leak-check=full --show-reachable=yes --track-fds=yes" 2>&1 > "${OUTFILE}.test"
echo_so "Inspect the '${OUTFILE}.test' file for any memory and file descriptor leaks." x=`grep -Fic "no leaks are possible" "${OUTFILE}.test"`
y=`grep -Fic "${bin_valgrind}" "${OUTFILE}.test"`
if [ ${x} -eq ${y} ]
then
echo_so "(POSSIBLY) no memory leaks detected".
else
echo_so "(POSSIBLY) some memory leaks detected".
fi
echo_so "Inspect the '${OUTFILE}.test' file for details"
;;
*)
echo_so "USAGE: $0 init"
echo_so " $0 test"
echo_so " $0 leakcheck"
exit 1
;;
esac
exit 0
;; ;;
*) *)
echo_so "USAGE: $0 init" echo_so "USAGE: $0 init"

View File

@ -1,5 +1,5 @@
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
../../cmd/sed/.libs/qsesed -n -f 001.sed 001.dat </dev/stdin 2>&1 ../../cmd/sed/.libs/qsesed -n -f s001.sed s001.dat </dev/stdin 2>&1
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
ab...c AAA ab...c AAA
@ -15,7 +15,7 @@ pq...r AAA2
kbs ddd kbs ddd
dif cccc dif cccc
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
../../cmd/sed/.libs/qsesed -f 002.sed 002.dat </dev/stdin 2>&1 ../../cmd/sed/.libs/qsesed -f s002.sed s002.dat </dev/stdin 2>&1
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
ab...c AAA ab...c AAA
@ -31,7 +31,7 @@ pq...r AAA2
kbs ddd kbs ddd
dif cccc dif cccc
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
../../cmd/sed/.libs/qsesed -f 003.sed 003.dat </dev/stdin 2>&1 ../../cmd/sed/.libs/qsesed -f s003.sed s003.dat </dev/stdin 2>&1
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
ab...c AAA ab...c AAA
@ -47,7 +47,7 @@ pq...r AAA2
kbs ddd kbs ddd
dif cccc dif cccc
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
../../cmd/sed/.libs/qsesed -f 004.sed 004.dat </dev/stdin 2>&1 ../../cmd/sed/.libs/qsesed -f s004.sed s004.dat </dev/stdin 2>&1
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
linux { linux {
HOST: com.com HOST: com.com

View File

@ -49,10 +49,10 @@ TMPFILE="${TMPFILE:=./regress.temp}"
OUTFILE="${OUTFILE:=./regress.out}" OUTFILE="${OUTFILE:=./regress.out}"
PROGS=" PROGS="
001.sed/001.dat//-n s001.sed/s001.dat//-n
002.sed/002.dat// s002.sed/s002.dat//
003.sed/003.dat// s003.sed/s003.dat//
004.sed/004.dat// s004.sed/s004.dat//
" "
[ -x "${QSESED}" ] || [ -x "${QSESED}" ] ||
@ -122,7 +122,15 @@ leakcheck)
exit 1 exit 1
} }
run_scripts "${bin_valgrind} --leak-check=full --show-reachable=yes --track-fds=yes" 2>&1 > "${OUTFILE}.test" run_scripts "${bin_valgrind} --leak-check=full --show-reachable=yes --track-fds=yes" 2>&1 > "${OUTFILE}.test"
echo_so "Inspect the '${OUTFILE}.test' file for any memory and file descriptor leaks." x=`grep -Fic "no leaks are possible" "${OUTFILE}.test"`
y=`grep -Fic "${bin_valgrind}" "${OUTFILE}.test"`
if [ ${x} -eq ${y} ]
then
echo_so "(POSSIBLY) no memory leaks detected".
else
echo_so "(POSSIBLY) some memory leaks detected".
fi
echo_so "Inspect the '${OUTFILE}.test' file for details"
;; ;;
*) *)
echo_so "USAGE: $0 init" echo_so "USAGE: $0 init"