diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c
index 91f919c34cd6f248cbaadb74eabdb8a413941b2c..2c963b67a327adea6de25695674ce11e6a42ea62 100644
--- a/src/bin/pg_basebackup/streamutil.c
+++ b/src/bin/pg_basebackup/streamutil.c
@@ -340,7 +340,9 @@ CreateReplicationSlot(PGconn *conn, const char *slot_name, const char *plugin,
 	{
 		const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
 
-		if (slot_exists_ok && strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
+		if (slot_exists_ok &&
+			sqlstate &&
+			strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
 		{
 			destroyPQExpBuffer(query);
 			PQclear(res);