Shared posts
CodeSOD: Overloaded Loop
Brian found himself digging through some C++ code, trying to figure out a cross-thread synchronization bug. It had something to do with the NetLockWait
function, based on his debugging, so he dug into the code.
bool CMyDatastore::NetLockWait(DWORD dwEvent, long nRecord, CMySignal& Signal, DWORD dwTieout)
{
bool retBool;
long value;
DWORD reason;
DWORD currentAttach;
CTimerElapsed timeout;
timeout.SetTime(dwTimeout);
retBool = false;
while (timeout)
{
if (::WaitForSingleObject(Signal.GetEvent(), timeout) != WAIT_OBJECT_0)
{
break;
}
ReserveSynch();
Signal.Pop(reason, value);
ReleaseSynch();
if (reason == dwEvent && value == nRecord)
{
retBool = true;
break;
}
}
return (retBool);
}
While reading this, something leapt out to Brian- the variable timeout
was never changed inside the loop. So, this is a simple bug, right? The timeout is basically ignored, and the loop keeps retrying until it hits one of the break
statements?
Well… no. In fact, the loop did successfully time out, and timeouts had nothing to do with the synchronization bug. Brian couldn’t help but be curious- how did the timeout actually get decremented?
CTimerElapsed::operator DWORD(void)
{
DWORD wait = 0;
DWORD dwCurrentTick;
DWORD dwElapsed;
if (m_dwTimeout == INFINITE)
{
wait = INFINITE;
}
else
{
dwCurrentTick = ::GetTickCount();
if (dwCurrentTick < m_dwStartTicks)
dwElapsed = (UINT_MAX - m_dwStartTicks) + dwCurrentTick;
else
dwElapsed = dwCurrentTick - m_dwStartTicks;
if (dwElapsed < m_dwTimeout)
wait = m_dwTimeout - dwElapsed;
}
return (wait);
}
A number of languages prohibit operator overloading, for good reasons. C++ is highly optimized for autopodóplo- shooting ones own foot. Thus, C++ cheerfully lets you create this monstrosity.
This block here overrides the DWORD
type cast- whenever a CTimerElapsed
object is used where a DWORD
is expected, this code is called to convert the CTimerElapsed
instance into a DWORD
, decrementing the count, and pushing the boundaries of what operator overloading is for.
This code was not the source of Brian’s bug. This code works. It’s not wrong, but it’s wrong.
Full-camo Ferrari F12 M spied testing in Maranello
Filed under: Spy Photos,Geneva Motor Show,Ferrari,Coupe,Performance,Supercars
It's time for an update to the front-engine two seater.Continue reading Full-camo Ferrari F12 M spied testing in Maranello
Full-camo Ferrari F12 M spied testing in Maranello originally appeared on Autoblog on Thu, 10 Nov 2016 10:15:00 EST. Please see our terms for use of feeds.
Permalink | Email this | CommentsDaily Morning Epicness (40 Pictures)
I see what you did there.
They never grow up
Some assholes in Stockton robbed a bank and took a 12 yr old hostage. This was their car after the car chase.
Why you shouldn’t fall asleep at the beach
One of the creepiest thing I found when looking through old photos
290 to 180 and done. Myfitnesspal.
I knew having trashy Facebook friends would eventually pay off.
The struggle is real
Every Movie Is Better with Batman
There are good movies. There are even great movies. But every moive--and I do mean every movie--can be improved simply by adding Batman as a character. The blog It's Better with Batman is dedicated to proving that by photoshopping the Dark Knight into famous scenes.
See? Just like I said: every movie.
But all of us at Neatorama promote an open exchange of ideas, even heretical ones. So if you wish to argue that a movie could not be improved by adding Batman, please state your case in the comments.
-via Nerd Approved
Astrophysicist Neil deGrasse Tyson Fact-Checks Gravity on Twitter
How a hockey star destroyed a nasty online troll on Twitter
Ice hockey star Paul Bissonnette (right) can smash your nose with a punch both in the rink and in the internet. The Coyotes player was being harassed by a typical Twitter imbecile called Matt Hogue (left) when he decided to play hardball on the troll. The results were delicious.