gawk

[old] Sed-like interface to the Gopher protocol
Log | Files | Refs | LICENSE

commit ad8003e6580c65dff0411539d4d4a3fb16a653a0
parent bd3e341b2ef355e6f25e97c1478e7227202f52ff
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Tue, 22 Dec 2020 02:15:31 -0800

Fix stack overflow and add depth to prompt

I was allocating far too much memory before which, on my system,
generally caused a segmentation fault at eight recursions. With
these smaller (on my system) but still very comfortable limits I
am able to recurse 4800 times safely.

Diffstat:
Mmain.c | 44++++++++++++++++++++++++--------------------
1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/main.c b/main.c @@ -32,11 +32,15 @@ #define LEN(X) (sizeof(X) / sizeof(*X)) -#define BUFSIZE 4096 -#define ARGV_MAX 16 +#define BUFSIZE 128 #define ARG_SEP ":\t " #define CMD_SEP ";\n" -#define URL_MAX 128 + +#define MY_ARGV_MAX 12 +#define MY_INPUT_MAX 128 /* MAX_INPUT */ +#define MY_LINE_MAX 128 +#define MY_PATH_MAX 128 /* NOTE: PATH_MAX should be used with realpath(1), etc. */ +#define MY_URL_MAX 72 enum gphitem { GI_INFO, GI_PATH, GI_HOST, GI_PORT, GI_NULL }; @@ -132,13 +136,13 @@ strtorange(unsigned int *r, unsigned int min, unsigned int max, const char *s) void newpath(char *path, const char *newpath) { - char tmp[PATH_MAX]; + char tmp[MY_PATH_MAX]; if (*newpath == '/') { - strlcpy(path, newpath, PATH_MAX); + strlcpy(path, newpath, MY_PATH_MAX); } else { - snprintf(tmp, PATH_MAX, "%s/%s", path, newpath); - strlcpy(path, tmp, PATH_MAX); + snprintf(tmp, MY_PATH_MAX, "%s/%s", path, newpath); + strlcpy(path, tmp, MY_PATH_MAX); } } @@ -207,7 +211,7 @@ input(char *buf, int size, const char *delims, const char *prompt, FILE *fp) { int next; static int len; - static char bb[ARG_MAX]; + static char bb[MY_INPUT_MAX]; if (len <= 0) { putprompt(prompt); @@ -322,7 +326,7 @@ int findex(const char *cache, int argc, const char **argv, command *func) { FILE *fp; - char buf[LINE_MAX]; + char buf[MY_LINE_MAX]; int more; unsigned int i, j; unsigned int indexes[argc]; @@ -360,7 +364,7 @@ int fstrmatch(const char *cache, int argc, const char **argv, command *func) { FILE *fp; - char buf[LINE_MAX]; + char buf[MY_LINE_MAX]; int anystr; int i; @@ -386,7 +390,7 @@ int fgroup(const char *cache, int argc, const char **argv, command *func) { FILE *fp; - char buf[LINE_MAX]; + char buf[MY_LINE_MAX]; int anytype; int i; @@ -416,7 +420,7 @@ int sgoto(int argc, const char **argv, int depth, const char *host, const char *port, const char *path) { - char npath[PATH_MAX]; + char npath[MY_PATH_MAX]; const char *nhost, *nport; wunused(3, argc, argv); @@ -505,7 +509,7 @@ cfetch(int argc, const char **argv, int i, const char **request) int cextern(int argc, const char **argv, int index, const char **item) { - char url[URL_MAX]; + char url[MY_URL_MAX]; int i; if (argc == 0) { @@ -620,20 +624,20 @@ invalid_statement: void getcache(char *cache, const char *host, const char *port, const char *path) { - char tmp[PATH_MAX]; + char tmp[MY_PATH_MAX]; - strlcpy(tmp, path, PATH_MAX); + strlcpy(tmp, path, MY_PATH_MAX); tr(tmp, '/', '-'); - snprintf(cache, PATH_MAX, "%s/%s-%s-%s", tmpdir, host, port, tmp); + snprintf(cache, MY_PATH_MAX, "%s/%s-%s-%s", tmpdir, host, port, tmp); } int gawk(const char *host, const char *path, const char *port) { static int depth; - char *argv[ARGV_MAX]; - char cache[PATH_MAX]; - char inbuf[ARG_MAX]; + char *argv[MY_ARGV_MAX]; + char cache[MY_PATH_MAX]; + char inbuf[MY_INPUT_MAX]; char prompt[64]; int argc; int done; @@ -652,7 +656,7 @@ gawk(const char *host, const char *path, const char *port) ++depth; done = 0; - snprintf(prompt, sizeof(prompt), "[%s] %s ", host, path); + snprintf(prompt, sizeof(prompt), "(%d) [%s] %s ", depth, host, path); while (!done && input(inbuf, sizeof(inbuf), CMD_SEP, prompt, stdin) == 0) { argc = argsplit(argv, LEN(argv), inbuf, ARG_SEP); if (argc < 0) {