'Stack smashing detected error on function return

I'm making a project for a class in C [ANSI-90]

for some reason, I get a stack smashing error, I know it's gcc protection for buffer overflow, or accessing a memory that shouldn't be accessed as far as I've understood.

but I can't find why. i found out that it aborts once i return from a function that is supposed to parse only the first word of input into a command using strtok this is what i got:

    int getCommand(char *p, commands *cmd)
{
    int i = 0;
    char lineCopy[MAX_LINE_LEN];
    char *cmdName;
    strcpy(lineCopy, p);
    cmdName = strtok(lineCopy, "\t \n");
    if (cmdName != NULL)
    {
        while (cmd[i].func != NULL)
        {
            printf("%d",i);
            if (strcmp(cmdName, cmd[i].name) == 0)
                return i;
            ++i;
        }
    }
    i=-1;
    puts("return error"); /* Just to check I'm reaching here, and i do */
    return i; /* ERROR */
}

input example to recreate:

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

i will be happy if you could point and explain what am i doing wrong?

c


Solution 1:[1]

As @Alexander pointed out, I didn't allocate memory for linecopy, fixed with:

char *lineCopy = malloc(strlen(p));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lee Taylor