diff options
| -rw-r--r-- | file_loader.c | 1 | ||||
| -rw-r--r-- | file_loader.h | 2 | ||||
| -rw-r--r-- | vm.c | 8 |
3 files changed, 6 insertions, 5 deletions
diff --git a/file_loader.c b/file_loader.c index 5d231d3..0ce4b4c 100644 --- a/file_loader.c +++ b/file_loader.c @@ -33,6 +33,7 @@ file_t *load_file(const char *filename) f->contents = realloc(f->contents, sizeof(char)*(capacity *= 2) + 1); } f->contents[f->length] = '\0'; + f->end = &f->contents[f->length]; close(fd); // Calculate line numbers: diff --git a/file_loader.h b/file_loader.h index 37399ae..a93c7a3 100644 --- a/file_loader.h +++ b/file_loader.h @@ -8,7 +8,7 @@ typedef struct { const char *filename; - char *contents, **lines; + char *contents, **lines, *end; size_t length, nlines; unsigned int mmapped:1; } file_t; @@ -83,7 +83,7 @@ static match_t *_match(grammar_t *g, file_t *f, const char *str, vm_op_t *op, un { switch (op->op) { case VM_ANYCHAR: { - if (!*str || (!op->multiline && *str == '\n')) + if (str >= f->end - 1 || (!op->multiline && *str == '\n')) return NULL; match_t *m = calloc(sizeof(match_t), 1); m->op = op; @@ -138,14 +138,14 @@ static match_t *_match(grammar_t *g, file_t *f, const char *str, vm_op_t *op, un // This isn't in the for() structure because there needs to // be at least once chance to match the pattern, even if // we're at the end of the string already (e.g. "..$"). - if (*str && (op->multiline || *str != '\n')) ++str; + if (str < f->end && (op->multiline || *str != '\n')) ++str; } destroy_match(&m); return NULL; } else if (op->multiline) { - while (*str) ++str; + str = f->end; } else { - while (*str && *str != '\n') ++str; + while (str < f->end && *str != '\n') ++str; } m->end = str; return m; |
