Invalid memory writes #2

Closed
erjiang opened this Issue Nov 6, 2011 · 1 comment

Comments

Projects
None yet
2 participants
@erjiang
Contributor

erjiang commented Nov 6, 2011

At MyBot.c:109

    char *data = malloc(initial_buffer);
    memset(data, 0, initial_buffer);

    *data = '\n';

    char *ins_data = data + 1;

    int i = 0;

    while (1 > 0) {
        ++i;

        if (i > initial_buffer) {
            initial_buffer *= 2;
            data = realloc(data, initial_buffer);
            memset(ins_data, 0, initial_buffer/2);
        }

        *ins_data = getchar();

You realloc data but you never update ins_data to point to the new data! If it ever outgrows its initial buffer, it will probably keep writing memory it shouldn't be writing to. Also, since data is only realloc'd when i is strictly greater than initial_buffer, it will write past the end of the buffer by one byte.

@flagcapper

This comment has been minimized.

Show comment
Hide comment
@flagcapper

flagcapper Nov 6, 2011

Owner

Thanks for the heads up! This should be fixed now.

Owner

flagcapper commented Nov 6, 2011

Thanks for the heads up! This should be fixed now.

@flagcapper flagcapper closed this Nov 6, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment