Xref: news2.mv.net comp.os.msdos.djgpp:577 Newsgroups: comp.os.msdos.djgpp From: mambuhl AT ripco DOT com (Martin Ambuhl) Subject: Re: a bug in sscanf? Message-ID: Sender: usenet AT rci DOT ripco DOT com (Net News Admin) Organization: Ripco Internet BBS Chicago Date: Tue, 23 Jan 1996 07:21:38 GMT Lines: 61 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp "Alexander V. Lukyanov" in wrote: >I've found some strangeness in sscanf which I suspect to be a bug. >I'm using v2beta4. I think that you are wrong. The key here is that (7.9.6.6) "Reaching the end of the string is equivalent to encountering end-of-file for the fscanf function." If you change your input string to "abc123 ", you will find the behavior is what you expect. Notice that the EOF on "abc123" is encountered before the attempt to use "%n", and (7.9.6.2) "If end-of-file is encountered during input, conversion is terminated. If end-of-file occurs before any characters matching the current directive have been read (other than leading white space, where permitted), execution of the current directive terminates with an input failure; otherwise, unless execution of the current directive is terminated with a matching failure, execution of the following directive (if any) is terminated with an input failure." The fact that %n does not consume any input is irrelevant here. >Here is a simple test program: >---8<--- >#include >int main() >{ > char name[256]; > int ch_read; > printf("sscanf=%d\n",sscanf("abc123","%255[a-zA-Z0-9]%n",name,&ch_read)); > printf("ch_read=%d\n",ch_read); > printf("name=%s\n",name); > return 0; >} >---8<--- >It outputs: >---8<--- >sscanf=1 >ch_read=-1058078715 >name=abc123 >---8<--- >That is ch_read is uninitialized and sscanf succseeded! :-( >I think it is because a bug in doscan.c. It checks 'fileended' variable >and immediately exits. But in the format string next token is '%n' which does >not require more input. >This test works correctly on Solaris2.4. >--- >Alexander Lukyanov >lav AT video DOT yars DOT free DOT net -- * Martin Ambuhl net: mambuhl AT ripco DOT com * Chicago, IL (USA)