diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2009-03-25 09:42:02 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2009-03-25 09:42:02 (GMT) |
commit | 80d4b51b173d9ea9a4a56230d026a2e91bb04f50 (patch) | |
tree | 4e76f31db2c8a3b410e94e89d6c13ee80d5c10b4 | |
parent | e34a24dcd49c055741c9a5914479842021175551 (diff) | |
download | Qt-80d4b51b173d9ea9a4a56230d026a2e91bb04f50.zip Qt-80d4b51b173d9ea9a4a56230d026a2e91bb04f50.tar.gz Qt-80d4b51b173d9ea9a4a56230d026a2e91bb04f50.tar.bz2 |
Fix a small mistake in recalculating the timeout in case we get
interrupted twice by a signal.
If we're interrupted only once, there's no problem. If we're
interrupted twice, we subtract the elapsed time since the beginning
from the remaining time, so this won't work. The correct thing is to
recalculate from the original timeout value.
This is extremely difficult to test, since it requires that the
select(2) call be interrupted twice by signals. The only way to do
this is by sending two signals to a program from another program (or
threads, with pthread_kill(3)) with less than half of the time left,
then send data to cause the loop to exit with success.
Reviewed-by: Peter Hartmann <peter.hartmann@nokia.com>
-rw-r--r-- | src/network/socket/qnativesocketengine_unix.cpp | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/src/network/socket/qnativesocketengine_unix.cpp b/src/network/socket/qnativesocketengine_unix.cpp index 534f7ec..73f6f84 100644 --- a/src/network/socket/qnativesocketengine_unix.cpp +++ b/src/network/socket/qnativesocketengine_unix.cpp @@ -884,15 +884,15 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) co if (timeout > 0) { // recalculate the timeout - timeout -= timer.elapsed(); - tv.tv_sec = timeout / 1000; - tv.tv_usec = (timeout % 1000) * 1000; - - if (timeout < 0) { + int t = timeout - timer.elapsed(); + if (t < 0) { // oops, timeout turned negative? retval = -1; break; } + + tv.tv_sec = t / 1000; + tv.tv_usec = (t % 1000) * 1000; } } while (true); @@ -927,15 +927,15 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool c if (timeout > 0) { // recalculate the timeout - timeout -= timer.elapsed(); - tv.tv_sec = timeout / 1000; - tv.tv_usec = (timeout % 1000) * 1000; - - if (timeout < 0) { + int t = timeout - timer.elapsed(); + if (t < 0) { // oops, timeout turned negative? ret = -1; break; } + + tv.tv_sec = t / 1000; + tv.tv_usec = (t % 1000) * 1000; } } while (true); if (ret <= 0) |