]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
windows: Always check return value of GetOverlappedResult().
[libserialport.git] / serialport.c
index bae37ed73a2801359f50b35cfbc4381ddbbf4495..39500f8f93fa699e17c5b13bf6292551ae388198 100644 (file)
@@ -758,18 +758,17 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
        }
 
        /* Start write. */
-       if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl) == 0) {
-               if (GetLastError() == ERROR_IO_PENDING) {
-                       DEBUG("Waiting for write to complete");
-                       GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
-                       DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
-                       RETURN_INT(bytes_written);
-               } else {
-                       RETURN_FAIL("WriteFile() failed");
-               }
-       } else {
+       if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl)) {
                DEBUG("Write completed immediately");
                RETURN_INT(count);
+       } else if (GetLastError() == ERROR_IO_PENDING) {
+               DEBUG("Waiting for write to complete");
+               if (GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE) == 0)
+                       RETURN_FAIL("GetOverlappedResult() failed");
+               DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
+               RETURN_INT(bytes_written);
+       } else {
+               RETURN_FAIL("WriteFile() failed");
        }
 #else
        size_t bytes_written = 0;
@@ -970,17 +969,16 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
        }
 
        /* Start read. */
-       if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0) {
-               if (GetLastError() == ERROR_IO_PENDING) {
-                       DEBUG("Waiting for read to complete");
-                       GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
-                       DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count);
-               } else {
-                       RETURN_FAIL("ReadFile() failed");
-               }
-       } else {
+       if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl)) {
                DEBUG("Read completed immediately");
                bytes_read = count;
+       } else if (GetLastError() == ERROR_IO_PENDING) {
+               DEBUG("Waiting for read to complete");
+               if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0)
+                       RETURN_FAIL("GetOverlappedResult() failed");
+               DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count);
+       } else {
+               RETURN_FAIL("ReadFile() failed");
        }
 
        TRY(restart_wait_if_needed(port, bytes_read));