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

ID Number: Q83084

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

buglist5.10 buglist6.00 buglist6.00a buglist6.00ax buglist7.00

Summary:

PROBLEM ID: C9204001

SYMPTOMS

Passing a string buffer longer than 32K+2 to sscanf() will cause

sscanf() to return -1. It doesn't matter what you are trying to

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 C versions 5.1,

6.0, 6.0a, 6.0ax, C/C++ 7.0, QuickC versions 2.0, 2.01, 2.5, 2.51, and

QC/Win version 1.0. We are researching this problem

and will post new information here as it becomes available.

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 2.00 2.50 5.10 6.00 6.00a 6.00ax 7.00