diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c
index 91f919c..ccc6108 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 (sqlstate &&
+			slot_exists_ok &&
+			strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
 		{
 			destroyPQExpBuffer(query);
 			PQclear(res);
