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

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:

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

Last Reviewed: July 18, 1997