flaw found [in my own program]

Jeffrey Goldberg jeffrey at goldmark.org
Tue Jun 9 23:19:24 UTC 2009

On Jun 8, 2009, at 7:15 PM, Gary Kline wrote:

> 	not surprisingly, i found a fla w in my getc(fp) program that
> 	tried to read past "<?" and "?>" ...  the example i added to my
> 	test file was simply the 2 bytes "<" and "?".  so if you have a
> 	stray
> 	"<?"
> 	with a matching close case, the binary hangs on a read.
> 	so, again, can anybody suggest a better example, in C, to get
> 	past two delimiters?

Back in the days when I taught introductory C programming, one the the  
early homework assignments was to write a filter that would strip C- 
style comments.  As a follow-up they had to do this allowing for  
nested comments.

I don't think I can recover things from the back-up tapes that I have  
for that corse material, but the approach I directed people toward was  
to have a variable, let's call it status that records one of four states

  OUTSIDE  /* just reading normally, not in the material to be striped  
  AFTER_LT /* You've read in a '<' and are looking for a '?' */
  INSIDE   /* You are in the material to be stripped */
  AFTER_Q  /* You are in the material to be stripped and have just  
read a '?' */

then use a switch statement on the character you are reading in.

        switch(c) {
          case '<': ...
          case '?': ...
          case '>': ...
          case EOF: ...
          default: ...

In each case, you look at the current state, decide whether the write  
'c' to output and what state to change to.  The most common mistake  
students would make would be to forget the EOF case.  I suspect that  
you may have done the same.


More information about the freebsd-questions mailing list