cin >> unsigned --> program hang

Hey, I was playing with input the other day with my GCC 4.7, and found the following to produce an unexpected HANG when a negative number was provided at input:

1
2
unsigned n;
cin >> n;

(I validated that the problem occurred with stringstream as well, but did not play with a filestream.)

I considered this to be a serious security issue, and was concerned that it happened at all. I mean, I would have presumed that the standard library code would be able to, at the very least, return a nonsense value and leave cin in a fail state. But instead the program just hung, and the standard MS "searching for solution" dialogue came up.


And now that I want to play with it some more, I cannot reproduce the error.


In (supposedly) unrelated news, I am finally happy that I got Microsoft's compiler installed on my machine! (For years I couldn't, because my XP system was one of the few that, for reasons MS couldn't (or wouldn't) explain, refused to successfully install .NET 3.5 -- a known bug for some small number of XP systems). In any case, now that I'm running Windows 7 I've got VS happily installed.

I don't know how MinGW does string-to-integer conversions -- if it is with some MS lib then I may have inadvertently updated it with my fresh VS 11 install...


In any case, has anyone else had this issue? Is this something I should worry about in the FAQ dealing with converting strings to integers? (Remember, I'm being pragmatic with the FAQ, and not idealistic. For example, I'll not claim that <regex> works nicely.)
If you can't reproduce the error it was probably not what was wrong in the first place. Probably something else in the original program caused the program to hang.
I'm not a newb.

The original program:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
  {
  unsigned n;

  cout << "n? ";
  cin >> n;
  cout << "n = " << n << endl;

  return 0;
  }
I use MinGW, GCC 4.7 (but with Code::Blocks as my IDE), and your program converted a negative int input to its equivalent unsigned value. cin didn't go into a fail state nor did I get an error you described.

Maybe it was a hiccup from installing a new compiler? Like having to run a new engine for a few minutes to get the oil flowing? I don't have a clue.
Topic archived. No new replies allowed.