[vtk-developers] Bug in vtkWriter::EncodeWriteString?

Wilson, Andrew T atwilso at sandia.gov
Tue Oct 20 18:39:47 EDT 2009


I'm trying to use vtkTableWriter / vtkTableReader to save and load tables
with vtkUnicodeStringArray columns.  I'm seeing some strange behavior when
encoding non-ASCII data.  I've been able to reproduce this under Mac OSX
10.5.8 (gcc 4.0.0), Red Hat Enterprise Workstation 5 (gcc 4.1.2, x86_64) and
Visual Studio 2008 (debug build) under XP SP3.


To be specific, the code in vtkWriter::EncodeWriteString is producing too
much output when it encounters a byte that it wants to escape.    It
*should* produce %B3, %CA, and so forth.  Instead I observe that it produces
%FFFFFFB3, %FFFFFFCA, et cetera.  It's like the 8-bit char argument to
sprintf is being promoted to a 32-bit int.  I presume (but haven't verified)
that vtkWriter::EncodeString has the same problem.

Here's the code in question:

  while( name[cc] )
    {
    // Encode spaces and %'s (and most non-printable ascii characters)
    // The reader does not support spaces in strings.
    if ( name[cc] < 33  || name[cc] > 126 ||
         name[cc] == '\"' || name[cc] == '%' )
      {
      sprintf(buffer, "%02X", name[cc]);
      if (doublePercent)
        {
        str << "%%";
        }
      else
        {
        str << "%";
        }
      str << buffer;
      }
    else
      {
      str << name[cc];
      }
    cc++;
    }


I'm attaching a very short program that demonstrates the bug.  I also have a
proposed fix: 

(up at the top of the file)
namespace 
{
  const char hex_digits[] = "0123456789ABCDEF";
};

(in EncodeWriteString, replacing part of the while loop above)

    if ( name[cc] < 33  || name[cc] > 126 ||
         name[cc] == '\"' || name[cc] == '%' )
      {
      if (doublePercent)
        {
        *out << "%%";
        }
      else
        {
        *out << "%";
        }
      *out << (hex_digits[(name[cc] >> 4) & 0x0F])
           << (hex_digits[name[cc] & 0x0F]);
      } 




Comments?  Am I doing something wrong or is this a real bug?

-- Andy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: DataWriterBugDemo.cxx
Type: application/octet-stream
Size: 1651 bytes
Desc: DataWriterBugDemo.cxx
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20091020/c2787286/attachment-0001.obj>


More information about the vtk-developers mailing list