Windows Event tutorial bug



I'm like to bring the attention of the assembly community
to a serious bug in an on-line tutorial on how to use
Windows Events. The code in question is the thread handler:

************************
ThreadProc PROC USES ecx Param:DWORD
invoke WaitForSingleObject,hEventStart,INFINITE
mov ecx,600000000
.WHILE ecx!=0
.if EventStop!=TRUE
add eax,eax
dec ecx
.else
invoke MessageBox,hwnd,ADDR StopString,ADDR
AppName,MB_OK
mov EventStop,FALSE
jmp ThreadProc
.endif
.ENDW
invoke PostMessage,hwnd,WM_FINISH,NULL,NULL
invoke EnableMenuItem,hMenu,IDM_START_THREAD,MF_ENABLED
invoke EnableMenuItem,hMenu,IDM_STOP_THREAD,MF_GRAYED
jmp ThreadProc
ret
ThreadProc ENDP

*********************
The bug is the use of the
jmp ThreadProc
instructions. The problem with these is that (at least
under MASM) the ThreadProc target is *not* the INVOKE
line at the apparent start of the code; there are hidden
PUSH instructions due to the PARAM and USES in
the PROC line. So, every time the JMP is hit, more params
are pushed onto the stack.

If you use an arrangement like this in something that handles
a lot of events (such as wave buffer handlers) you can eventually
overflow the stack. In Win95 this will produce the traditional stack
overflow error, but in XP it can cause the program to close abruptly
without warning (or any cleanup, etc), nor any type of error
message... just vanish from the screen.

This type of bug can have a really delayed effect, so it might go
undetected for a long time. Then when the crash happens, it
is not at all clear where to look for the trouble... *very* hard to
debug. (Ask me how I know this! <g>)

The simplest way to code this properly is to insert a local label just
before the INVOKE, and jump to that instead.

Best regards,



Bob Masta

D A Q A R T A
Data AcQuisition And Real-Time Analysis
www.daqarta.com
Scope, Spectrum, Spectrogram, Signal Generator
Science with your sound card!

.



Relevant Pages

  • Re: Windows Event tutorial bug
    ... ThreadProc PROC USES ecx Param:DWORD ... invoke WaitForSingleObject,hEventStart,INFINITE ... jmp ThreadProc ... The bug is the use of the ...
    (comp.lang.asm.x86)
  • Re: Windows Event tutorial bug
    ... Windows Events. ... ThreadProc PROC USES ecx Param:DWORD ... invoke WaitForSingleObject,hEventStart,INFINITE ... The bug is the use of the ...
    (comp.lang.asm.x86)
  • Re: Control.BeginInvoke is NOT fire-and-forget
    ... Do you think this is a CLR bug or it is by design? ... concise-but-complete code sample that reliably demonstrates the ... helper.postProcessing = delegate; ... of the worker threadproc from BeginInvoke to Invoke does cure the problem. ...
    (microsoft.public.dotnet.languages.csharp)
  • System.Threading.Timer improper function in Release mode
    ... I've encountered a strange phenomena which appears to me as a bug: ... I have an engine that uses a System.Threading.Timer to invoke a delegate ... The workaround solution I used was declaring the timer as static. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Debug vs. Release build in Delphi?
    ... > and invoke them form there. ... D7 has a bug here - it will haphazardly remove all BAT files from ... the project group. ...
    (borland.public.delphi.non-technical)