简体   繁体   English

如何修复 Visual Studio 中的堆叠溢出错误?

[英]How do I fix a stacked overflow error in visual studio?

When I attempt to debug the code listed below in Visual Studio 2019, I get a stacked cookie buffer overrun error before the system pause line of the main function.当我尝试在 Visual Studio 2019 中调试下面列出的代码时,我在主 function 的系统暂停行之前收到堆栈 cookie 缓冲区溢出错误。 I can not figure out how to fix it.我不知道如何解决它。 I am new to programming and have no clue what to do.我是编程新手,不知道该怎么做。 Can someone please help me understand?有人可以帮我理解吗?

#define _CRT_SECURE_NO_WARNINGS

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

/************************************************************************************/
int Greater(int na, int nb);
void DisplayArray(int arr[], int asize);
/************************************************************************************/


/************************************************************************************/
int main(void)
{

    int myArray[10] = { 43, 24, 76, 11, 37, 34, 55, 49, 5 };  /* initialize an array */
    int asize = 10;  /* set the size of the above array */

    int na, nb;
    int result;

    printf("Enter 2 integers\n");
    scanf("%d %d", &na, &nb);/* the variables were missing the & in the scanf function call*/
    result = Greater(na, nb);

    fprintf(stdout, "na = %d nb = %d greater = %d\n", na, nb, result);

    ModifyArray(myArray, asize);
    fprintf(stdout, "Array after modification\n");
    DisplayArray(myArray, asize);


    system("pause");
    return 0;
}

/***************************************************************************************
This function returns the larger of its two arguments
***************************************************************************************/
int Greater(int na, int nb) {

    if (na >= nb)
        return na;
    else
        return nb;
}

/***************************************************************************************
This function modifies the elements of an array
***************************************************************************************/
int ModifyArray(int arr[], int asize) {
    int i;

    /* Add 5 to each element of array */
    for (i = 0; i <= asize; i++); {
        arr[i] += 5;
    }
}

/***************************************************************************************
Display function
/***************************************************************************************/
void DisplayArray(int arr[], int asize) {
    int i;

    for (i = 0; i < asize; i++) {
        fprintf(stdout, "i = %d arr[i] = %d\n", i, arr[i]);
    }

A problem with a stack based cookie usually means that you have tried to write outside of a buffer that is stored in the stack (usually, local variables are stored in the stack) and have overwritten a value that Visual Studio inserted just to detect these kind of bugs (as buffer overwrites in the stack can lead to exploitable vulnerabilities in applications).基于堆栈的 cookie 的问题通常意味着您尝试在存储在堆栈中的缓冲区之外写入(通常,局部变量存储在堆栈中)并覆盖了 Visual Studio 插入的值,只是为了检测这些类型错误(因为堆栈中的缓冲区覆盖可能导致应用程序中的可利用漏洞)。 If your code had behaved correctly, the cookie would not have been modified and you would not have run into this error.如果您的代码行为正确,则 cookie 不会被修改,您也不会遇到此错误。

As Ron commented, the problem appears because you are accessing arr[10] in ModifyArray.正如 Ron 评论的那样,出现问题是因为您正在访问 ModifyArray 中的 arr[10]。 Not because of reading it, but because you are updating the value.不是因为阅读它,而是因为您正在更新价值。 Remember that, as arrays are 0-based, an array of size 10 does not have an element at index 10, so the loop in ModifyArray goes too far away and ends up modifying the cookie.请记住,由于 arrays 是从 0 开始的,大小为 10 的数组在索引 10 处没有元素,因此 ModifyArray 中的循环走得太远,最终会修改 cookie。 In fact, note that you have a different loop condition in DisplayArray (which is the correct one).事实上,请注意您在 DisplayArray 中有不同的循环条件(这是正确的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM