1

sprintf文字列をフォーマットし、結果をスタック変数に格納するために呼び出しようとしています。しかし、私の試みは惨めに失敗し、即座にクラッシュします。

sub esp, 0x100                                  ;Allocate 256 bytes on the stack.
push dword[RequestedFile]                       ;push string2
push dword[Host]                                ;push string1
push dword[GetHeader]                           ;push format   "String1: %s, String2: %s"
push dword[ebp - 0x04]                          ;push buffer/stack variable
call [sprintf]                                  ;store string in buffer
add esp, 0x10                                   ;restore stack

push dword[ebp - 0x04]                          ;push the stack variable.
push StringFormat                               ;push the format
call [printf]                                   ;print the new string.
add esp, 0x08                                   ;restore the stack

add esp, 0x100                                  ;destroy the stack variable.

私が間違っていることはありますか?

4

1 に答える 1

2

バッファへのポインタであるかのように使用[ebp-4]していますが、実際にはバッファの最後の 4 バイトにあるランダムなメモリ ガベージです (まだスタックから何も割り当てられていないと仮定します)。使い続けたい場合は[ebp-4]、それもスタックから割り当てて、アドレスに初期化する必要があります。例えば:

sub esp, 0x104                  ;Allocate 256 bytes buffer and 4 bytes pointer
mov dword[ebp - 0x04], esp      ;store address of buffer in local variable
push dword[RequestedFile]       ;push string2
push dword[Host]                ;push string1
push dword[GetHeader]           ;push format   "String1: %s, String2: %s"
push dword[ebp - 0x04]          ;push buffer/stack variable
call [sprintf]                  ;store string in buffer
add esp, 0x10                   ;restore stack

push dword[ebp - 0x04]          ;push the stack variable.
push StringFormat               ;push the format
call [printf]                   ;print the new string.
add esp, 0x08                   ;restore the stack

add esp, 0x104                  ;destroy the stack variables.
于 2014-05-05T00:25:57.590 に答える