Search code examples
arrayscvisual-studiocharruntime-error

Why do vs2019 still prompt at the end of function execution: run time check failure # - stack around the variable 'binR' was corrupted


My program uses socket communication for inadvertent transmission. I hope to get 128 random strings inadvertently transmitted at the same time. How to choose 0 and 1 is determined by the 0 and 1 values of each bit of the input string. Based on the above idea, I made this program.

For the function get_ reuse_ For rule (), I want to enter a 16 bit string (including '\ 0'17 bits) and return the sum of XOR values of 128 ot strings mentioned above.

Unfortunately, there was a runtime error, but I can still get the final XOR.

The error is at the end of the function

Here I show the code

int main(){
    socket_conn();
    char *rule1="networkingjjjjjj";//小于16 (10)
    char re_rule[17];
    memset(re_rule, 0, sizeof(re_rule));
    get_reuse_rule(rule1, re_rule);
    socket_clean();
    system("pause");
    return 0;
}

Error function

void get_reuse_rule(char *rule,char *re_rule) {
    int binR[128];
    memset(binR, 0, sizeof(binR));
    get_arraized_rule(rule, binR);
    //根据规则进行ot确认key_array
    char ki[128][17];
    memset(ki, 0, sizeof(ki));
    for (int i = 0; i < 128; i++) printf("%d", binR[i]);
    printf("\n");
    ot_128_get(ki, binR);
    
    for (int i = 0; i < 128; i++) {
        printf("%s\n", ki[i]);
    }

    printf("%d\n", strlen(ki[127]));

    //得到了对应的一组ki与规则进行异或加密规则
    char Irule[17];//可重用的规则
    memset(Irule, 0, sizeof(Irule));
    for (int i = 0; i < 128; i++) {
        for (int j = 0; j < 16; j++) {
            Irule[j] = Irule[j] ^ ki[i][j];
        }
    }
    printf("\n%s\n", Irule);
    memcpy(re_rule, Irule, sizeof(Irule));
    //memset(ki, 0, sizeof(ki));
    
}

Another function called Ki, which I tested in the main function with no problem

void ot_128_get(char (*choose_ki)[17],int *bin_rule) {
    for (int i = 0; i < 128; i++) {
        ot_get_msg(choose_ki[i], bin_rule[i]);
        printf("%d\n", i);
    }
    printf("\not all is ok!\n");
}

ot_get_msg(choose_ki[i], bin_rule[i]):

According to the second parameter is 0 or 1 to receive the inadvertent transmission, the first parameter as the return value

I searched through the search engine and mentioned that this problem may be caused by array out of bounds, but I don't think there is array out of bounds in my program. If you know how to solve this problem, please leave me a message. Thank you for your reading.

-------------------------new update---------------------------------

I have written a function that is almost the same. I put the local variables that may be wrong into the main function. Nothing else has changed. When I don't close the program, I won't report an error. Closing the program will cause the same error, but this time the error report is at the end of the main function. enter image description here

----------------ot_get_msg------------------------

void ot_get_msg(char *get_msg,int choose) {
    //int choose = 1;//先写死选择1
    int r;
    char buf[1024];
    memset(buf, 0, sizeof(buf));
    r= recv(clientSocket1, buf, 1023, NULL);

    //printf("%s", buf);//n/e/rmsg1/rmsg2
    char* p[4];
    p[0] = strtok(buf, "/");
    p[1] = strtok(NULL, "/");
    p[2] = strtok(NULL, "/");
    p[3] = strtok(NULL, "/");
    //strtok(NULL, "/");
    char v[100];
    char k[100];
    memset(v, 0, sizeof(v));
    memset(k, 0, sizeof(k));
    ot_recv_compute_v(v, k, p[0], p[1], p[2], p[3],choose);

    //printf("V:%s\n", v);
    //printf("K:%s\n", k);
    //把v发送给client
    send(clientSocket1, v, strlen(v), NULL);

    char buf2[1024];
    memset(buf2, 0, sizeof(buf2));
    recv(clientSocket1, buf2, 1023, NULL);
    char* enmsg[2];
    enmsg[0] = strtok(buf2, "/");
    enmsg[1] = strtok(NULL, "/");
    //解密
    char demsg1[100];
    char demsg2[100];
    memset(demsg1, 0, sizeof(demsg1));
    memset(demsg2, 0, sizeof(demsg2));
    ot_decode_msg(demsg1, demsg2, enmsg[0], enmsg[1], k);
    //printf("%s\n", demsg1);
    //printf("%s\n", demsg2);
    if (choose == 0) {
        memcpy(get_msg, demsg1,sizeof(demsg1));
    }
    else {
        memcpy(get_msg, demsg2,sizeof(demsg2));
    }
    //closesocket(servSOCKET);
}

error codeRun-Time Check Failure #2 - Stack around the variable 'ki' was corrupted.


Solution

  • I think I've found a way to make the program run without errors. The Chinese version is as follows in here.

    set:project > Configuration Properties > C / C + + - > code generation > Basic runtime check as the default value

    The original guess is: "when the project reaches a certain size, the programmer will occupy a large amount of stack. I also have a deep understanding. Because I originally wrote a class, there was no error at runtime, but when adding member properties, it is easy to make such errors in other ways. Therefore, I guess that vs internally limits the size of the stack. When the item is large enough, it will overflow. "