Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ cout和sleep会导致不同的结果_C++_Postgresql_Bcrypt_Libpqxx - Fatal编程技术网

C++ cout和sleep会导致不同的结果

C++ cout和sleep会导致不同的结果,c++,postgresql,bcrypt,libpqxx,C++,Postgresql,Bcrypt,Libpqxx,我有一个类,它接受用户输入(用户名/密码),bcrypt对输入密码进行散列,以检查它是否与存储在数据库中的散列匹配,如果成功,则让用户登录。我遇到的问题是,如果我像评论中提到的那样调用cout,您就会出现同步问题 根据经验,每当sleep或cout(或printf)解决您的问题时,在程序中的某些语句中,您都没有正确实现互斥逻辑 你考虑纠正Cuffice密码函数。 您是否100%确定存储过程不需要某种同步 附言: 令人困惑的“\n”效应具有刷新缓冲区的习惯,导致同步。听起来像是可能由数据竞争引起的

我有一个类,它接受用户输入(用户名/密码),bcrypt对输入密码进行散列,以检查它是否与存储在数据库中的散列匹配,如果成功,则让用户登录。我遇到的问题是,如果我像评论中提到的那样调用
cout,您就会出现同步问题

根据经验,每当sleep或cout(或printf)解决您的问题时,在程序中的某些语句中,您都没有正确实现互斥逻辑

<>你考虑纠正Cuffice密码函数。 您是否100%确定存储过程不需要某种同步

附言:
令人困惑的“\n”效应具有刷新缓冲区的习惯,导致同步。

听起来像是可能由数据竞争引起的计时问题。如果您使用的是线程,那么您是否正确地使用了互斥体?我使用的是线程(它是一个web服务器),据我所知,我正确地使用了互斥体。在这个特定的示例中,唯一需要的互斥是pqxx连接,它在第一个代码段方法的开头被锁定。您是否检查了字符串是否为空/是否有效?我现在引入了一个检查,并且字符串不是零(它是60个字符,这是哈希的正确大小)。此外,我的字符串检查没有使
checkPassword()
正确计算。上一次检查是在调用
checkPassword()
之前完成的。将检查置于
checkPassword()
中会导致
checkPassword()
正确运行。
// pqxx::result
string storedPass = result.begin()["passwordBCrypt_12"].as<string>();

// Uncommenting either cout or sleep causes checkPassword to work as expected
//cout << "\n"; // Confusingly, cout must contain "\n" to have the effect
//sleep(1);
if (!checkPassword(inputPass, storedPass))
    credError = true;
bool DB::checkPassword(string& password, string& passwordHash){
    char cpassword[password.length()];
    char hashInDatabase[BCRYPT_HASHSIZE];
    char outTestHash[BCRYPT_HASHSIZE];

    for (size_t i = 0; i < password.length(); i++){
        cpassword[i] = password[i];
    }
    for (size_t i = 0; i < BCRYPT_HASHSIZE; i++){
        hashInDatabase[i] = passwordHash[i];
    }

    if (bcrypt_hashpw(cpassword, hashInDatabase, outTestHash) == 0){
        if (strcmp(hashInDatabase, outTestHash) == 0) {
            // password matches
            return true;
        }
        // password does not match
    }
    return false;
}