News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Send Mysteriously Failing With WSAEFAULT (SOLVED)

Started by Max_Power, June 06, 2007, 10:53:19 PM

Previous topic - Next topic

Max_Power

Turns out there is a very subtle bug in Microsoft's RtlFillMemory function. The solution was to write my own.

Greetings,

I am working on an email client that connects to a POP3 server and downloads new emails, but I am having a very odd problem. Here is the relevant code:


LOCAL pszPassPckt:DWORD

invoke lstrlen,ADDR szPassPckt
push eax
invoke lstrlen,pszPassword
pop ecx
add ecx,eax
inc ecx
mov pszPassPckt,alloc(ecx)
invoke wsprintf,pszPassPckt,ADDR szPassPckt,pszPassword

;Send the password
invoke lstrlen,pszPassPckt
.IF FUNC(send,dwSocket,pszPassPckt,eax,0)!=SOCKET_ERROR
invoke recv,dwSocket,ADDR szRecvBuf,MAX_PACKET_LEN,0
       
                 ...
.ENDIF

free pszPassPckt

xor eax,eax
ret


The problem is that when the password packet is of even length and greater than 12 characters (12 works...) send returns SOCKET_ERROR and the error code is WSAEFAULT. I have made sure the packet is readable and writable for the entire chunk being sent, hell I even tried not dynamically allocating the packet buffer and just used a local byte array and got the same results. As a result if the password you log in with makes the packet an even length the program doesn't work, but if your password makes it an odd length it works perfect. Here is the code that creates the socket if that is of any use:


    ...

    ;Initialize the winsock api.
    .IF FUNC(WSAStartup,101h,ADDR wsaData)!=0
    xor eax,eax
  mov edx,ERROR_WSA
  ret
    .ENDIF

    ;Create TCP/IP socket
    .IF FUNC(socket,AF_INET,SOCK_STREAM,0)==INVALID_SOCKET
    xor eax,eax
    mov edx,ERROR_SOCKET
    ret
    .ELSE
    mov dwSocket,eax
    .ENDIF

    ;Set socket operation information
    invoke htons,dwPort
    mov sarAddress.sin_port,ax
    mov sarAddress.sin_family,AF_INET

    .IF FUNC(inet_addr,pszTargetIP)==INADDR_NONE
        xor eax,eax
mov edx,ERROR_INVALID_IP
ret
    .ELSE
mov sarAddress.sin_addr,eax
    .ENDIF

    ;Connect to the server
    .IF FUNC(connect,dwSocket,ADDR sarAddress,sizeof SOCKADDR_IN)==SOCKET_ERROR
xor eax,eax
mov edx,ERROR_CONNECT
ret
    .ENDIF

    mov eax,dwSocket
    ret