0

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. 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]);
    }
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • You are observing the effects of [undefined behavior](https://en.cppreference.com/w/c/language/ub). You are invoking undefined behavior by reading out of array bounds with `arr[10]` and by accessing an uninitialized local variable `i`. There are other errors, such as `for (i = 0; i <= asize; i++);`, the lack of function declarations and more. A good [C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) might be in order. – Ron Mar 21 '21 at 21:23

1 Answers1

0

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). If your code had behaved correctly, the cookie would not have been modified and you would not have run into this error.

As Ron commented, the problem appears because you are accessing arr[10] in ModifyArray. 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. In fact, note that you have a different loop condition in DisplayArray (which is the correct one).

Ion Larrañaga
  • 464
  • 2
  • 5