Abort trap: 6 in C when using strcpy on MacOS

2.3k views Asked by At

I am working on some code takes a string as input and returns it reversed.

When I input a string, I get an "abort trap: 6" error. I think the issue is in my use (misuse?) of strcpy, but GDB is not being helpful and other questions on SO about this error and strcpy have not helped me understand why I am getting this error.

I have added some comments to my code explaining the intended functionality.

Thank you for any help or reading material you can provide!

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

        int main()
        {
            char line[1024];
            fgets(line,sizeof(line),stdin);
            int size = strlen(line);
            for(int I = 0; I <size-I;I++)
            {
                char temp;
                int relative_size = size-I;
                strcpy(&temp,&line[I]);//??copies Ith character of line to temp??
                strcpy(&line[I],&line[relative_size]); //??swaps characters at [I] and [size-I]??
                strcpy(&line[relative_size],&temp); 
            }
            printf("%s", line);
            return 0;
        }
3

There are 3 answers

1
Amin Negm-Awad On

strcpy() does not copy a single character, but a whole 0-terminated string. Therefore strcpy(&temp, &line[i]); is a "buffer overflow" (temp isn't really a buffer), if line[I] isn't \0. To copy a single character, simply assign it:

temp = line[I];

This applies to the following two statements, too.

0
e.jahandar On

strcpy doesn't copy the characters alone. it copies strings. The string is number of characters followed by a \0 termination character.

 strcpy(&temp,&line[I]);

Above strcpy tries to copy an string starting from I th character until end (reaching to first \0) to address specified by &temp. because the temp is single char variable actually you're broking your stack frame and writing things to other variables accidentally.

You should use something like this to get I th character

temp = line[I];
0
Vlad from Moscow On

To reverse a string you need to swap pairs of characters of the string excluding the terminating zero.

The standard C function strcpy is designed to copy strings. For example if the variable line contains string "abcd" and the variable I is equal to zero then this statement

strcpy(&temp,&line[I]);

is equivalent to this statement

strcpy(&temp,&line[0]);

and tries to copy the whole string in the memory allocated for only one byte as it is seen from the declaration

char temp;

As result the memory outside the variable temp will be overwritten that in turn results in undefined behavior of the program.

So instead of using the function you should assign separate characters.

The loop could look the following way

size_t size = strlen(line);

for (size_t i = 0; i < size - i; i++)
{
    char temp = line[i];
    line[i] = line[size - i - 1];
    line[size - i - 1] = temp;
}

Take into account that the function fgets appends terminating zero to character arrays if they have enough memory. You should remove it from the string before reversing it. It can be done the following way.

fgets(line,sizeof(line),stdin);
line[ strcspn( line, "\n" ) ] = '\0';
size_t size = strlen(line);

Also the standard function strlen has return type size_t. So it is better to use this type for the variable size instead of the type int.