2

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;
}
Javier
  • 4,552
  • 7
  • 36
  • 46
  • 3
    How are you calling your implementation? That code would be helpful. – Jason Aug 13 '09 at 21:18
  • 1
    Man, that was a bunch of quick answers. Welcome to the world of overrun buffers. :) – Craig Aug 13 '09 at 21:22
  • I had thought about just putting that, but I wanted to do something a bit more clear. However, I'll probably end up using it. It would be nice what I'm doing wrong with my current code anyway. – Javier Aug 13 '09 at 21:31
  • Same as http://stackoverflow.com/questions/164194 and countless others. – ephemient Aug 13 '09 at 21:37
  • If you're going to copy into some memory, you need to *allocate* some memory. – David Schwartz Aug 28 '11 at 15:13
  • What's doing the allocation for dest before it's passed into your strcpy? Odds are that's the problem. – Joe Aug 13 '09 at 21:18

8 Answers8

18
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
  • 54,279
  • 5
  • 125
  • 144
7

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    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 Aug 13 '09 at 21:28
  • 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 Aug 13 '09 at 21:34
  • 1
    @Craig: `strcpy` should return a copy of its first parameter, it's what the language standard requires. – CB Bailey Aug 13 '09 at 21:40
3

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
  • 47,733
  • 20
  • 85
  • 108
1

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!

0

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

John Boker
  • 82,559
  • 17
  • 97
  • 130
0

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
  • 611
  • 1
  • 7
  • 10
0

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
  • 11,614
  • 13
  • 44
  • 62
0

when ever the code starting execution(generaly it starts from main function). here the code means sequence of execution.so, when the process(sequence of execution) starts , the PCB(process control block) is created,the pcb having complete infromation about the process like process address space,kernal stack,ofdt table like this.

in your code

char* s = "hello";
char* t = "abc";

this is the what you have taken inputs of two strings like this.

here, the the strings(which means double quoted) which are present in text section of the process address space . here text section is the one of the section which is present in the process address space and text section only having the permissions of read-only. that is why when you trying to modify the source string/destination string, we MUST NOT allowable to change the whatever data is present in the text setion. so, this is what the reason for your code you need to be CAUTIOUS. hope you understand.

prashad
  • 107
  • 2
  • 15