The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: ecube on November 23, 2006, 07:33:29 AM

Title: I think I found a bug in windows
Post by: ecube on November 23, 2006, 07:33:29 AM
Well the bug I think I found applys to createmutex. According to  http://tinyurl.com/bkwe  "If the function fails, the return value is NULL. To get extended error information, call GetLastError". Calling GetLastError right after the function call works fine but if you just check the return value againts NULL it'll fail every single time. Regardless if the mutex is created or not, I tested on xp and xp sp2.

to test this run it once which should create the mutex in memory(you can check with process exporer if you want) then run it again which it should tell you the mutex is already created and exits, but doesn't. Afterwards manually terminate this process since it loops in mem to keep mutex alive.

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

.data
TheStr db "themutext",0
Exists db "Mutex already exists",0
Creating db "Creating mutex",0
.code
start:
invoke CreateMutex,NULL,TRUE,addr TheStr
.if eax==NULL
invoke MessageBox,0,addr Exists,addr Exists,MB_ICONINFORMATION ;if fails assume it already exists
.else
invoke MessageBox,0,addr Creating,addr Creating,MB_ICONINFORMATION
@@:
invoke Sleep,2000
jmp @B
.endif
invoke ExitProcess,0
end start
Title: Re: I think I found a bug in windows
Post by: evlncrn8 on November 23, 2006, 07:44:19 AM
i usually use openmutex to check if the mutex is already created..
never encountered this issue though..
will check later on when i have some free time.. could be an interesting find :)
Title: Re: I think I found a bug in windows
Post by: MichaelW on November 23, 2006, 07:48:45 AM
CreateMutex is supposed to return a handle if it succeeds, or null if it fails.

MSDN: CreateMutex (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createmutex.asp)
Title: Re: I think I found a bug in windows
Post by: hutch-- on November 23, 2006, 08:00:12 AM
 :bg

>      I think I found a bug in windows

Our name is LEGION for we are many. (Bugs in Windows)  :bdg
Title: Re: I think I found a bug in windows
Post by: ecube on November 23, 2006, 08:33:46 AM
Quote from: MichaelW on November 23, 2006, 07:48:45 AM
CreateMutex is supposed to return a handle if it succeeds, or null if it fails.

MSDN: CreateMutex (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createmutex.asp)


I know that, and it doesn't ever return NULL even during failure, that's the whole point of the test :)
Title: Re: I think I found a bug in windows
Post by: MichaelW on November 23, 2006, 09:06:50 AM
I had forgotten about this detail. You would think that a function named CreateMutex would return failure if it failed to create the mutex object. But it actually returns the handle to the existing object, and sets LastError to ERROR_ALREADY_EXISTS.

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

.data
TheStr db "themutext",0
Error db "Error creating mutex",0
Exists db "Mutex already exists",0
Creating db "Creating mutex",0
.code
start:
invoke CreateMutex,NULL,TRUE,addr TheStr
.if eax==NULL
invoke MessageBox,0,addr Exists,addr Error,MB_ICONINFORMATION ;if fails assume it already exists
.else
      invoke GetLastError
      .if eax==ERROR_ALREADY_EXISTS
        invoke MessageBox,0,addr Exists,addr Exists,MB_ICONINFORMATION
      .else     
  invoke MessageBox,0,addr Creating,addr Creating,MB_ICONINFORMATION
      .endif 
@@:
invoke Sleep,2000
jmp @B
.endif
invoke ExitProcess,0
end start