0

I'm creating a program on DOS using the FreeRTOS kernel which allows me to render multiple windows onto the screen that contains its own text user interface. The issue is that I've encountered an overflow error which was caused by feeding more than 256 characters into the buffer. Is there a way to work around this?

A portion of my code:

int default_background=0;
int default_foreground=15;

struct window {                        /* Window structure */
    int special_id;
    int cursor_x,cursor_y;
    int width,height;
    long *background,*foreground;
    char *buffer;
};

long window_index_count=0;
struct window current_windows[10];

long create_window(int width,int height) {
    int i;
    long t;
    struct window new_window;
    new_window.special_id=window_index_count;
    window_index_count=window_index_count+1;
    new_window.cursor_x=0;
    new_window.cursor_y=0;
    new_window.width=width;
    new_window.height=height;
    for (t=0; t<width*height; t++) {
        new_window.background[t]=default_background;
        new_window.foreground[t]=default_foreground;
        new_window.buffer[t]=' ';      /* This is where the error occurs */
    }
    for (i=0; i<10; i++) {
        if (current_windows[i].special_id<=0) {
            current_windows[i]=new_window;
            break;
        }
    }
    return new_window.special_id;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Jason Lee
  • 83
  • 1
  • 1
  • 5

1 Answers1

2

You don't actually allocate memory for the buffer. Since local non-static variables are by default uninitialized, their values are indeterminate. So when you use the pointer new_window.buffer you have no idea where it points to, leading to undefined behavior.

It's the same with the other pointers in the structure as well.

The solution is to actually allocate memory for the pointers to point to.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621