Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

c - 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

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] != '')
    {
        k = strcspn(temp, ".!?;");
        length = strlen(temp);
        temp[k] = '';
        *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, "
");
        while(temp[k + 1] == ' ')
            for(i = k;i < strlen(temp);i++)
                temp[i] == temp[i + 1];
        if(!(strcmp(*(*s + *n_s - 1), "
")))
        {
            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
", num_sent);
    for(i = 0;i < num_sent;i++)
        printf("%s
", sentences[i]);

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...