Buffer Over Flow with string Arrays

This is a discussion on Buffer Over Flow with string Arrays within the C Programming forums, part of the General Programming Boards category; Hello friends, Iam implementing my own strcpy function following is the code. Code: #include <stdio.h> void myStrCpy(char *pDstStr,char *pSrcStr); void ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    113

    Buffer Over Flow with string Arrays

    Hello friends,

    Iam implementing my own strcpy function

    following is the code.

    Code:
    #include <stdio.h>
    
    void myStrCpy(char *pDstStr,char *pSrcStr);
    void myStrCpyTD();
    
    void myStrCpyTD()
    {
            char str[100];
            char tempStr[100];
            char *p;
    
            memset(str,sizeof(str),0);
            memset(tempStr,sizeof(tempStr),0);
    
            fflush(stdin);
    
            while(fgets(str,sizeof(str),stdin) != NULL)
                                            ;
    
            if ((p = strchr(str,'\n')) != NULL) ;
                            *p = '\0';
    
            myStrCpy(tempStr,str);
            printf("copied for dst from src and Dst is %s \n",tempStr);
    
    }
    
    
    void myStrCpy(char *pDstStr,char *pSrcStr)
    {
            while (*pSrcStr != '\0')
            {
                    *pDstStr = *pSrcStr;
                    pSrcStr++;
                    pDstStr++;
            }//while
            pDstStr = '\0';
            printf("%s",pDstStr);
    }
    
    
    
    int main()
    {
            int choice = 0;
    
            do
            {
                    printf("3.Copy form Src to Dst\n");
                    printf("6.To Exit \n");
                    scanf("%d",&choice);
    
                    switch(choice)
                    {
                            case 3:
                                    myStrCpyTD();
                                    break;
                            case 6:
                                    printf("Coming out the program \n");
                                    break;
                            default:
                                    printf("Please choose the valid option \n");
                                    break;
                    }//switch
    
            }while (choice != 6); //while
    
            return 0;
    }
    Output

    # ./ex6.o
    3.Copy form Src to Dst
    6.To Exit
    3
    John
    (null)copied for dst from src and Dst is John¡,î¿
    3.Copy form Src to Dst
    6.To Exit
    6
    Coming out the program



    1) The bug iam seeing is while printing the tempStr it contains some other junk characters
    also
    2) why cannot i print the the string with pDstStr in the myStrCpy function.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    1) The bug iam seeing is while printing the tempStr it contains some other junk characters
    You assign '\0' to the pointer instead of the character pointed to.

    2) why cannot i print the the string with pDstStr in the myStrCpy function.
    The pointer does not point to the start of the string by the time you print. (And also see #1.) In fact, there is no reason for you to print the string in myStrCpy(). Refer to what strcpy() does. Your functions should do one thing and do it well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  5. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21