/gs选项导致程序引发异常 最近,我一直在尝试编写一个小测试程序,与C++中的MySQL服务器通信。我目前正在使用MySQL Connector/C++作为API连接到数据库服务器。我花了很长时间才让它运行起来,因为oracle/mysql几乎没有关于如何将connector/c++与Visual Studio 10+结合使用的文档
最后,在一切正常后,当应用程序试图退出时,似乎出现了一些问题。它抛出以下未处理的异常: mysql2.exe中0x00C62291处的未处理异常:堆栈cookie检测代码检测到基于堆栈的缓冲区溢出 在研究了错误之后,我发现这是由于安全检查选项/gs编译器选项造成的。当我禁用此编译器选项时,应用程序将正常退出 我觉得我不应该关闭它,因为它是Visual Studio 2012和其他版本中的默认选项 我的问题是: 为什么此/gs编译器选项会导致未处理的异常? 关闭/gs编译器选项是否安全? 以下是未经处理的异常在一个名为:gs_report.c的文件中指向的代码段:/gs选项导致程序引发异常 最近,我一直在尝试编写一个小测试程序,与C++中的MySQL服务器通信。我目前正在使用MySQL Connector/C++作为API连接到数据库服务器。我花了很长时间才让它运行起来,因为oracle/mysql几乎没有关于如何将connector/c++与Visual Studio 10+结合使用的文档,c++,mysql,visual-studio,security,C++,Mysql,Visual Studio,Security,最后,在一切正常后,当应用程序试图退出时,似乎出现了一些问题。它抛出以下未处理的异常: mysql2.exe中0x00C62291处的未处理异常:堆栈cookie检测代码检测到基于堆栈的缓冲区溢出 在研究了错误之后,我发现这是由于安全检查选项/gs编译器选项造成的。当我禁用此编译器选项时,应用程序将正常退出 我觉得我不应该关闭它,因为它是Visual Studio 2012和其他版本中的默认选项 我的问题是: 为什么此/gs编译器选项会导致未处理的异常? 关闭/gs编译器选项是否安全? 以下是未
#if defined (_M_IX86) || defined (_M_X64)
if (IsProcessorFeaturePresent(PF_FASTFAIL_AVAILABLE))
#endif /* defined (_M_IX86) || defined (_M_X64) */
__fastfail(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE);
这是我的申请代码:
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Let's have MySQL count from 10 to 1..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;
/* Create a connection */
driver = sql::mysql::get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "MSxa5y");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT)");
delete stmt;
/* '?' is the supported placeholder syntax */
pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
for (int i = 1; i <= 10; i++) {
pstmt->setInt(1, i);
pstmt->executeUpdate();
}
delete pstmt;
/* Select in ascending order */
pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
res = pstmt->executeQuery();
/* Fetch in reverse = descending order! */
res->afterLast();
while (res->previous())
cout << "\t... MySQL counts: " << res->getInt("id") << endl;
delete res;
delete pstmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "() on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
/gs选项允许运行时检测程序中的错误。删除该选项允许程序在未检测到的情况下退出,但错误仍然存在。在代码或库中的某个地方,堆栈上的内存正在被覆盖。我会尝试对代码进行二进制搜索,看看哪个语句导致了错误。您可能还希望始终检查所有数据库调用的返回值
您绝对不应该禁用此选项。这样做将隐藏真正的错误,并可能使您的程序受到黑客攻击 确保检查方法调用的结果代码。这听起来类似于这个问题:因为Visual Studio的/gs标志警告缓冲区溢出。我甚至尝试将代码缩短为一个简单的数据库连接,然后断开连接。它仍然给出相同的错误。因此,我假设这是由Connector/C++库引起的。如果你在调试器中单步执行,你能看到哪个调用触发了缓冲区溢出吗?正如上面的警告一样,Raymond Chen的以下文章值得一读;我在最近的VS2017调试会话中发现,编辑并继续也会导致这种情况。