diff --git a/src/cfgparse.y b/src/cfgparse.y
index f8e84ae9..f1843312 100644
--- a/src/cfgparse.y
+++ b/src/cfgparse.y
@@ -390,6 +390,7 @@ void parse_file(const char *f) {
         int extra = (strlen(current->value) - strlen(current->key));
         char *next;
         for (next = bufcopy;
+             (bufcopy + (next - bufcopy)) < (buf + stbuf.st_size) &&
              (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
              next += strlen(current->key)) {
             *next = '_';