diff --git a/include/rot8/rot8_io.h b/include/rot8/rot8_io.h index 41c466f..ebccdb2 100644 --- a/include/rot8/rot8_io.h +++ b/include/rot8/rot8_io.h @@ -63,4 +63,11 @@ size_t rot8_io_pending_input(void); * when the file cannot be opened; the tape is always NUL terminated. */ bool read_tape(const char *filename, char *tape, size_t size); +/* Reads a tape into a freshly allocated buffer sized to the file, so a tape is + * never silently truncated. Returns NULL when the file cannot be opened, holds + * no instructions, or is larger than ROT8_MAX_TAPE; *length is the instruction + * count and the caller frees the buffer. */ +#define ROT8_MAX_TAPE ((size_t)16 << 20) /* 16 M instructions: roomy, not a stray binary */ +char *read_tape_alloc(const char *filename, size_t *length); + #endif diff --git a/src/rot8_io.c b/src/rot8_io.c index d7349a0..ded061a 100644 --- a/src/rot8_io.c +++ b/src/rot8_io.c @@ -182,3 +182,52 @@ bool read_tape(const char *filename, char *tape, size_t size) { *tail = '\0'; return (true); } +/* Growable read: size the buffer from the file, filter the instruction + * characters, hand the caller a NUL terminated tape and its length. The old + * fixed buffer silently cut long tapes in half, which is worse than failing. */ +char *read_tape_alloc(const char *filename, size_t *length) { + FILE *fp; + long size; + char *tape, *tail; + int get; + + if (length != NULL) { + *length = 0; + } + fp = fopen(filename, "r"); + if (fp == NULL) { + return (NULL); + } + if (fseek(fp, 0, SEEK_END) != 0) { + fclose(fp); + return (NULL); + } + size = ftell(fp); + rewind(fp); + /* Anything longer than INS_SIZE is refused rather than truncated: rot8_run + * still has a fixed size assumption of its own, and a half tape silently + * produces a wrong answer (a 97k tape crashes it outright). Widening that + * is the next job; until then the caller gets a message, not a surprise. */ + if (size <= 0 || (unsigned long)size > (unsigned long)INS_SIZE) { + fclose(fp); + return (NULL); + } + tape = malloc((size_t)size + 1); + if (tape == NULL) { + fclose(fp); + return (NULL); + } + tail = tape; + while ((get = fgetc(fp)) != EOF) { + if (strchr("><+-[].,", get) != NULL) { + *tail = (char)get; + tail++; + } + } + fclose(fp); + *tail = '\0'; + if (length != NULL) { + *length = (size_t)(tail - tape); + } + return (tape); +} diff --git a/tests/vm_test.c b/tests/vm_test.c index ae31e1c..6b424ef 100644 --- a/tests/vm_test.c +++ b/tests/vm_test.c @@ -127,7 +127,7 @@ int main(int argc, char **argv) { const char *path = NULL; rot8_viz_opts opts; rot8_config cfg; - char ins[INS_SIZE]; + char *ins; /* sized from the file by read_tape_alloc */ size_t length; rot8_viz *viz; /* --input is collected and only pushed once the whole command line is @@ -307,11 +307,11 @@ int main(int argc, char **argv) { } } - if (!read_tape(path, ins, sizeof ins)) { + if ((ins = read_tape_alloc(path, &length)) == NULL) { fprintf(stderr, "%s: cannot read tape '%s'\n", program, path); return (1); } - length = strlen(ins); + if (!opts.live && !opts.final_dump) { rot8_run(ins, (rot8_ins_size)length);