Search code examples
cbuffer-overflow

A variable is being overwritten for no apparent reason


I am writing a scheduler in C, and in order to print it I have prepared a function that prints a table with names and times of "events". In order to print the tables I have a separation after each line, which is basically "|=======|========|=======|.....", so I saved it in a variable and tried using it to print. And for some reason I overflow it with values.

I tried finding out where the error is, and I've tried decreasing the amount of characters added to a string that is defined after it(to prevent the overflow), but those didn't work

char line_break[] = "|====================|====================|====================|====================|====================|====================|====================|";
printf("\n|       Sunday       |       Monday       |       Teusday      |      Wedensday     |      Thursday      |       Friday       |      Saturday      |\n%s\n",line_break);

for (int i=0; i < max(schedule_index);i++) {
    char events[8+EVENT_NAME_SIZE*7] = {0};
    char  hours[8+EVENT_NAME_SIZE*7] = {0};
    strcat(events, "|");
    strcat(hours,  "|");
    for (int day=0; day < DAYS_PER_WEEK; day++) {
        char temp_events[EVENT_NAME_SIZE]= {0};
        char temp_hours[EVENT_NAME_SIZE] = {0};
        for (int i=0; i<EVENT_NAME_SIZE;i++) {
            temp_events[i] = ' ';
            temp_hours[i] = 'b';
        }

        strcat(events, temp_events);
        strcat(events, "|");

        strcat(hours, temp_hours);
        strcat(hours, "|");
    }

    printf("%s\n", events);
    printf("%s\n", hours);
    printf("%s\n", line_break);
}

Whenever I execute the code I get the following:

|       Sunday       |       Monday       |       Teusday      |      Wedensday     |      Thursday      |       Friday       |      Saturday      |
|====================|====================|====================|====================|====================|====================|====================|
|                    |                    |                    |                    |                    |                    |                    |
|                    |                    |                    |                    |                    |                    |                    |
|bbbbbbbbbbbbbbbbbbbb|bbbbbbbbbbbbbbbbbbbb|bbbbbbbbbbbbbbbbbbbb|bbbbbbbbbbbbbbbbbbbb|bbbbbbbbbbbbbbbbbbbb|bbbbbbbbbbbbbbbbbbbb|bbbbbbbbbbbbbbbbbbbb|
bbbbbbbbbbbbbbbbb|

So that's how I figured out the problem is the line_break variable is being overridden by the hours variable but I still have no clue as to why it's happening. EVENT_NAME_SIZE is declared to be 20,DAYS_PER_WEEK is also 7, max(schedule_index) is just for me to find out what is the max amount of hours I have to draw,


Solution

  • All your array declarations exclude space for string terminating zero byte.

    It should be:

    char events[8+EVENT_NAME_SIZE*7+1] = {0};
    char  hours[8+EVENT_NAME_SIZE*7+1] = {0};
    

    where you want to hold EVENT_NAME_SIZE spaces or b characters and 8 | characters.

    and:

        char temp_events[EVENT_NAME_SIZE+1]= {0};
        char temp_hours[EVENT_NAME_SIZE+1] = {0};
    

    If you want to store EVENT_NAME_SIZE characters in a string, you need EVENT_NAME_SIZE bytes of space plus one byte for terminating zero character.

    Functions like strcat need zero terminated arrays. Also programmer need to make sure the destination have enough space available. The behavior of the first strcat(events, temp_events); is undefined - temp_events is not null terminated, so strcat was accessing some random data of memory.