Fix some valgrind errors.

A null pointer dereference and a memory leak with the line/col mapping.
This commit is contained in:
Calvin Rose 2020-11-27 12:21:23 -06:00
parent fb0859dfe6
commit a0964d44d5
3 changed files with 5 additions and 9 deletions

View File

@ -1684,9 +1684,9 @@ static Janet cfun_ev_call(int32_t argc, Janet *argv) {
janet_arity(argc, 1, -1); janet_arity(argc, 1, -1);
JanetFunction *fn = janet_getfunction(argv, 0); JanetFunction *fn = janet_getfunction(argv, 0);
JanetFiber *fiber = janet_fiber(fn, 64, argc - 1, argv + 1); JanetFiber *fiber = janet_fiber(fn, 64, argc - 1, argv + 1);
if (NULL == fiber) janet_panicf("invalid arity to function %v", argv[0]);
fiber->env = janet_table(0); fiber->env = janet_table(0);
fiber->env->proto = janet_current_fiber()->env; fiber->env->proto = janet_current_fiber()->env;
if (NULL == fiber) janet_panicf("invalid arity to function %v", argv[0]);
janet_schedule(fiber, janet_wrap_nil()); janet_schedule(fiber, janet_wrap_nil());
return janet_wrap_fiber(fiber); return janet_wrap_fiber(fiber);
} }

View File

@ -352,7 +352,7 @@ static int stringend(JanetParser *p, JanetParseState *state) {
*w++ = *r++; *w++ = *r++;
} }
} }
buflen = (int32_t) (w - bufstart); buflen = (int32_t)(w - bufstart);
} }
/* Check for trailing newline character so we can remove it */ /* Check for trailing newline character so we can remove it */
if (buflen > 0 && bufstart[buflen - 1] == '\n') { if (buflen > 0 && bufstart[buflen - 1] == '\n') {

View File

@ -102,10 +102,7 @@ static LineCol get_linecol_from_position(PegState *s, int32_t position) {
for (const uint8_t *c = s->text_start; c < s->text_end; c++) { for (const uint8_t *c = s->text_start; c < s->text_end; c++) {
if (*c == '\n') newline_count++; if (*c == '\n') newline_count++;
} }
int32_t *mem = malloc(sizeof(int32_t) * newline_count); int32_t *mem = janet_smalloc(sizeof(int32_t) * newline_count);
if (NULL == mem) {
JANET_OUT_OF_MEMORY;
}
size_t index = 0; size_t index = 0;
for (const uint8_t *c = s->text_start; c < s->text_end; c++) { for (const uint8_t *c = s->text_start; c < s->text_end; c++) {
if (*c == '\n') mem[index++] = (int32_t)(c - s->text_start); if (*c == '\n') mem[index++] = (int32_t)(c - s->text_start);
@ -337,13 +334,13 @@ tail:
} }
case RULE_LINE: { case RULE_LINE: {
LineCol lc = get_linecol_from_position(s, (int32_t) (text - s->text_start)); LineCol lc = get_linecol_from_position(s, (int32_t)(text - s->text_start));
pushcap(s, janet_wrap_number((double)(lc.line)), rule[1]); pushcap(s, janet_wrap_number((double)(lc.line)), rule[1]);
return text; return text;
} }
case RULE_COLUMN: { case RULE_COLUMN: {
LineCol lc = get_linecol_from_position(s, (int32_t) (text - s->text_start)); LineCol lc = get_linecol_from_position(s, (int32_t)(text - s->text_start));
pushcap(s, janet_wrap_number((double)(lc.col)), rule[1]); pushcap(s, janet_wrap_number((double)(lc.col)), rule[1]);
return text; return text;
} }
@ -489,7 +486,6 @@ tail:
/* Throw generic error */ /* Throw generic error */
int32_t start = (int32_t)(text - s->text_start); int32_t start = (int32_t)(text - s->text_start);
LineCol lc = get_linecol_from_position(s, start); LineCol lc = get_linecol_from_position(s, start);
int32_t end = (int32_t)(result - s->text_start);
janet_panicf("match error at line %d, column %d", lc.line, lc.col); janet_panicf("match error at line %d, column %d", lc.line, lc.col);
} }
return NULL; return NULL;