diff --git a/contrib/pg_standby/pg_standby.c b/contrib/pg_standby/pg_standby.c
index d6b1692..cd69f54 100644
--- a/contrib/pg_standby/pg_standby.c
+++ b/contrib/pg_standby/pg_standby.c
@@ -426,9 +426,11 @@ CheckForExternalTrigger(void)
 		close(fd);
 		return;
 	}
-	buf[len] = '\0';
 
-	if (strncmp(buf, "smart", 5) == 0)
+	if (len < sizeof(buf))
+		buf[len] = '\0';
+
+	if (strncmp(buf, "smart", 5) == 0 && len == 5)
 	{
 		Failover = SmartFailover;
 		fprintf(stderr, "trigger file found: smart failover\n");
@@ -437,7 +439,7 @@ CheckForExternalTrigger(void)
 		return;
 	}
 
-	if (strncmp(buf, "fast", 4) == 0)
+	if (strncmp(buf, "fast", 4) == 0 && len == 4)
 	{
 		Failover = FastFailover;
 
