[PATCH] handle some failed open conditions.

Marcus Meissner marcus at jet.franken.de
Mon Apr 9 11:05:32 CDT 2007


spotted by Coverity.

Not all might really happen, but check for it
anyway.

Ciao, Marcus
---
 dlls/msvcrt/tests/file.c |   63 +++++++++++++++++++++++++--------------------
 1 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c
index d7d7676..1b4e988 100644
--- a/dlls/msvcrt/tests/file.c
+++ b/dlls/msvcrt/tests/file.c
@@ -563,24 +563,28 @@ static void test_file_write_read( void )
 
   memset(btext, 0, LLEN);
   tempfd = _open(tempf,_O_APPEND|_O_RDWR); /* open for APPEND in default mode */
-  ok(tell(tempfd) == 0, "bad position %lu expecting 0\n", tell(tempfd));
-  ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_APPEND got bad length\n");
-  ok( memcmp(mytext,btext,strlen(mytext)) == 0, "problems with _O_APPEND _read\n");
-  _close(tempfd);
+  if (tempfd != -1) {
+    ok(tell(tempfd) == 0, "bad position %lu expecting 0\n", tell(tempfd));
+    ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_APPEND got bad length\n");
+    ok( memcmp(mytext,btext,strlen(mytext)) == 0, "problems with _O_APPEND _read\n");
+    _close(tempfd);
+  }
 
   /* Test reading only \n or \r */
   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
-  _lseek(tempfd, -1, FILE_END);
-  ret = _read(tempfd,btext,LLEN);
-  ok(ret == 1, "_read expected 1 got bad length: %d\n", ret);
-  _lseek(tempfd, -2, FILE_END);
-  ret = _read(tempfd,btext,LLEN);
-  ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret);
-  _lseek(tempfd, -3, FILE_END);
-  ret = _read(tempfd,btext,2);
-  ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
-  ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
-  _close(tempfd);
+  if (tempfd != -1) {
+    _lseek(tempfd, -1, FILE_END);
+    ret = _read(tempfd,btext,LLEN);
+    ok(ret == 1, "_read expected 1 got bad length: %d\n", ret);
+    _lseek(tempfd, -2, FILE_END);
+    ret = _read(tempfd,btext,LLEN);
+    ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret);
+    _lseek(tempfd, -3, FILE_END);
+    ret = _read(tempfd,btext,2);
+    ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
+    ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
+    _close(tempfd);
+  }
 
   ret = unlink(tempf);
   ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
@@ -593,20 +597,23 @@ static void test_file_write_read( void )
      "_write _O_BINARY bad return value\n");
   _close(tempfd);
   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
-  ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
-     "_read _O_BINARY got bad length\n");
-  ok( memcmp(dostext,btext,strlen(dostext)) == 0,
-      "problems with _O_BINARY _write / _read\n");
-  ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
-  _close(tempfd);
+  if (tempfd != -1) {
+    ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
+       "_read _O_BINARY got bad length\n");
+    ok( memcmp(dostext,btext,strlen(dostext)) == 0,
+        "problems with _O_BINARY _write / _read\n");
+    ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
+    _close(tempfd);
+  }
   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
-  ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
-     "_read _O_TEXT got bad length\n");
-  ok( memcmp(mytext,btext,strlen(mytext)) == 0,
-      "problems with _O_BINARY _write / _O_TEXT _read\n");
-  _close(tempfd);
-
-   ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
+  if (tempfd != -1) {
+    ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
+       "_read _O_TEXT got bad length\n");
+    ok( memcmp(mytext,btext,strlen(mytext)) == 0,
+        "problems with _O_BINARY _write / _O_TEXT _read\n");
+    _close(tempfd);
+  }
+  ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
   ok( ret == 0,
      "Can't chmod '%s' to read-write: %d\n", tempf, errno);
   ret = unlink(tempf);
-- 
1.4.3.4



More information about the wine-patches mailing list