Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Errors - Warnings    [ Add a report in this area ]  
Report #:  894   Status: Open
No warning when a function returning a "string" never initializes Result
Project:  Delphi Build #:  4.453
Version:    7.0 Submitted By:   Jordan Russell
Report Type:  Basic functionality failure Date Reported:  4/18/2002 12:42:17 PM
Severity:    Commonly encountered problem Last Updated: 3/20/2012 2:24:39 AM
Platform:    All platforms Internal Tracking #:   74879
Resolution: None (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: (36 Total Ratings)
4.64 out of 5
Total Votes: 120

The compiler does not issue a warning when a function returning a string never initializes Result. This behavior may lead one to assume that Delphi clears string Result variables automatically as it does with local variables of type string, but that is not the case.

On functions that return any other type (e.g. Integer), a warning is properly issued.
Steps to Reproduce:

* compile

function f: string;

//exp: [Warning] Return value of function 'f' might be undefined
//act: no warning

Julian Raschke at 5/13/2002 5:49:17 AM -
Hmmm, if it doesn't initialize interfaces and strings to nil/'', wouldn't setting the result then cause an AV (trying to decrease the reference rounter of a stale pointer)?

Jordan Russell at 5/16/2002 7:21:10 PM -
No. It's important to understand that a function that's declared like this:

  function X: String;

really works like this internally:

  procedure X(var Result: String);

A function that calls X will always pass a valid pointer to a String on the stack. However, the String it passes may not be necessarily be empty. Consider this code for example:

  S: String;
  S := 'test';
  S := X;
  // S will still equal 'test' here if the function X didn't initialize Result

Jordan Russell at 8/29/2002 9:32:48 PM -
Issue still exists in Delphi 7.

Ralf Stocker at 10/11/2006 11:41:12 PM -
Still exists in Turbo Delphi 2006 10.0.2288.42451

Pieter Zijlstra at 2/6/2008 12:53:40 PM -
Still exists in Delphi 2007 11.0.2902.10471 (aka December update)

Krystian Bigaj at 7/29/2009 12:50:56 PM -
Still in D2009 12.0.3420.21218 (Update 3)

Tomohiro Takahashi at 7/29/2009 6:30:05 PM -
Thanks for the reconfirmation.
Yes, this issue is still Open.

Server Response from: ETNACODE01