Avoid racing condition between stat and fopen (Coverity CID 1295398).
diff --git a/regress/ziptool_regress.c b/regress/ziptool_regress.c
index 849d235..7bb9d87 100644
--- a/regress/ziptool_regress.c
+++ b/regress/ziptool_regress.c
@@ -122,16 +122,16 @@
static zip_t *
read_to_memory(const char *archive, int flags, zip_error_t *error, zip_source_t **srcp)
{
- struct stat st;
zip_source_t *src;
zip_t *zb;
+ FILE *fp;
if (strcmp(archive, "/dev/stdin") == 0) {
zip_error_set(error, ZIP_ER_OPNOTSUPP, 0);
return NULL;
}
- if (stat(archive, &st) < 0) {
+ if ((fp=fopen(archive, "r")) == NULL) {
if (errno == ENOENT) {
src = zip_source_buffer_create(NULL, 0, 0, error);
}
@@ -141,10 +141,10 @@
}
}
else {
- FILE *fp;
+ struct stat st;
- if ((fp=fopen(archive, "r")) == NULL) {
- zip_error_set(error, ZIP_ER_READ, errno);
+ if (stat(archive, &st) < 0) {
+ zip_error_set(error, ZIP_ER_OPEN, errno);
return NULL;
}
if (fragment_size == 0) {