Microsoft KB Archive/11581

Pascal 3.32: Encode Does Not Detect Overflowed Lstrings PSS ID Number: Q11581 Article last modified on 03-01-1991 PSS database name: S_PasCal

3.3x

MS-DOS

Problem:

In Microsoft Pascal version 3.32, encode does not return false if an lstring is too small to hold the string generated. The resultant lstring is expanded beyond the maximum size allocated, and adjacent memory contents are destroyed.

The following code demonstrates the problem:

Code Example: ENCODE.PAS
program test(input,output);

var NEXTLSTR:  lstring(5); LSTR:      lstring(5); REALNUM:   real; FR:        boolean;

begin readln(realnum); NEXTLSTR:='next'; while realnum<> 0 do begin FR:=encode(lstr,realnum:-5:1); writeln(fr); writeln(realnum); writeln(lstr.len); writeln(lstr); writeln(nextlstr); readln(realnum); end; end. Response:

This problem was corrected in Pascal version 4.00. Encode returns false if target lstring is not long enough, though lstring is still being expanded without apparent harm to subsequently defined data. In Pascal version 3.32, the lstring was expanded, data declared below the lstring was over-written, and encode returned true anyway.

The encode function is designed to expand the target string if necessary. You must declare a string or lstring of adequate length to contain all the characters of the encoded string.

Copyright Microsoft Corporation 1991.