tech-misc archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

"stop and wait" scanf [was Re: Bug in TRE regular expression library]



> I never put any thought into this scanf arguments, simply because
> they serve a single purpose: Stop the application so I can read its
> output and press any key to quit quickly.  Works fine for me here.

It actually looks as though you meant to write "%*s" but got the * and
% switched.  %*s is not quite what you want either, since it will
silently consume whitespace, including newlines.  You might want %*c,
but that will consume only the next character, rather than up to the
next newline (which I suspect is what you want) - scanf("%*c") is
pretty much the same as getchar() in this use.

There is actually a syntax that is very close to what I think you want:
scanf("%*[^\n]").  The * suppresses assignment, so you don't have to
pass a pointer, and the format spec says to consume a string of
unlimited length made up of non-newlines.  However, it does not consume
the \n, so if you use it twice, the second call will return
immediately.  It might work to do scanf("%*[^\n]%*1[\n]") (the second
format specifier attempts to consume at most one newline), but I'd want
to test that before depending on it - I suspect that's a piece of scanf
that gets little testing and thus is likely to be buggy.

> I am sorry to say that I am not much concerned about any problems
> this key press might cause elsewehere, as this is not a working
> application but some simple code to demonstrate a problem somewhere
> else, possibly in production code.

Normally, I'd feel the same way.  But this looks like not a standalone
test program but a small part of a larger test scaffold, and if running
one test can corrupt things so as to cause a later test to fail
spuriously, that sounds to me like a real problem.

/~\ The ASCII                             Mouse
\ / Ribbon Campaign
 X  Against HTML                mouse%rodents-montreal.org@localhost
/ \ Email!           7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index