Thread: Buffer Over Flow with string Arrays

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

    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09: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