fixed a critical bug in handling a multi-dimensional index with delete (e.g. delete[20,"data"])

This commit is contained in:
hyung-hwan 2020-03-02 07:23:34 +00:00
parent 140c12f726
commit 48809c9e69

View File

@ -2568,66 +2568,26 @@ static int run_nextoutfile (hawk_rtx_t* rtx, hawk_nde_nextfile_t* nde)
static int run_nextfile (hawk_rtx_t* rtx, hawk_nde_nextfile_t* nde) static int run_nextfile (hawk_rtx_t* rtx, hawk_nde_nextfile_t* nde)
{ {
return (nde->out)? return (nde->out)? run_nextoutfile(rtx, nde): run_nextinfile(rtx, nde);
run_nextoutfile (rtx, nde):
run_nextinfile (rtx, nde);
} }
static int delete_indexed ( static int delete_indexed (hawk_rtx_t* rtx, hawk_htb_t* map, hawk_nde_var_t* var)
hawk_rtx_t* rtx, hawk_htb_t* map, hawk_nde_var_t* var)
{ {
hawk_val_t* idx; hawk_ooch_t* iptr;
hawk_oow_t ilen;
hawk_ooch_t idxbuf[IDXBUFSIZE];
HAWK_ASSERT (var->idx != HAWK_NULL); HAWK_ASSERT (var->idx != HAWK_NULL);
idx = eval_expression (rtx, var->idx); /* delete x["abc"];
if (idx == HAWK_NULL) return -1; * delete x[20,"abc"]; */
ilen = HAWK_COUNTOF(idxbuf);
iptr = idxnde_to_str(rtx, var->idx, idxbuf, &ilen);
if (!iptr) return -1;
hawk_rtx_refupval (rtx, idx); hawk_htb_delete (map, iptr, ilen);
if (HAWK_RTX_GETVALTYPE(rtx, idx) == HAWK_VAL_STR)
{
/* delete x["abc"] */
hawk_htb_delete (map, ((hawk_val_str_t*)idx)->val.ptr, ((hawk_val_str_t*)idx)->val.len);
hawk_rtx_refdownval (rtx, idx);
}
else
{
/* delete x[20] */
hawk_ooch_t buf[IDXBUFSIZE];
hawk_rtx_valtostr_out_t out;
/* try with a fixed-size buffer */
out.type = HAWK_RTX_VALTOSTR_CPLCPY;
out.u.cplcpy.ptr = buf;
out.u.cplcpy.len = HAWK_COUNTOF(buf);
if (hawk_rtx_valtostr(rtx, idx, &out) <= -1)
{
int n;
/* retry it in dynamic mode */
out.type = HAWK_RTX_VALTOSTR_CPLDUP;
n = hawk_rtx_valtostr(rtx, idx, &out);
hawk_rtx_refdownval (rtx, idx);
if (n <= -1)
{
/* change the error line */
ADJERR_LOC (rtx, &var->loc);
return -1;
}
else
{
hawk_htb_delete (map, out.u.cpldup.ptr, out.u.cpldup.len);
hawk_rtx_freemem (rtx, out.u.cpldup.ptr);
}
}
else
{
hawk_rtx_refdownval (rtx, idx);
hawk_htb_delete (map, out.u.cplcpy.ptr, out.u.cplcpy.len);
}
}
if (iptr != idxbuf) hawk_rtx_freemem (rtx, iptr);
return 0; return 0;
} }