Code works fine on my system but causes a stack smashing error when i submit it to the bot that is supposed to check it

拈花ヽ惹草 提交于 2020-01-26 04:11:07

问题


This program at this stage is supposed to get text from the user and separate it into paragraphs, sentences and words. The code works fine so far on my system(ubuntu 18.04) but when i submit it to the bot that is supposed to give input to it, a stack smashin error comes up. Is there a way to make my code read input without crashing?

Edit: Here's a test input. I think the problem is that it reads it all at once.(also there are some other options apart from ap: that i haven't made yet):

Test 3 Input: ap:At the center of the novel is the typical Graham Greene character.[
]fw:h[
]fs:ovel[ ]fp:typical[
]owf[
]owl[
]ap:He is tired and skeptical, basically decent yet cynical. One senses
that life has no real colors to him, in fact it bores him, yet there is an underlying hope of some redemption, of some sense of meaning.[
]fw:or[
]fw:is[
] owf[
]owl[
]qt

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

void ap(char ***p, char ***s, char ***w, int *n_p, int *n_s, int *n_w)
{
    char *temp = malloc(10001 * sizeof(char));
    int i, k, j, length;

    if(temp == NULL)
    {
        printf("Could not allocate memory");
        return;
    }
    fgets(temp, 10001, stdin);
    while(temp[0] == ' ')
        for(i = 0;i < strlen(temp);i++)
            temp[i] = temp[i + 1];

    //paragraphs
    *n_p += 1;
    **p = realloc(**p, *n_p * sizeof(char *));
    *(*p + *n_p - 1) = malloc((strlen(temp) + 1) * sizeof(char));
    if(**p == NULL || *(*p + *n_p - 1) == NULL)
    {
        printf("Could not allocate memory");
        return;
    }
    strcpy(*(*p + *n_p - 1), temp);

    //sentences
    while(temp[0] != '\0')
    {
        k = strcspn(temp, ".!?;");
        length = strlen(temp);
        temp[k] = '\0';
        *n_s += 1;
        **s = realloc(**s, *n_s * sizeof(char *));
        *(*s + *n_s - 1) = malloc((strlen(temp) + 1) * sizeof(char));
        if(**s == NULL || *(*s + *n_s - 1) == NULL)
        {
            printf("Could not allocate memory");
            return;
        }
        strcpy(*(*s + *n_s - 1), temp);
        j = 0;
        for(i = k + 1;j <= length - k;i++)
        {
            temp[j] = temp[i];
            j++;
        }
        while(temp[0] == ' ')
            for(i = 0;i < strlen(temp);i++)
                temp[i] = temp[i + 1];
        k = strcspn(temp, "\n");
        while(temp[k + 1] == ' ')
            for(i = k;i < strlen(temp);i++)
                temp[i] == temp[i + 1];
        if(!(strcmp(*(*s + *n_s - 1), "\n")))
        {
            free(*(*s + *n_s - 1));
            *n_s -= 1;
        }
     }
}

int main()
{
    char **paragraphs, **sentences, **words, option[5];
    int num_par = 0, num_sent = 0, num_words = 0, i;

    paragraphs = malloc(sizeof(char *));
    sentences = malloc(sizeof(char *));
    words = malloc(sizeof(char *));
    if(paragraphs == NULL || sentences == NULL || words == NULL)
    {
        printf("Could not allocate memory");
        return 1;
    }

    do
    {
        scanf("%s", option);
        if(!(strcmp(option, "ap:")))
            ap(&paragraphs, &sentences, &words, &num_par, &num_sent, &num_words);
    }
    while(strcmp(option, "qt"));

    //test
    for(i = 0;i < num_par;i++)
        printf("%s", paragraphs[i]);
    printf("-------------  %d\n", num_sent);
    for(i = 0;i < num_sent;i++)
        printf("%s\n", sentences[i]);

    return 0;
}

回答1:


option is too small, the line:

scanf("%s", option);

Tries to read each line into a 5 character array. Lines that are more than 5 characters overflow the array and gcc catches it with a canary and kills you for stack smashing.

It might run if your local compiler doesn't implement stack smashing detection.

Increase char option[x] to some reasonable value of x for your line lengths.



来源:https://stackoverflow.com/questions/59509457/code-works-fine-on-my-system-but-causes-a-stack-smashing-error-when-i-submit-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!