Skip to content

Commit

Permalink
strawk: fix build and typos, plug b.c leak
Browse files Browse the repository at this point in the history
not really a leak, but will show up as one.
  • Loading branch information
qwx9 committed Jan 2, 2025
1 parent 2dd91dc commit 8f7d0fa
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 199 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ OBJ:=\
ui/ui.o\
util/print.o\
strpg.o\
strawk/alloc.o\
strawk/awkgram.tab.o\
strawk/b.o\
strawk/parse.o\
Expand Down
34 changes: 17 additions & 17 deletions strawk/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,22 @@ char *defstrdup(const char *s)
size_t n;

n = strlen(s);
return resize(s, n+1, n+1, compile_time == RUNNING);
return resize(s, n+1, n+1, compile_time == RUNNING || evalstr != NULL);
}

void *defrealloc(void *p, size_t old, size_t n)
{
return resize(p, old, n, compile_time == RUNNING);
return resize(p, old, n, compile_time == RUNNING || evalstr != NULL);
}

void *defalloc(size_t n)
{
return alloc(n, compile_time == RUNNING);
}

void egrow(void **buf, int *size, void **pos, int want, int blocksz, char *fn)
void dgrow(void **buf, int *size, void **pos, int want, int blocksz, const char *fn)
{
int sz, n;
size_t sz, n;
intptr_t off;
uschar *p;

Expand All @@ -167,50 +167,50 @@ void egrow(void **buf, int *size, void **pos, int want, int blocksz, char *fn)
want += blocksz - n;
p = *buf;
off = pos != NULL ? *(uschar**)pos - p : 0;
p = erealloc(p, off > 0 ? off : sz, want, fn);
DPRINTF("%p %d -> %d\n", p, sz, n);
p = drealloc(p, off > 0 ? off : sz, want, fn);
DPRINTF("%p %zu -> %zu\n", p, sz, n);
*buf = p;
if (pos)
*pos = p + off;
}

void *erealloc(void *s, size_t old, size_t new, char *fn)
void *drealloc(void *s, size_t old, size_t new, const char *fn)
{
void *p;

DPRINTF(awkstderr, "erealloc %p %d to %d in %s ", s, old, new, fn);
DPRINTF("drealloc %p %zu to %zu in %s ", s, old, new, fn);
if((p = realloc(s, new)) == NULL)
FATAL("realloc: out of memory");
if(new > old)
memset((uschar *)p + old, 0, new - old);
DPRINTF(awkstderr, "-> %p", p);
DPRINTF("-> %p", p);
return p;
}

void *emalloc(size_t n, char *fn)
void *dmalloc(size_t n, const char *fn)
{
void *p;

DPRINTF(awkstderr, "emalloc %d in %s ", n, fn);
DPRINTF("dmalloc %zu in %s ", n, fn);
if((p = calloc(1, n)) == NULL)
FATAL("calloc: out of memory");
DPRINTF(awkstderr, "-> %p", p);
DPRINTF("-> %p", p);
return p;
}

char *estrdup(char *s, char *fn)
char *dstrdup(char *s, const char *fn)
{
void *p;

DPRINTF(awkstderr, "estrdup %s in %s ", s, fn);
DPRINTF("dstrdup %s in %s ", s, fn);
if((p = strdup(s)) == NULL)
FATAL("strdup: out of memory");
DPRINTF(awkstderr, "-> %p", p);
DPRINTF("-> %p", p);
return p;
}

void efree(void *p, char *fn)
void dfree(void *p, const char *fn)
{
DPRINTF(awkstderr, "efree %p in %s\n", p, fn);
DPRINTF("dfree %p in %s\n", p, fn);
free(p);
}
1 change: 1 addition & 0 deletions strawk/awk.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ typedef struct fa {
int initstat;
int curstat;
int accept;
int cst;
struct rrow re[1]; /* variable: actual size set by calling malloc */
} fa;

Expand Down
24 changes: 12 additions & 12 deletions strawk/b.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,17 @@ fa *makedfa(const char *s, bool anchor) /* returns dfa for reg expr s */
resizesetvec(__func__);
}

if (compile_time != RUNNING) /* a constant for sure */
return mkdfa(s, anchor);
//if (compile_time != RUNNING) /* a constant for sure */
// return mkdfa(s, anchor);
for (i = 0; i < nfatab; i++) /* is it there already? */
if (fatab[i]->anchor == anchor
&& strcmp((const char *) fatab[i]->restr, s) == 0) {
fatab[i]->use = now++;
return fatab[i];
}
pfa = mkdfa(s, anchor);
if(compile_time != RUNNING)
pfa->cst = 1;
if (nfatab < NFA) { /* room for another */
fatab[nfatab] = pfa;
fatab[nfatab]->use = now++;
Expand All @@ -196,8 +198,10 @@ fa *makedfa(const char *s, bool anchor) /* returns dfa for reg expr s */
}
use = fatab[0]->use; /* replace least-recently used */
nuse = 0;

/* FIXME: possible overflow? */
for (i = 1; i < nfatab; i++)
if (fatab[i]->use < use) {
if (!fatab[i]->cst && fatab[i]->use < use) {
use = fatab[i]->use;
nuse = i;
}
Expand All @@ -223,7 +227,7 @@ fa *mkdfa(const char *s, bool anchor) /* does the real work of making a dfa */

poscnt = 0;
penter(p1); /* enter parent pointers and leaf indices */
f = (fa *) CALLOC(1, sizeof(fa) + poscnt * sizeof(rrow));
f = (fa *) MALLOC(sizeof(fa) + poscnt * sizeof(rrow));
f->accept = poscnt-1; /* penter has computed number of positions in re */
cfoll(f, p1); /* set up follow sets */
resize_state(f, 1);
Expand All @@ -233,10 +237,8 @@ fa *mkdfa(const char *s, bool anchor) /* does the real work of making a dfa */
f->initstat = makeinit(f, anchor);
f->anchor = anchor;
f->restr = (uschar *) tostring(s);
if (firstbasestr != basestr) {
if (basestr)
xfree(basestr);
}
if (firstbasestr != basestr)
basestr = NULL;
return f;
}

Expand Down Expand Up @@ -1168,10 +1170,8 @@ replace_repeat(const uschar *reptok, int reptoklen, const uschar *atom,
j += suffix_length;
buf[j] = '\0';
/* free old basestr */
if (firstbasestr != basestr) {
if (basestr)
xfree(basestr);
}
if (firstbasestr != basestr)
basestr = NULL;
basestr = buf;
prestr = buf + prefix_length;
if (special_case == REPEAT_ZERO) {
Expand Down
4 changes: 2 additions & 2 deletions strawk/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,14 +587,14 @@ int regexpr(void)
{
int c;
static char *buf = NULL;
static int bufsz = 500;
static int bufsz = 64;
char *bp;

if (buf == NULL)
buf = (char *) MALLOC(bufsz);
bp = buf;
for ( ; (c = input()) != '/' && c != 0; ) {
if (!adjbuf(&buf, &bufsz, bp-buf+3, 500, &bp, "regexpr"))
if (!adjbuf(&buf, &bufsz, bp-buf+3, 64, &bp, "regexpr"))
FATAL("out of space for reg expr %.10s...", buf);
if (c == '\n') {
*bp = '\0';
Expand Down
49 changes: 0 additions & 49 deletions strawk/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,55 +68,6 @@ extern jmp_buf evalenv;
static Cell dollar0 = { OCELL, CFLD, NULL, EMPTY, {0}, REC|STR|DONTFREE, NULL, NULL };
static Cell dollar1 = { OCELL, CFLD, NULL, EMPTY, {0}, FLD|STR|DONTFREE, NULL, NULL };

static size_t nalloc, renalloc;

/* FIXME: use strangepg's emalloc functions when linked? */
void *dmalloc(size_t size, const char *fn)
{
void *p;

nalloc += size;
p = malloc(size);
DPRINTF("ALLOC malloc %s %p %zd / %zd\n", fn, p, size, nalloc);
return p;
}
char *dstrdup(const char *s, const char *fn)
{
char *p;
size_t size;

size = strlen(s);
nalloc += size;
p = strdup(s);
DPRINTF("ALLOC strdup %s %p %zd / %zd [%s]\n", fn, (void*)p, size, nalloc, s);
return p;
}
void *dcalloc(size_t nmemb, size_t size, const char *fn)
{
void *p;

nalloc += nmemb * size;
p = calloc(nmemb, size);
DPRINTF("ALLOC calloc %s %p %zd / %zd\n", fn, p, nmemb*size, nalloc);
return p;
}
void *drealloc(void *ptr, size_t size, const char *fn)
{
void *p;

renalloc += size;
p = realloc(ptr, size);
DPRINTF("ALLOC realloc %s %p→%p %zd / %zd\n", fn, ptr, p, size, renalloc);
return p;
}
void dfree(void *ptr, const char *fn)
{
if(ptr == NULL)
return;
DPRINTF("ALLOC free %s %p\n", fn, ptr);
free(ptr);
}

void recinit(unsigned int n)
{
record = (char *) MALLOC(n);
Expand Down
Loading

0 comments on commit 8f7d0fa

Please sign in to comment.