[英]Stackbased buffer overrun
When running my code I get the following error: 运行我的代码时,出现以下错误:
Unhandled exception at 0x00BA16A0 in GameLauncher.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.
GameLauncher.exe中0x00BA16A0处未处理的异常:堆栈cookie工具代码检测到基于堆栈的缓冲区溢出。
I have no idea what could be causing this. 我不知道是什么原因造成的。 It is caused with the following code:
它是由以下代码引起的:
#include "stdafx.h"
#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
int main()
{
std::cout << "Which process would you like to close? (Include .exe)" << std::endl;
wchar_t userProcessToFind;
std::wcin.getline(&userProcessToFind, 20);
HANDLE processSnapshot;
DWORD processID = 0;
PROCESSENTRY32 processEntery;
processEntery.dwSize = sizeof(PROCESSENTRY32);
processSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, processID);
if(Process32First(processSnapshot, &processEntery) == TRUE)
{
while (Process32Next(processSnapshot, &processEntery) == TRUE)
{
if (_wcsicmp(processEntery.szExeFile, &userProcessToFind) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, processEntery.th32ProcessID);
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}
}
CloseHandle(processSnapshot);
}
return 0;
}
In 在
wchar_t userProcessToFind;
std::wcin.getline(&userProcessToFind, 20);
You have allocated space for a single wchar_t
but you are trying to read in up to 20 characters and place it in the memory at the address of userProcessToFind
. 您已经为单个
wchar_t
分配了空间,但是您尝试读取最多20个字符并将其放置在内存中的userProcessToFind
地址userProcessToFind
。 This will cause stack corruption as you are going to try to write into memory that does not belong to &userProcessToFind
. 当您尝试写入不属于
&userProcessToFind
内存时,这将导致堆栈损坏。 What you need to do is create an array like 您需要做的是创建一个像
wchar_t userProcessToFind[20];
std::wcin.getline(userProcessToFind, 20);
Or you could use a std::wstring
and your code would become 或者您可以使用
std::wstring
,您的代码将变为
std::wstring userProcessToFind;
std::getline(std::wcin, userProcessToFind);
This gives the benefit of not having to use an arbitrary size for the process name as std::wstring
will scale to fit the input. 这样做的好处是不必为进程名称使用任意大小,因为
std::wstring
可以缩放以适合输入。 If you need to pass the underlying wchar_t*
to a function you can use std::wstring::c_str()
to get it. 如果需要将基础
wchar_t*
传递给函数,则可以使用std::wstring::c_str()
来获取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.