[fpc-pascal] memory corruption

Michael Müller mueller_michael at alice-dsl.net
Thu Jan 26 08:10:45 CET 2012


Am 26.01.2012 um 03:50 schrieb ik:

> Hello,
> 
> While testing my following code:
> https://github.com/ik5/redis_client.fpc/blob/master/tests/test_parser.lpr#L166
> 
> When I'm executing the code on the second item (that is suppose to be null in the protocol -> '$-1#13#10'),
> the program crashes with the following message:
> *** glibc detected *** redis_client/tests/test_parser: malloc(): memory corruption (fast): 0x0000000000750d50 ***
> 
> Using gdb to display the information seems that the following like:
> https://github.com/ik5/redis_client.fpc/blob/master/tests/test_parser.lpr#L85 is the cause.
> It looks like accessing either tmps or ALine[j] is causing it.

I would expect that already line 83 is the problem. You have to change the order of the statements otherwise you'll access a not existing element in ALine before checking if the index is already to big when j becomes > alength.
So change
while (ALine[j] <> #13) and (j <= alength) do
into
while (j <= alength) and (ALine[j] <> #13) do

But this will only work if the compiler switch {$B-} (verbose version {$BOOLEVAL OFF}) is set which is the default in Delphi so I assume the same in FPC too.
If you can't ensure that you should do it like this
while (j <= alength) do
  if (ALine[j] <> #13) then

Regards

Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20120126/e9eaa1b7/attachment.html>


More information about the fpc-pascal mailing list