c ++和全局变量在函数中发生了变化

时间:2022-03-17 16:49:35

I have these variable defined as global variables outside any function

我将这些变量定义为任何函数之外的全局变量

//testing parameters (init to all nonfail)
int serverRandom = 0; //nonzero == fail > gotofail for first sha1 methode
SSLBuffer sigpar= 0; //nonzero == fail > gotofail for second sha1 methode
string hashOut = "nonfail"; //"fail" == error condition > gotofail for third sha1 methode

These variables are used in this function:

这些变量用于此函数:

static OSStatus SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams, uint8_t *signature, uint16_t signatureLen)
{
    OSStatus err;
    cout << "initval:"<< serverRandom<< signedParams<< hashOut<<endl;

    if ((err = SSLHashSHA1::update(&hashCtx, &serverRandom)) != 0)
        cout << "firstfail" <<endl;
        goto fail;
    if ((err = SSLHashSHA1::update(&hashCtx, &signedParams)) != 0)
        cout <<"secondfail"<<endl;
        goto fail;
        //goto fail;
    if ((err = SSLHashSHA1::final(&hashCtx, &hashOut)) != 0)
        cout << "thirdfail" << endl;
        goto fail;

    cout << "nonfail" << endl;

    fail:
        SSLFreeBuffer(&signedHashes);
        SSLFreeBuffer(&hashCtx);
        return err;
}

Note that the global variables ServerRandom and hashout are accessed directly in that function while the sigpar variable is given to the signedParams arguments of the sslVerify function.

请注意,全局变量ServerRandom和hashout直接在该函数中访问,而sigpar变量则赋予sslVerify函数的signedParams参数。

Now, I wrote a unit test to test each failing case seperatly via this function

现在,我编写了一个单元测试,通过这个函数分别测试每个失败的情况

void unittest(){
    //all tests passing
    cout << "-------------nonfail-------------" << endl;
    result = SSLVerifySignedServerKeyExchange(&ctx,isrsa,sigpar,&sig,siglen);


    //first test fail
    cout << "-------------firstfail-------------" << endl;
    serverRandom = 1;
    result = SSLVerifySignedServerKeyExchange(&ctx,isrsa,sigpar,&sig,siglen);
    serverRandom = 0;

    //second test fail
    cout << "-------------secondfail-------------" << endl;
    sigpar= 1;
    result = SSLVerifySignedServerKeyExchange(&ctx,isrsa,sigpar,&sig,siglen);
    sigpar= 0;

    //third test fail
    cout << "-------------thirdfail-------------" << endl;
    hashOut = "fail";
    result = SSLVerifySignedServerKeyExchange(&ctx,isrsa,sigpar,&sig,siglen);
    hashOut = "nonfail";

}

Now it works perfectly for the first fail case eg. outputting the firstfail line. After that It does not work anymore as it just outputs the ---------secondfail-------- & ------thirdfail------- without outputting the failchecks (secondfail/thirdfail).

现在它适用于第一个失败案例,例如。输出firstfail线。之后它不再起作用,因为它只输出--------- secondfail --------&------ thirdfail -------而不输出failchecks( secondfail / thirdfail)。

note that the initval inside sslverify...() shows the correct values, however the update/final function after the first testcause show 0 as a value for the respective values they recieve.

请注意,sslverify ...()中的initval显示正确的值,但第一次testcause后的update / final函数显示0作为它们收到的相应值的值。

1 个解决方案

#1


0  

well, it was easy to solve in the end:

好吧,最后很容易解决:

in the SSLVerifySignedServerKeyExchange() I just forgot to add {} around the multi-line if's after I added the cout<< so it would always goto fail after the first if

在SSLVerifySignedServerKeyExchange()中,我只是忘了在添加cout < <之后添加{}围绕多行,所以它会在第一个之后总是转到失败< p>

#1


0  

well, it was easy to solve in the end:

好吧,最后很容易解决:

in the SSLVerifySignedServerKeyExchange() I just forgot to add {} around the multi-line if's after I added the cout<< so it would always goto fail after the first if

在SSLVerifySignedServerKeyExchange()中,我只是忘了在添加cout < <之后添加{}围绕多行,所以它会在第一个之后总是转到失败< p>