summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--src/builtin/sprintf.bi96
-rw-r--r--src/prog.c21
2 files changed, 63 insertions, 54 deletions
diff --git a/src/builtin/sprintf.bi b/src/builtin/sprintf.bi
index 11effd5..0a927dd 100644
--- a/src/builtin/sprintf.bi
+++ b/src/builtin/sprintf.bi
@@ -35,12 +35,13 @@ typedef enum {
} printf_format_state;
-char *
-get_num(char *p, unsigned *pn)
+static int
+get_num(const char *p, int i, unsigned *pn)
{
unsigned n = 0;
- for (; *p && mu_isdigit(*p); p++)
- n = n * 10 + *p - '0';
+
+ for (; p[i] && mu_isdigit(p[i]); i++)
+ n = n * 10 + p[i] - '0';
*pn = n;
- return p;
+ return i;
}
@@ -51,6 +52,6 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
{
int i = 0;
- char *p = format;
- char *start;
+ int cur = 0;
+ int start;
char buf[SPRINTF_BUF_SIZE];
printf_format_state state = fmts_copy;
@@ -62,5 +63,5 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
MF_OBSTACK_BEGIN();
MF_VA_START();
- while (*p) {
+ while (format[cur]) {
unsigned n;
char *str;
@@ -72,6 +73,6 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
case fmts_copy:
/* Expect `%', and copy all the rest verbatim */
- if (*p == '%') {
- start = p;
+ if (format[cur] == '%') {
+ start = cur;
state = fmts_pos;
flags = 0;
@@ -79,22 +80,22 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
prec = 0;
} else
- MF_OBSTACK_1GROW(*p);
- p++;
+ MF_OBSTACK_1GROW(format[cur]);
+ cur++;
break;
case fmts_pos:
/* Expect '%' or an argument position -- %_% or %_2$ */
- if (*p == '%') {
+ if (format[cur] == '%') {
MF_OBSTACK_1GROW('%');
- p++;
+ cur++;
state = fmts_copy;
break;
}
- if (mu_isdigit(*p)) {
- char *q = get_num(p, &n);
- if (*q == '$') {
+ if (mu_isdigit(format[cur])) {
+ int pos = get_num(format, cur, &n);
+ if (format[pos] == '$') {
argnum = n - 1;
flags |= FMT_ALTPOS;
- p = q + 1;
+ cur = pos + 1;
}
}
@@ -104,28 +105,28 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
case fmts_flags:
/* Expect flags -- %2$_# */
- switch (*p) {
+ switch (format[cur]) {
case '#':
flags |= FMT_ALTERNATE;
- p++;
+ cur++;
break;
case '0':
flags |= FMT_PADZERO;
- p++;
+ cur++;
break;
case '-':
flags |= FMT_ADJUST_LEFT;
- p++;
+ cur++;
break;
case ' ':
flags |= FMT_SPACEPFX;
- p++;
+ cur++;
break;
case '+':
flags |= FMT_SIGNPFX;
- p++;
+ cur++;
break;
@@ -137,9 +138,9 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
case fmts_width:
/* Expect width -- %2$#_8 or %2$#_* */
- if (mu_isdigit(*p)) {
- p = get_num(p, &width);
+ if (mu_isdigit(format[cur])) {
+ cur = get_num(format, cur, &width);
state = fmts_prec;
- } else if (*p == '*') {
- p++;
+ } else if (format[cur] == '*') {
+ cur++;
state = fmts_width_arg;
} else
@@ -150,9 +151,9 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
/* Expect width argument position -- %2$#*_1$ */
state = fmts_prec;
- if (mu_isdigit(*p)) {
- char *q = get_num(p, &n);
- if (*q == '$') {
+ if (mu_isdigit(format[cur])) {
+ int pos = get_num(format, cur, &n);
+ if (format[pos] == '$') {
MF_VA_ARG(n-1, NUMBER, num);
- p = q + 1;
+ cur = pos + 1;
if (num < 0) {
flags |= FMT_SPACEPFX;
@@ -177,10 +178,10 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
/* Expect precision -- %2$#*1$_. */
state = fmts_conv;
- if (*p == '.') {
- p++;
- if (mu_isdigit(*p)) {
- p = get_num(p, &prec);
- } else if (*p == '*') {
- p++;
+ if (format[cur] == '.') {
+ cur++;
+ if (mu_isdigit(format[cur])) {
+ cur = get_num(format, cur, &prec);
+ } else if (format[cur] == '*') {
+ cur++;
state = fmts_prec_arg;
}
@@ -192,11 +193,11 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
%2$#*1$.*_3$ */
state = fmts_conv;
- if (mu_isdigit(*p)) {
- char *q = get_num(p, &n);
- if (*q == '$') {
+ if (mu_isdigit(format[cur])) {
+ int pos = get_num(format, cur, &n);
+ if (format[pos] == '$') {
MF_VA_ARG(n-1, NUMBER, num);
if (num > 0)
prec = (unsigned) num;
- p = q + 1;
+ cur = pos + 1;
break;
}
@@ -211,5 +212,5 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
if (!(flags & FMT_ALTPOS))
argnum = i++;
- switch (*p) {
+ switch (format[cur]) {
case 's':
MF_VA_ARG(argnum, STRING, str);
@@ -332,5 +333,5 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
if (prec || (flags & FMT_ADJUST_LEFT))
flags &= ~FMT_PADZERO;
- fmtbuf[1] = *p;
+ fmtbuf[1] = format[cur];
snprintf(buf+2, sizeof(buf)-2, fmtbuf, num);
str = buf + 2;
@@ -343,5 +344,5 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
if (flags & FMT_ALTERNATE) {
- *--str = *p;
+ *--str = format[cur];
*--str = '0';
n += 2;
@@ -407,8 +408,9 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
default:
- MF_OBSTACK_GROW(start, p - start + 1);
+ MF_OBSTACK_GROW(&format[start],
+ cur - start + 1);
}
- p++;
+ cur++;
state = fmts_copy;
}
diff --git a/src/prog.c b/src/prog.c
index 83401ef..75b5228 100644
--- a/src/prog.c
+++ b/src/prog.c
@@ -385,5 +385,5 @@ env_get_locus(eval_environ_t env, struct mu_locus_range *locus)
mu_locus_range_init(locus);
locus->beg.mu_file = (char*)(env->dataseg + env->locus.file);
- env_register_auto(env, (void*) locus->beg.mu_file);
+ env_register_auto(env, (void*) &locus->beg.mu_file);
locus->beg.mu_line = env->locus.line;
#if 0
@@ -494,12 +494,18 @@ static void
env_register_auto(eval_environ_t env, void *ptr)
{
+ char *addr = *(char**)ptr;
+
if (env->numautos == MAX_AUTO_PTR)
runtime_error(env, "INTERNAL ERROR at %s:%d, please report",
__FILE__, __LINE__);
+ /* Check if address is within the dataseg */
+ if (!(addr >= (char*) env->dataseg
+ && (addr < (char*) (env->dataseg + datasize + env->stack_size))))
+ ptr = NULL;
env->auto_ptr[env->numautos++] = ptr;
}
/* Pop the last registered auto variable */
-void
+static void
env_pop_auto(eval_environ_t env)
{
@@ -507,5 +513,5 @@ env_pop_auto(eval_environ_t env)
}
-void
+static void
env_unregister_autos(eval_environ_t env)
{
@@ -513,5 +519,5 @@ env_unregister_autos(eval_environ_t env)
}
-void
+static void
env_fixup_autos(eval_environ_t env, ptrdiff_t offset)
{
@@ -519,5 +525,6 @@ env_fixup_autos(eval_environ_t env, ptrdiff_t offset)
for (i = 0; i < env->numautos; i++) {
STKVAL *pptr = env->auto_ptr[i];
- mf_c_val(*pptr,str) += offset; /*FIXME*/
+ if (pptr)
+ mf_c_val(*pptr,str) += offset; /*FIXME*/
}
}
@@ -800,5 +807,5 @@ heap_obstack_grow(eval_environ_t env, void * MFL_DATASEG ptr, size_t size)
char *ret;
- env_register_auto(env, ptr);
+ env_register_auto(env, (void*) &ptr);
if (env->tos - env->toh < words + B2STACK(env->temp_size))
expand_dataseg(env, words,
@@ -830,5 +837,5 @@ pushs(eval_environ_t env, const char * MFL_DATASEG s)
size_t off;
- env_register_auto(env, (void*) s);
+ env_register_auto(env, (void*) &s);
off = heap_reserve(env, strlen(s) + 1);
strcpy((char*) env_data_ref(env, off), s);

Return to:

Send suggestions and report system problems to the System administrator.