--- a/libopagent/opagent.c
+++ b/libopagent/opagent.c
@@ -57,6 +57,7 @@
 #include <stdint.h>
 #include <limits.h>
 #include <sys/types.h>
+#include <dirent.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -136,21 +137,46 @@
 	struct timeval tv;
 	FILE * dumpfile = NULL;
 
-	if (0 == stat(TMP_OPROFILE_DIR, &dirstat) && !S_ISDIR(dirstat.st_mode)) {
-		fprintf(stderr, "Error: Creation of directory %s failed.\n", TMP_OPROFILE_DIR);
-		errno = EEXIST;
-		return NULL;
+	/* Coverity complains about 'time-of-check-time-of-use' race if we do stat() on
+	 * a file (or directory) and then open or create it afterwards.  So instead,
+	 * we'll try to open it and see what happens.
+	 */
+	int create_dir = 0;
+	DIR * dir1 = opendir(TMP_OPROFILE_DIR);
+	if (!dir1) {
+		if (errno == ENOENT) {
+			create_dir = 1;
+		} else if (errno == ENOTDIR) {
+			fprintf(stderr, "Error: Creation of directory %s failed. File exists where directory is expected.\n",
+			        TMP_OPROFILE_DIR);
+			return NULL;
+		}
 	} else {
+		closedir(dir1);
+	}
+	if (create_dir) {
+		create_dir = 0;
 		rc = mkdir(TMP_OPROFILE_DIR, S_IRWXU | S_IRWXG | S_IRWXO);
 		if (rc && (errno != EEXIST)) {
 			fprintf(stderr, "Error trying to create %s dir.\n", TMP_OPROFILE_DIR);
 			return NULL;
 		}
 	}
-	if (0 == stat(JITDUMP_DIR, &dirstat) && !S_ISDIR(dirstat.st_mode)) {
-		fprintf(stderr, "Error: Creation of directory %s failed.\n", JITDUMP_DIR);
-		return NULL;
+
+	dir1 = opendir(JITDUMP_DIR);
+	if (!dir1) {
+		if (errno == ENOENT) {
+			create_dir = 1;
+		} else if (errno == ENOTDIR) {
+			fprintf(stderr, "Error: Creation of directory %s failed. File exists where directory is expected.\n",
+			        JITDUMP_DIR);
+			return NULL;
+		}
 	} else {
+		closedir(dir1);
+	}
+
+	if (create_dir) {
 		rc = mkdir(JITDUMP_DIR, S_IRWXU | S_IRWXG | S_IRWXO);
 		if (rc && (errno != EEXIST)) {
 			fprintf(stderr, "Error trying to create %s dir.\n", JITDUMP_DIR);