Microsoft KB Archive/83084

{|
 * width="100%"|

BUG: sscanf Fails If String Is Longer Than 32K

 * }

Q83084

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a | 1.00 1.50 MS-DOS | OS/2 | WINDOWS kbprg kbbuglist -- The information in this article applies to: - The C Run-time (CRT), included with: - Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0ax - Microsoft C for OS/2, versions 5.1, 6.0, and 6.0a - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0 and 1.5 -- SYMPTOMS ======== Passing a string buffer longer than 32K+2 to sscanf will cause sscanf to return -1. It doesn't matter what is being read from the buffer. CAUSE ===== The sscanf function is implemented in such a way that its buffer has the same restrictions as a file-stream buffer. Because file streams cannot have a buffer larger than 32K, sscanf cannot accept a buffer larger than 32K. RESOLUTION ========== You can work around this problem by placing a &quot;\0&quot; character in the string buffer within the first 32K so that sscanf sees a string buffer shorter than 32K. If you are reading the data from a file, you can work around this problem by using fscanf and scanning the data directly from the file you are reading from. STATUS ====== Microsoft has confirmed this to be a problem in the products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available. This is not a problem in Visual C++ 32-bit Edition. MORE INFORMATION ================ The following code sample reproduces the problem: Sample Code --- /* Compile options needed: none */ #include #include int func ( unsigned bufsize ); static char buffer[33000]; FILE *fptr; void main { int result; unsigned bufsize = 32768U; while (((result = func (bufsize) ) >= 0) && (bufsize < 33000U)) bufsize++; } int func ( unsigned bufsize ) { int result; char data; memset( buffer, 'A', bufsize ); buffer[bufsize] = '\0'; if (( result = sscanf( buffer, &quot; %c&quot;, &data )) != 1) printf( &quot;\nSSCANF error\n&quot; ); printf( &quot;bufsize is %u : sscanf returned %d\n&quot;, bufsize, result ); return result; } Additional reference words: 1.00 1.50 5.10 6.00 6.00a 6.00ax 7.00 KBCategory: kbprg kbbuglist KBSubcategory: CRTIss

Keywords : kb16bitonly

Issue type :

Technology : kbVCsearch kbAudDeveloper kbCRT