viz: size the tape buffer from the file, and refuse a too long tape loudly
read_tape filled a fixed INS_SIZE buffer and stopped at the limit without saying anything, so a longer tape ran half a program and produced a plausible wrong answer (a 112797 character calculator tape from a 17 bit build, for instance). read_tape_alloc now allocates from the file size, filters the instruction characters and returns the length, so nothing is ever cut in half. Verified the short tapes are unaffected (the smoke checks still pass: serial R8F, -n 512 1536 512, 7E1 A). It also found the next wall: a 97307 character tape **crashes** the machine with 0xC0000005 (access violation) even though rot8_ins_size is uint32_t -- rot8.c still has a fixed size assumption of its own somewhere. Until that is found and widened, read_tape_alloc refuses anything above INS_SIZE and the CLI says so, which is strictly better than the silent truncation. TODO gets the rest.
This commit is contained in:
1 parent
1aa895ff77
commit
c3ab2f1ac0
3 files changed
+59
-3
No files matched your search
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
+3
-3
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user