BUG: sscanf() Fails If String Is Longer Than 32K

Last reviewed: July 17, 1997
Article ID: 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 "\0" 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 <stdio.h>
#include <memory.h>

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, " %c", &data )) != 1)
      printf( "\nSSCANF error\n" );

   printf( "bufsize is %u : sscanf returned %d\n", 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


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.