Hi,

Below is a stupid bug that made me crazy while developping another app.

Enjoy!




.686
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data

msg db "Blah",0

.code
start:
std
invoke MessageBox,0,addr msg, addr msg,0
invoke ExitProcess,0

end start


PS: If the bug don't happen your system you're very lucky ! I tested on both win9x winXP & it crashes the same way.
Posted on 2002-08-08 23:58:47 by Axial
std sets the df flag to 1. I believe will read a data before the data segment??? this is probably causing the error. Add a after this will negate the effect of .
Posted on 2002-08-09 00:07:32 by stryker
Hi stryker,

I know that "STD" causes bug before many api calls but ... I still wonder why microsoft coderz(as they should be aware of that bug) didn't correct it !



pushf

cld
...

popf

[\QUOTE]

IS this too difficult to correct ?
Posted on 2002-08-09 00:19:03 by Axial
Hi Axial,

Why use STD in this case? It is used to decrement both the edi and esi registers.These registers are incremented by default when using things like stosb. If you use STD you should follow it by CLD.

best regards,

czDrillard
Posted on 2002-08-09 00:31:43 by czDrillard
IS this too difficult to correct ?
No, but it's not the job of the message box functon to care about the direction flag or any flags. MessageBox job is to display a message box. This is not a bug but a coding error on the part of the programmer. If MessageBox function uses in its routines, MessageBox will use a before the routine ends. If you use a HLL, a good compiler will correct this.
Posted on 2002-08-09 00:46:54 by stryker

Hi Axial,

Why use STD in this case? It is used to decrement both the edi and esi registers.These registers are incremented by default when using things like stosb. If you use STD you should follow it by CLD.

best regards,

czDrillard



Of course in this case std inst appropriate :) I used it only in order to show the bug. Here is the snippet of code where the bug firstly happened:


lea ebx,
call GetTickCount
and eax,7
cmp word ptr ,'23'
jnz @F
or al,1
@@:
mov esi, dword ptr
inc esi

and byte ptr ,0
invoke lstrcat, addr RegData, esi
invoke lstrcat, addr RegData, addr Ext

mov esi,edi
std
mov al,'\'
repnz scasb
inc edi
inc edi
cld ; <----------------------------------- CLD added : bug free now :)

invoke MessageBox, ebp,addr RegData, edi, ebp

Posted on 2002-08-09 00:57:29 by Axial
Maybe this macro would help
axialcall MACRO a:VARARG

pushfd
cld
invoke a
popfd
ENDM

.data

msg db "Blah",0

.code
start:

std
axialcall MessageBox, 0, OFFSET msg, OFFSET msg, 0
axialcall ExitProcess, 0

end start
Posted on 2002-08-09 00:59:13 by stryker
The bug is in using STD without CLD.

Ultimately, the Win32 API calling convention is the calling convention of the 32-bit C/C++ compiler (VC). And the calling convention requires DF to be cleared before entering and before exiting a function. This little information is not always mentioned when discussing calling (register saving) conventions. If you never change DF, you never need to remember this.
Posted on 2002-08-10 20:20:50 by tenkey
Yep,

That says it all, unless you have a reason to SET the direction flag and this will be something like using string instruction in reverse, setting STD and leaving it is building the bug into your own code.

Regards,

hutch@movsd.com
Posted on 2002-08-11 04:29:22 by hutch--