-1

ncurses を使用して stdin からパスワードを読み取る必要がある Linux アプリを作成しています。問題なく C スタイルの文字列を読み取ることができますが、これはセキュリティ上のリスクをもたらすため、STL 文字列 (std::string) を読み取る方法を見つける必要があります。私のコードの関連部分は次のとおりです。

initscr();
noecho();
string key;
... // Get the key from the user
string enter=key+"A"; // So the entered key is not the user-set one
while(enter!=key)
{
    const char* msg="Key to unlock terminal? ";
    move(y/2, (x-strlen(msg))/2);
    erase();
    printw(msg);
    sscanw("%s", enter); // How do I read into an STL string?
}
4

1 に答える 1

1

あまり明確ではありませんsscanwが、フォーマットがうまくいく場合sscanfは、入力のサイズを制限することをお勧めします。

char[21] enter;
sscanw("%20s", enter);
enter[20] = 0;

セキュリティの問題には、バッファ オーバーフロー (ユーザーがバッファの末尾を超えてプログラム空間に書き込む) が含まれます。この問題を解決するに%20sは、バッファのサイズに読み込む character( ) の数を制限します。

于 2013-04-02T16:02:48.600 に答える