调用sprintf在程序集中崩溃 [英] call to sprintf crashing in assembly

查看:208
本文介绍了调用sprintf在程序集中崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用sprintf格式化字符串并将结果存储在堆栈变量中.但是,我的尝试惨遭失败,并立即崩溃.

I'm trying to call sprintf to format a string and store the result in a stack variable. However, my attempt is failing miserably and it crashes instantly.

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.

有什么想法我在做什么错吗?

Any ideas what I'm doing wrong?

推荐答案

您正在使用[ebp-4],就好像它是指向缓冲区的指针一样,而实际上它只是缓冲区最后4个字节中的随机内存垃圾(假设尚未从堆栈中分配任何其他内容).如果要继续使用[ebp-4],则还需要从堆栈中分配它,并将其初始化为地址.例如:

You are using [ebp-4] as if it were a pointer to your buffer, when in fact it is just random memory garbage in the last 4 bytes of your buffer (assuming nothing else has been allocated from the stack yet). If you want to keep using [ebp-4] you will need to allocate that from the stack too and initialize it to the address. For example:

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.

这篇关于调用sprintf在程序集中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆