Windows Event tutorial bug
- From: NoSpam@xxxxxxxxxxx (Bob Masta)
- Date: Mon, 25 Jun 2007 14:15:06 GMT
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!
.
- Follow-Ups:
- Re: Windows Event tutorial bug
- From: ArarghMail706NOSPAM
- Re: Windows Event tutorial bug
- From: Jascwa
- Re: Windows Event tutorial bug
- From: Betov
- Re: Windows Event tutorial bug
- Prev by Date: Re: AT&T or Intel syntax ?
- Next by Date: Re: Windows Event tutorial bug
- Previous by thread: Number of cpu cores
- Next by thread: Re: Windows Event tutorial bug
- Index(es):
Relevant Pages
|