So, we recently updated to XE3 at work (began transition before XE4)... and today one of our customers ran across this code-gen bug, which causes an AV due to nil pointer access. Took some time to find it since we of course mostly run Debug builds on our dev machines, so couldn't reproduce the AV right away.

So, we recently updated to XE3 at work (began transition before XE4)... and today one of our customers ran across this code-gen bug, which causes an AV due to nil pointer access. Took some time to find it since we of course mostly run Debug builds on our dev machines, so couldn't reproduce the AV right away.

{$O+}
procedure TForm1.Button1Click(Sender: TObject);
var
  lValue: string;
begin
  lValue:='X';
  if not cds1.IsEmpty and (lValue='D') then
  //if (lValue='D') and not cds1.IsEmpty then  //<---this line works
    cds1.Delete
  else if not cds1.IsEmpty then
    cds1.Edit
  else
    cds1.Append;
end;

"cds1" here is a TClientDataSet with a single field. It crashes on the Edit or Append.

Disabling optimizations works, as does rearranging the "and" expression.

Wonder how this slipped past unit testing.

Comments

  1. Asbjørn Heid I would have been surprised if it were caught! It's not one of those things you'd generally expect to fail :)

    A

    ReplyDelete
  2. Look if this synthax works:
    if (not cds1.IsEmpty) and (lValue='D') then

    MB

    ReplyDelete
  3. Marcin Barański Not in the test app we wrote :( Using XE3 Update 2.

    Seems the issue is that it loads the address of "cds1"  into a register (ESI here) on the Delete line, and then thinks this register is valid when it calls Edit or Append, using it to initialize EAX before the method calls.

    ReplyDelete
  4. Andrea Raimondi Well this is the compiler, they should test all the things that just should work ;)

    ReplyDelete
  5. Stefan Glienke Thanks for checking. Not sure we'll jump to XE4 or 5 anytime soon anyway, components makes it a drag :(

    ReplyDelete
  6. You should fill a QC report with that bug. Don't forget to add complete code to reproduce the error easily.

    ReplyDelete
  7. My coworker said he had sent it to emb. I'll ask if he meant qc or direct mail (good a time as any to use one of our SA support incidents I guess).

    ReplyDelete
  8. He reported it directly, so it's now internally tracked. I guess I can still make a QC out of it and mention this?

    ReplyDelete

Post a Comment