Thread: Simple Buffer Overflow Question

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    10

    Question Simple Buffer Overflow Question

    Hello All,

    I'm brand new to C++ and have run into a problem when my program asks for a character to be input (like a menu choice), I'm allowed to type in as much or as little as i want while the program is waiting for me to hit 'enter'.

    Checking for more than one input character was'nt a requirement of the assignment, but I was wondering how this is typically handled in C++ with regards to user input using CIN.

    Not asking for code, but maybe someone can point me in the right direction.

    Thanks in advance,

    BH

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Code:
    #include <iostream.h>
    #define BUFF_LEN 512
    
    int main(void)
    {
      char Buff[BUFF_LEN];
      int Len;
      
      cin.getline(Buff, BUFF_LEN);
      Len = strlen(Buff);
    
      /*if(Len > ??)
         {
            //do whatever
         }  
         else
         {
            //something
         }  
      */
    
      return 0;
    }
    Haven't compiled this, but should be close to what you want.



    [/code]
    Be a leader and not a follower.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If your using a char, then it can handle only one character, so most of the input (the extra characters) is discarded. If you have a character array, then it is handled often as in the example above. If you have a string class, or a char*, then cin will reallocate enough memory (if possible) to give you all the input.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    char p[100];
    cin.width(100); //Max 99 + '\0'
    cin >> p;
    >>or a char*, then cin will reallocate enough memory (if possible) to give you all the input.

    Code:
    char *p = new char[10];
    cin >> p; //NO reallocation if more than 10 chars -> buffer overflow
    Last edited by Wledge; 06-13-2003 at 10:34 AM.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Not quite what I was getting at, but good point.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If your using a char, then it can handle only one character, so most of the input (the extra characters) is discarded.

    cin will only read in one char for a char type, but how is "most" of the rest of the input discarded?

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If you type in more than one character, only one is actually input. The rest, though showing up on the screen is not actually input, and hence, discarded.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    Hello,

    First off, thanks for all the replies and help. This is actually a very basic program (have'nt hit DEFINE's yet o.O), and I am using a char variable to store a menu choice.

    It seems that when I input say a valid menu choice(r-s-c) with an extra character tagged on the end

    Like: r-r-'enter'
    The program goes into a loop for some reason with numbers spewing all over.

    If I enter an invalid menu choice with another character tagged on the end

    Like x-r-'enter'
    The program goes into a loop when I try to enter a valid menu choice the 2nd time menu is displayed.

    I tested with the following:

    #include <iostream.h>

    main()
    {

    char c[10]; char d[10];

    cout << "Enter a string, only 4 characters will be saved: ";
    cin.width(5); //Define string width for input including NULL
    cin >> c ;

    /*Truncated characters from input above seem to get pushed to the next CIN statement */

    cin.width(10); //Stores up to 9 pushed characters
    cin >> d;

    cout << "String as saved: " << c << endl;
    cout << "Overflowing Input: " << d << endl;

    system("PAUSE");

    return 0;
    }

    Which shows that extra input is definitely not truncated when you hit 'enter.

    Sorry for my lack of knowledge and the long post, but im learning

    BH

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Its not truncated... its just left on the buffer. Call cin.sync(); to resynchronize the input stream.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    That was exactly what I was looking for! Thanks for everyone's help. VS.Net help is a nightmare =-\.


    BH

    "I like a man who grins when he fights."
    - Winston Churchill

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "The rest, though showing up on the screen is not actually input, and hence, discarded."

    Discarded? "Most" of the rest of the input isn't discarded, and in fact "none" of the rest of the input is discarded.

    You might want to try this code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    { 
    	char ch;
    		
    	cout<<"Enter the letters abc:\n";
    	cin>>ch;
    
    	char more;
    	cout<<"Type y to continue, n to quit:\n";
    	cin>>more;
    
    	if(more=='y')
    		cout<<"It looks like you chose to continue.\n";
    	else if(more=='n')
    		cout<<"It looks like you chose to quit.\n";
    	else
    		cout<<"What happened?\n";	
      return 0;
    }
    Last edited by 7stud; 06-13-2003 at 02:27 PM.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by Black-Hearted
    That was exactly what I was looking for! Thanks for everyone's help. VS.Net help is a nightmare =-\.


    BH

    "I like a man who grins when he fights."
    - Winston Churchill
    Why are you using <iostream.h> with VC .NET? .NET is new enough to support the correct <iostream> headers. In fact, .NET 2003 doesn't even HAVE iostream.h.

    Use the correct standard headers, not the no-longer-standard headers from the 90's.

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    Agreed, I found out the same by messing around with how the "extra" was stored. Just call cin.sync() after your 1st cin as Zach said and your good to go. Seems kind of weird that one 'Enter' can get you through X number of CIN statements, assuming you have an overflow.

    thanks again all

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    Cat,

    Sorry for the confusion, I have VS.net installed, but it is a pain in the butt for the little C++ programs I have to write for now. Plus the book we are using for the class was written in 2001, and does'nt know much about 2003 VS.net. For now, I'm writing with Dev C++. But use VS for help sometimes.

    Nice catch,

    BH
    Last edited by Black-Hearted; 06-13-2003 at 02:51 PM.

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Dev C++ isn't ANSI standard??? I find it hard to believe they haven't come out with a version of Dev C++ that meets the 1998 standards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on buffer overflows
    By maxhavoc in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2004, 03:48 PM
  2. Quick C Question - checking buffer for input
    By sean in forum C Programming
    Replies: 3
    Last Post: 11-13-2004, 12:23 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Buffer overflow issue.
    By caroundw5h in forum C Programming
    Replies: 3
    Last Post: 12-27-2003, 12:13 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM