tags:

views:

384

answers:

9

I've tried reinventing the strcpy C function, but when I try to run it I get this error:

Unhandled exception at 0x00411506 in brainf%ck.exe: 0xC0000005: Access violation writing location 0x00415760.

The error occurs in the *dest = *src; line. Here's the code:

char* strcpy(char* dest, const char* src) {
    char* dest2 = dest;
    while (*src) {
        *dest = *src;
        src++;
        dest++;
    }
    *dest = '\0';
    return dest2;
}

EDIT: Wow, that was fast. Here's the calling code (strcpy is defined in mystring.c):

#include "mystring.h"
#include <stdio.h>

int main() {
    char* s = "hello";
    char* t = "abc";
    printf("%s", strcpy(s, t));
    getchar();
    return 0;
}
+2  A: 

What's doing the allocation for dest before it's passed into your strcpy? Odds are that's the problem.

Joe
No that does not matter. It's as Michael has written. The stuf was places in read-only sections. You can change that in gcc AFAIKT, but I think changing that is a really poor idea
Friedrich
+7  A: 

The obvious potential problem is that your output buffer doesn't have enough memory allocated, or you've passed in NULL for dest. (Probably not for src or it would have failed on the line before.)

Please give a short but complete program to reproduce the problem, and we can check...

Here's an example which goes bang for me on Windows:

#include <stdlib.h>

char* strcpy(char* dest, const char* src) {
    char* dest2 = dest;
    while (*src) {
        *dest = *src;
        src++;
        dest++;
    }
    *dest = '\0';
    return dest2;
}

void main() {
    char *d = malloc(3);
    strcpy(d, "hello there this is a longish string");
}

Note that in this case I had to exceed the actual allocated memory by a fair amount before I could provoke the program to die - just "hello" didn't crash, although it certainly could depending on various aspects of the compiler and execution environment.

Jon Skeet
Why in the name of all that compiles are you returning dest2? The calling code doesn't care about a return value and the "input" parameter "d" is left high and dry.
Craig
I was just copying the code from the OP...
Jon Skeet
The exception text says it's a write-access exception to an address that is close to the code address - so dest is a global or a constant in the executable
Michael
@Craig: `strcpy` should return a copy of its first parameter, it's what the language standard requires.
Charles Bailey
A: 

There's nothing to stop strcpy() writing off the end of dest and that's what the crash you're seeing is telling you about.

Graham Lee
A: 

Make sure dest has it's memory allocated before calling that function.

John Boker
A: 

Probably an issue with the caller: did you check the dest pointer? Does it point to something valid or just garbage? Besides that, the least you could do is check for null pointers, like if (!dest || !source) { /* do something, like return NULL or throw an exception */ } on function entry. The code looks OK. Not very safe, but OK.

Axel Rietschin
A: 

There are several errors.

  1. You don't allocate a return buffer that can hold the copied string.
  2. You don't check to see if src is null before using *src
  3. You are both tring to get the answer in a parameter and return the value. Do one or the other.
  4. You can easily overrun the dest buffer.

Good luck.

Craig
Not one of these 'errors' is applicable to the OP's code.
hughdbrown
+12  A: 
char* s = "hello";
char* t = "abc";
printf("%s", strcpy(s, t));

The compiler placed your destination buffer, s, in read-only memory since it is a constant.

char s[5];
char* t = "abc";
printf("%s", strcpy(s, t));

Should fix this problem. This allocates the destination array on the stack, which is writable.

Michael
Thank you. (15 characters)
Javier Badia
+3  A: 

Your strcpy() is fine. You are writing to read-only memory. See this description here.

If you had written this, you'd be fine:

#include "mystring.h"
#include <stdio.h>

int main() {
    char s[] = "hello";
    char t[] = "abc";
    printf("%s", strcpy(s, t));
    getchar();
    return 0;
}
hughdbrown
+1  A: 

There is a problem with calling of your reinvented strcpy routine in the main routine, both character array: char* s = "hello"; char* t = "abc"; will land into memory READ ONLY segment at compile time. As you're trying to write to memory pointed by s in the routine strcpy, and since it points to a location in a READ ONLY segment, it will be caught, and you'll get an exception. These strings are READ ONLY!