discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSString.m patch


From: David Relson
Subject: NSString.m patch
Date: Sat, 26 May 2001 09:25:56 -0400

Greetings,

Attached is a patch for NSString.m that corrects a compilation warning, prevents a possible segfault, and shortens the code a bit.

David

*** explanation of what and why ***

The current version of NSString.m generates the following warning:

NSString.m: In function `-[NSString getLineStart:end:contentsEnd:forRange:]': NSString.m:1745: warning: `end' might be used uninitialized in this function

I took a look and observed a structure like:

    if (lineEndIndex || contentsEndIndex)
    {
        end = ...;
        if ( end < len )
        {
            *lineEndIndex;
        }
    }
    if (contentsEndIndex)
    {
        if ( end < len )
    }

The major problem above is that *lineEndIndex may be used even though lineEndIndex is NULL. This can be corrected with a test for lineEndIndex being non-NULL.

A minor problem is that the compiler can't detect that the final use of 'end' can only happen after it's set. Moving the contentsEndIndex conditional inside the first conditional fixes the problem.

These two changes give the structure shown below (and a clean compilation):

    if (lineEndIndex || contentsEndIndex)
    {
        end = ...;
        if ( lineEndIndex )
        {
            if ( end < len )
            {
                *lineEndIndex;
            }
        }
        if (contentsEndIndex)
        {
            if ( end < len ) ...;
        }
    }

Lastly, there is a series of nested conditionals for setting *lineEndIndex. Using '&&' the code can be shortened to:

          if (end < len
              && ((*caiImp)(self, caiSel, end) == (unichar)0x000D)
              && ((*caiImp)(self, caiSel, end+1) == (unichar)0x000A))
            {
                *lineEndIndex = end+1;
            }
          else
            *lineEndIndex = end;

Attachment: NSString.m.patch
Description: Binary data

--------------------------------------------------------
David Relson                   Osage Software Systems, Inc.
relson@osagesoftware.com       Ann Arbor, MI 48103
www.osagesoftware.com          tel:  734.821.8800

reply via email to

[Prev in Thread] Current Thread [Next in Thread]