]> granicus.if.org Git - curl/commitdiff
Eliminated an unlikely race condition in some tests.
authorDan Fandrich <dan@coneharvesters.com>
Wed, 7 Apr 2010 05:51:05 +0000 (22:51 -0700)
committerDan Fandrich <dan@coneharvesters.com>
Thu, 15 Apr 2010 17:51:03 +0000 (10:51 -0700)
Based on a patch from the FreeBSD ports by Peter Pentchev.

tests/libtest/lib505.c
tests/libtest/lib525.c
tests/libtest/lib541.c

index 9dfae8405328b03071b28f0066c27f10376396d6..5af7ac0bb16cd3a201400fdea8ff4d17ac665b3a 100644 (file)
@@ -54,34 +54,33 @@ int test(char *URL)
     return -1;
   }
 
+  hd_src = fopen(libtest_arg2, "rb");
+  if(NULL == hd_src) {
+    error = ERRNO;
+    fprintf(stderr, "fopen() failed with error: %d %s\n",
+            error, strerror(error));
+    fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
+    return -2; /* if this happens things are major weird */
+  }
+
   /* get the file size of the local file */
-  hd = stat(libtest_arg2, &file_info);
+  hd = fstat(fileno(hd_src), &file_info);
   if(hd == -1) {
     /* can't open file, bail out */
     error = ERRNO;
-    fprintf(stderr, "stat() failed with error: %d %s\n",
+    fprintf(stderr, "fstat() failed with error: %d %s\n",
             error, strerror(error));
-    fprintf(stderr, "WARNING: cannot open file %s\n", libtest_arg2);
+    fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
+    fclose(hd_src);
     return -1;
   }
 
   if(! file_info.st_size) {
-    fprintf(stderr, "WARNING: file %s has no size!\n", libtest_arg2);
+    fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
+    fclose(hd_src);
     return -4;
   }
 
-  /* get a FILE * of the same file, could also be made with
-     fdopen() from the previous descriptor, but hey this is just
-     an example! */
-  hd_src = fopen(libtest_arg2, "rb");
-  if(NULL == hd_src) {
-    error = ERRNO;
-    fprintf(stderr, "fopen() failed with error: %d %s\n",
-            error, strerror(error));
-    fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
-    return -2; /* if this happens things are major weird */
-  }
-
   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     fprintf(stderr, "curl_global_init() failed\n");
     fclose(hd_src);
index d2542577b91a3e68b7a05bfa5e624ba9a09d523a..ade6370121bf70541b9755e1025c038604ab24d9 100644 (file)
@@ -40,14 +40,6 @@ int test(char *URL)
     return -1;
   }
 
-  /* get the file size of the local file */
-  hd = open(libtest_arg2, O_RDONLY) ;
-  fstat(hd, &file_info);
-  close(hd) ;
-
-  /* get a FILE * of the same file, could also be made with
-     fdopen() from the previous descriptor, but hey this is just
-     an example! */
   hd_src = fopen(libtest_arg2, "rb");
   if(NULL == hd_src) {
     error = ERRNO;
@@ -57,6 +49,18 @@ int test(char *URL)
     return TEST_ERR_MAJOR_BAD;
   }
 
+  /* get the file size of the local file */
+  hd = fstat(fileno(hd_src), &file_info);
+  if(hd == -1) {
+    /* can't open file, bail out */
+    error = ERRNO;
+    fprintf(stderr, "fstat() failed with error: %d %s\n",
+            error, strerror(error));
+    fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
+    fclose(hd_src);
+    return -1;
+  }
+
   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     fprintf(stderr, "curl_global_init() failed\n");
     fclose(hd_src);
index c060b8ac759a908a9f316d263600e1aeb22e9279..2fc85b97d4ee307bd43a1666810002a792c140e5 100644 (file)
@@ -46,34 +46,33 @@ int test(char *URL)
     return -1;
   }
 
+  hd_src = fopen(libtest_arg2, "rb");
+  if(NULL == hd_src) {
+    error = ERRNO;
+    fprintf(stderr, "fopen() failed with error: %d %s\n",
+            error, strerror(error));
+    fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
+    return -2; /* if this happens things are major weird */
+  }
+
   /* get the file size of the local file */
-  hd = stat(libtest_arg2, &file_info);
+  hd = fstat(fileno(hd_src), &file_info);
   if(hd == -1) {
     /* can't open file, bail out */
     error = ERRNO;
-    fprintf(stderr, "stat() failed with error: %d %s\n",
+    fprintf(stderr, "fstat() failed with error: %d %s\n",
             error, strerror(error));
-    fprintf(stderr, "WARNING: cannot open file %s\n", libtest_arg2);
+    fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
+    fclose(hd_src);
     return -1;
   }
 
   if(! file_info.st_size) {
-    fprintf(stderr, "WARNING: file %s has no size!\n", libtest_arg2);
+    fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
+    fclose(hd_src);
     return -4;
   }
 
-  /* get a FILE * of the same file, could also be made with
-     fdopen() from the previous descriptor, but hey this is just
-     an example! */
-  hd_src = fopen(libtest_arg2, "rb");
-  if(NULL == hd_src) {
-    error = ERRNO;
-    fprintf(stderr, "fopen() failed with error: %d %s\n",
-            error, strerror(error));
-    fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
-    return -2; /* if this happens things are major weird */
-  }
-
   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     fprintf(stderr, "curl_global_init() failed\n");
     fclose(hd_src);