3

最近、C++でMySQLサーバーと通信するための小さなテストプログラムを作成しようとしています。現在、データベースサーバーに接続するためのAPIとしてMySQL Connector /C++を使用しています。oracle/mysqlにはVisualStudio10以降でconnector/c ++を使用する方法に関するドキュメントがほとんどないため、実行するのに非常に長い時間がかかりました。

最後に、すべてが機能するようになった後、アプリケーションを終了しようとすると問題が発生するようです。次の未処理の例外がスローされます。

Unhandled exception at 0x00C62291 in mysql2.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

エラーについて調べたところ、「セキュリティチェック」オプション(/ gsコンパイラオプション)が原因であることがわかりました。このコンパイラオプションを無効にすると、アプリケーションは正常に終了します。

Visual Studio 2012(およびおそらく他のバージョン?)のデフォルトオプションであるため、オフにすべきではないと感じています。

私の質問は次のとおりです。

  • この/gsコンパイラオプションが未処理の例外を引き起こすのはなぜですか?
  • / gsコンパイラオプションをオフにしても安全ですか、それとも大丈夫ですか?

未処理の例外が指すコードは次のとおりです(ファイル内、gs_report.cと呼ばれます)。

#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;
}
4

2 に答える 2

5

/gs オプションは、ランタイムがプログラムのエラーを検出できるようにします。オプションを削除すると、プログラムはそれを検出せずに終了できますが、エラーはまだ残っています。コードまたはライブラリのどこかで、スタック上のメモリが上書きされています。コードをバイナリ検索して、どのステートメントがエラーを引き起こしているかを確認します。また、すべてのデータベース呼び出しからの戻り値を常にチェックすることもできます。

このオプションを無効にしないでください。これを行うと、実際のエラーが隠され、プログラムがハッキングされる可能性があります。

于 2012-12-16T19:43:43.910 に答える
2

メソッド呼び出しの結果コードを確認してください。これは、次の質問に似ています。VisualStudioの/gsフラグがバッファオーバーフローについて警告しているため、 Mysqlコネクタc++を使用したバッファオーバーラン。

于 2012-12-16T20:14:17.020 に答える