PRB:gcvt Needs Larger Buffer than Documented to Avoid Overflow

ID Number: Q73515

5.10 6.00 6.00a 6.00ax | 5.10 6.00 6.00a

MS-DOS | OS/2

docerr

Summary:

The documentation for the gcvt() function in Microsoft C versions 5.1,

6.0, 6.0a, and 6.0ax fails to mention that in order to avoid buffer

overflow, the correct buffer size for converting a floating-point

value to a string may need to be 3 to 9 bytes larger than the number

of significant digits in the floating-point number.

More Information:

The documentation for the gcvt() function states the following:

The gcvt function converts a floating-point value to a character

string and stores the string in <buffer>. The buffer should be

large enough to accommodate the converted value plus a terminating

null character (\0), which is automatically appended. There is no

provision for overflow.

However, the converted value includes the number of significant digits

specified by the second parameter (<digits>), a decimal point, a

terminating null character, and possibly a sign byte. Therefore, the

minimum buffer size should be the number of significant digits plus 3.

In addition, if it is possible that the value may be returned in

exponential (E) notation, up to 6 more bytes (9 bytes total) should be

added to account for the additional characters used in this format.

Additional reference words: 6.00 6.00a 6.00ax