Stack around the variable 'variable name' was corrected Please ask me an error question. crying

Asked 1 years ago, Updated 1 years ago, 91 views

char* my_strcap(char* str1, char* str2) { char* result = (char*)malloc(sizeof(char) * 40);

int i, j;

if (result != NULL)
{
    //String is stored in array form, so it is stored in each array index
    for (i = 0; i < *(str1 + i) != 0; i++)
    {
        *(result + i) = *(str1 + i);
    }
    //i+j because the first array must be stored in the next index
    //If the first string is 4 characters, i will spin to 0,1,2,3 and move on to 4
    //that is, you will put it in the fourth index first
    for (j = 0; i < *(str2 + j) != 0; j++)
    {
        *(result + i + j) = *(str2 + j);
    }
    //to specify the end of the string.
    //Also, if the second string is 3 characters, j goes to 3
    //that is, it will be placed in an index of 4+3 = 7.
    //abcd + efg (+ null), a total of 7 characters, an array of arr[7].
    *(result + i + j) = NULL;
}

return result;

}

int main() { char input1[21], input2[21]; int input_1 = 0, input_2 = 0;

do
{
    scanf("%s %s", input1, input2);

    input_1 = strlen(input1);
    input_2 = strlen(input2);

    printf("%d, %d", input_1, input_2);

    if (input_1 <= 20 && input_2 <= 20)
    {
        printf("%s", my_strcap(input1, input2));
    }
    else
    {
        printf (terminated with an input error).\n\n");
    }

} } while (input_1 > 20 || input_2 > 20);

return 0;

}

Let me ask you a question.

The syntax is a code that accepts two strings of up to 20 characters, and connects the two strings if they are up to 20 characters each.

If two strings are less than 20 characters at a time, it will proceed without a problem.

If at least one of the two strings exceeds 20 characters, you will be re-entered in this process

The error Stack around the variable 'variable name' was corrected. appears at the end even if it is displayed at the end.

This is larger than the size of the array? It's said to occur when input goes in.No matter how hard I think about it, I don't know the solution.

What should I do?

stackoverflow

2022-09-20 17:22

2 Answers

To put it simply by linux...

-------Stack-----
input_B
input_A
canary bit
eip
------------------

I think input_B and input_A will be different depending on the compiler Anyway... if the length of a particular variable (in the above case, input_A) is exceeded, it has contaminated the canister bit to identify memory contamination (hacking) in the program.

The solution is

#define_CRT_SECURE_NO_WARNINGS Turn this off and use scanf_s... Verify the length to be entered.


2022-09-20 17:22

I don't know if it's an answer, but I'll write down my opinion. I'm done! But I think it's a code that stops if it's less than 20 characters and re-enters if it's more than 20 characters.

abcdefghijklmnopqrstuvwxyz //First input
asdfdsasdf //second input
26, Ended with 10 input error. //An error has occurred and must be terminated
//printf ("Ended with input error).\n\n"); that's why it's changed twice 
Asdfdsaasdfdsadsfds//back to loop.
adsfasdfa
19, 9asdfdsa asdfdsaadsfdsfadsfasdfa //You have entered it correctly and it will end.

I/O is as above.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

char* my_strcap(char* str1, char* str2)
{
    char* result = (char*)malloc(sizeof(char) * 40);

    int i, j;

    if (result != NULL)
    {
        //String is stored in array form, so it is stored in each array index
        for (i = 0; i < *(str1 + i) != 0; i++)
        {
            *(result + i) = *(str1 + i);
        }
        //i+j because the first array must be stored in the next index
        //If the first string is 4 characters, i will spin to 0,1,2,3 and move on to 4
        //that is, you will put it in the fourth index first
        for (j = 0; i < *(str2 + j) != 0; j++)
        {
            *(result + i + j) = *(str2 + j);
        }
        //to specify the end of the string.
        //Also, if the second string is 3 characters, j goes to 3
        //that is, it will be placed in an index of 4+3 = 7.
        //abcd + efg (+ null), a total of 7 characters, an array of arr[7].
        *(result + i + j) = NULL;
    }

    return result;
}

int main()
{
    char input1[21], input2[21];
    int input_1 = 0, input_2 = 0;

    do
    {
        scanf("%s %s", input1, input2);

        input_1 = strlen(input1);
        input_2 = strlen(input2);

        printf("%d, %d", input_1, input_2);

        if (input_1 <= 20 && input_2 <= 20)
        {
            printf("%s\n", my_strcap(input1,input2));//I put \n to make it look good.
        }
        else
        {
            printf (terminated with an input error).\n\n");
        }

    } while (input_1 < 20 && input_2 < 20); // the sign here was the opposite. (>) and (|)

    return 0;
}

I don't know how to solve it, but in my case, the Stack around the variable 'variable name' was corrected. error is Both input1 and input2 were above 20.


2022-09-20 17:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.