sigurasg@rhi.hi.is (Sigurdur Asgeirsson) (06/18/91)
A friend of mine is starting to program XCMD's and wanted me to show him how to concat two parameters into the result, so I wrote something like: type charArr = packed array [0..32767]; charPtr = ^charArr; charHndl = ^charPtr; ... Procedure Main(usual stuff) var len1, len2: integer; result: charHndl; begin len1 = GetHandleSize(param1) - 1; len2 = GetHandleSize(param2) - 1; result = charHndl(NewHandle(len1 + len2 + 1); ... {HLock'em all} BlockMove(param1^, result^, len1); BlockMove(param2^, @result^^[len1], len2); {So far so good, everything works till now (param n being parameter handle} {no. n from parameter block)} result^^[len1 + len2] := chr(0); {this compiles to an ADD.W D0,D7 or some such, and then CLR.B (A0, D0.L)} {instead of CLR.B (A0, D0.W), and some garbage in the high byte of D0, } {sending the zero byte to outer space (or inner space) corrupting the } {heap, crashing the mac, evoking a big laugh from my friend (who still } {doesn't believe that it's a compiler error).} ... {HUnlock'em all and put result into parameter block} end; The XCMD was compiled with range checking and overflow checking on, with Think Pascal 3.0.2 (upgraded from 3.0 I think). Has anyone seen this before or am I the first person in the world to be hit by this bug (or was it perhaps only a bad dream)? BTW: the code above was typed from memory, any errors result there from (the darned thing worked after I changed the D0.L to D0.W with TMON). -- sigurasg@rhi.hi.is | "Well you know, C isn't that hard, void (*(*f[])())() for Sigurdur Asgeirsson | instance defines f as an array of unspecified size, of | pointers to functions returning a pointer to a function | returning void... I think"
siegel@world.std.com (Rich Siegel) (06/19/91)
In article <3264@krafla.rhi.hi.is> sigurasg@rhi.hi.is (Sigurdur Asgeirsson) writes: > >result^^[len1 + len2] := chr(0); >{this compiles to an ADD.W D0,D7 or some such, and then CLR.B (A0, D0.L)} >{instead of CLR.B (A0, D0.W), and some garbage in the high byte of D0, } >{sending the zero byte to outer space (or inner space) corrupting the } >{heap, crashing the mac, evoking a big laugh from my friend (who still } >{doesn't believe that it's a compiler error).} This is indeed a bug. The workaround is to declare the upper array bound as 32768, to force a sign extension of the index. This will be fixed in the next release of THINK Pascal. R. -- ----------------------------------------------------------------------- Rich Siegel Internet: siegel@world.std.com Software Engineer Applelink: SIEGEL Symantec Languages Group