PRB: strlen() Does Not Return Correct Value for 64K String

ID Number: Q73687

5.10 6.00 6.00a 6.00ax | 5.10 6.00 6.00a

MS-DOS | OS/2

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

Summary:

SYMPTOMS

The strlen() function is designed to return the length of a string.

The return value is of type size_t, which can be a value from zero

to 65535, but strlen() does not return the correct value for a 64K

string. When passed a string with a length of exactly 65535 bytes,

strlen() incorrectly returns 65534.

This problem occurs in both the function and intrinsic versions of

the strlen() function in the C run-time library provided with

Microsoft C versions 5.1, 6.0, 6.0a, and 6.0ax and QuickC versions

2.0, 2.01, 2.5, and 2.51. The sample code below demonstrates this

behavior.

RESOLUTION

If this causes problems with the data in your program, the only

workaround is to add a check to your code in the event that the

return from strlen() is 65534.

STATUS

Microsoft has confirmed this to be a problem in C versions 5.1,

6.0, 6.0a, and 6.0ax and QuickC versions 2.0, 2.01, 2.5, and 2.51

(buglist2.00, buglist2.01, buglist2.50, and buglist2.51). We are

researching this problem and will post new information here as it

becomes available.

More Information:

Sample Code

-----------

/* Compile options needed: /AL

*/

#include <string.h>

#include <stdio.h>

void main(void)

{

size_t length;

static char _far sz65535[65536];

memset(sz65535, 'A', 65535U);

sz65535[65535] = '\0';

length = strlen( sz65535);

printf("Actual Length of string is 65535,\n"

"strlen(sz65535) returned %u\n", length);

}

Additional reference words: 2.00 2.50 6.00 6.00a 6.00ax