[Insight-users] Read image header info functions doesn't work? Memory access violation?

Jun Tan bitleo at gmail.com
Fri Dec 3 14:24:09 EST 2004


Hi, Luis and everybody:
I'm not able to get header information correctly by calling functions
such as "GetPatientName" of "DICOMImageIO2". Each time the program
caused some access violation problem.

When I traced into file "DICOMAppHelper.h", I found this:
  void GetPatientName(char* name)
    {
a:    strcpy(name, m_PatientName);
    }

When I traced further into file "DICOMAppHelper.cxx", I found the
following function definition:
void DICOMAppHelper::PatientNameCallback(DICOMParser *,
                                   doublebyte,
                                   doublebyte,
                                   DICOMParser::VRTypes,
                                   unsigned char* val,
                                   quadbyte len)
{
  if (val)
    {
b:    strncpy(m_PatientName, (const char*)val, len < 512 ? len : 511);
    }
  else
    {
    m_PatientName[0] = '\0';
    }
}

In "b:", only "len" characters are copied and there is no NULL('\0')
character appended to the end of m_PatientName. This will make "a:"
illegally overwrite some memory area.
The reason is that m_PatientName is unfortunately not initialized as
"\0\0\0\0\0\0\0\0\0\0....." but as "\-51\-51\-51\-51\-51......", it
does not contain a '\0' and the "strcpy" in "a:" will search and copy
characters until it hits a '\0' somewhere(no one knows where it is) in
the memory.

I'm just wondering if this is a bug or I am using something
incorrectly? I'm using VC++6 under Windows XP. I don't know whether
other C++ compilers have this problem or not. However, maybe manual
initialization for m_PatientName, m_....., or just appending a NULL
character is a good idea. Thank you.

Best regards,


More information about the Insight-users mailing list