diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 753316e..4f4d019 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -789,6 +789,24 @@ ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime)
 	walrcv->lastMsgReceiptTime = lastMsgReceiptTime;
 	SpinLockRelease(&walrcv->mutex);
 
+	/*
+	 * Check that the WAL position received from sender is valid and does
+	 * refer to a segment file whose name finishes by FF.
+	 */
+	if ((walEnd.xlogid & 0xff) == 0xff)
+	{
+		char		xlogfilename[MAXFNAMELEN];
+		uint32		xlogseg;
+		uint32		xlogid;
+
+		XLByteToPrevSeg(walEnd, xlogid, xlogseg);
+		XLogFileName(xlogfilename, ThisTimeLineID, xlogid, xlogseg);
+		ereport(ERROR,
+				(errcode(ERRCODE_PROTOCOL_VIOLATION),
+				 errmsg_internal("invalid WAL position %X/%X referring to segment %s",
+								 walEnd.xlogid, walEnd.xrecoff, xlogfilename)));
+	}
+
 	if (log_min_messages <= DEBUG2)
 	{
 		char	   *sendtime;
