From 80d4b51b173d9ea9a4a56230d026a2e91bb04f50 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 25 Mar 2009 10:42:02 +0100 Subject: 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 --- src/network/socket/qnativesocketengine_unix.cpp | 20 ++++++++++---------- 1 file 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) -- cgit v0.12