[SabreAMF] String references deserializing AMF3

Jim Mischel jim at mischel.com
Tue Nov 14 10:40:08 EST 2006


There doesn't appear to be any messages at all in the archive, so I 
don't know if anybody's caught this one.

Working with SabreAMF yesterday (I downloaded and installed version 
0.3.2066 using PEAR as described on the SabreAMF page at 
http://osflash.org/sabreamf), I noticed that there's a bug in reading 
objects with string references.  The AMF spec at 
http://www.osflash.org/amf3/index says, "...if the string is of zero 
length it is not stored for later reference, therefore does not increase 
the index counter."  The code in SabreAMF\AMF3\Deserializer.php, 
however, does not take that special case into account.  It caches empty 
strings.

Also, when reading a string reference, the code does an additional, 
unnecessary, right shift of the $strref value when accessing the array, 
resulting in the wrong string being read.

The fix is quite simple, as shown here:

/**
 * readString
 *
 * @return string
 */
public function readString() {

    $strref = $this->readInt();

    if (($strref & 0x01) == 0) {
        $strref = $strref >> 1;
        if ($strref>=count($this->storedStrings)) {
            throw new Exception('Undefined string reference: ' . $strref);
            return false;
        }
        // 2006-11-13 - jim - removed shift because shift has already 
been done.
        //printf("String reference %d\n", $strref);
        return $this->storedStrings[$strref];
        //return $this->storedStrings[$strref >> 1];
    } else {
        $strlen = $strref >> 1;
        $str = $this->stream->readBuffer($strlen);
        // 2006-11-13 - jim - if string is empty, don't save a reference.
        if ($str != "")
        {
          $this->storedStrings[] = $str;
          //printf("String #%d = %s\n", sizeof($this->storedStrings)-1, 
$str);
        }
        return $str;
    }
}

I've retained the old code (commented out), and also some debugging 
output that I used to isolate the problem.

Please let me know if this is the proper forum to report bugs and/or 
supply fixes.  I'm hoping to use SabreAMF for a project, and am happy to 
contribute to its ongoing development.

Jim






More information about the sabreamf mailing list