Author Topic: Overlapping buffer in copy operation in String.Delete method  (Read 4046 times)

Offline kkretzsch

  • PTeam Member
  • PixInsight Addict
  • ***
  • Posts: 217
Hi,
I am using the DeleteRight and DeleteLeft methods of the String class (String.h) to transform a String (actually an IsoString), e.g "%10.8m" into "10.8". So I want to remove the last and the first character from the source String. This works perfectly as long as the destination string is shorter than 4 character/bytes. If the destination String is 4 bytes long I get wrong results, e.g. with the example string above  "0.88" instead of "10.8". I debugged the code and found out the memcpy method in the CharTraits.h:577 file is responsible for that (see gdb session log below).

The source and destination addresses are only one byte away, so the buffer overlap here and I think a memmove would be the better choice than a memcpy (see String.h:1407)

Klaus


pcl::GenericString<char, pcl::IsoCharTraits, pcl::StandardAllocator>::Delete (this=0x7fffffffdae0, i=0, n=1) at /home/klaus/C++/PCL_dev/include/pcl/String.h:1382
1382          if ( n > 0 )
(gdb) n
1384             size_type len = Length();
(gdb) n
1385             if ( i < len ) // index must be valid
(gdb) n
1387                n = pcl::Min( n, len-i );  // constrain to delete existing chars.
(gdb) n
1388                if ( n == len )   // deleting the entire string
(gdb) n
1392                   SetUnique();   // ensure that only we own these chars.
(gdb) n
1394                   size_type newLen = len-n;
(gdb) n
1395                   T* old = data->string;
(gdb) n
1398                   if ( data->ShouldReallocate( newLen ) )
(gdb) n
1406                   if ( i < newLen )
(gdb) n
1407                      R::Copy( data->string+i, old+i+n, newLen-i );
(gdb) print old+i+n
$22 = 0x84c171 "10.8"
(gdb) print newLen-i
$23 = 4
(gdb) print data->string+1
$24 = 0x84c171 "10.8"
(gdb) print data->string+i
$25 = 0x84c170 "%10.8"
(gdb) s
pcl::IsoCharTraits::Copy (dst=0x84c170 "%10.8", src=0x84c171 "10.8", n=4) at /home/klaus/C++/PCL_dev/include/pcl/CharTraits.h:577
577           ::memcpy( dst, src, n );
(gdb) x/4b dst
0x84c170:       37      49      48      46
(gdb) x/4bx dst
0x84c170:       0x25    0x31    0x30    0x2e
(gdb) x/4bx src
0x84c171:       0x31    0x30    0x2e    0x38
(gdb) n
578        }
(gdb) x/4bx dst
0x84c170:       0x30    0x2e    0x38    0x38
(gdb) x/4bx src
0x84c171:       0x2e    0x38    0x38    0x38





 

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Overlapping buffer in copy operation in String.Delete method
« Reply #1 on: 2014 December 02 13:36:41 »
Hi Klaus,

I am going to fix this problem ASAP on the GitHub repository; in the meanwhile, please apply the fix with memmove() to your copy of PCL. Sorry for the trouble.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline kkretzsch

  • PTeam Member
  • PixInsight Addict
  • ***
  • Posts: 217
Re: Overlapping buffer in copy operation in String.Delete method
« Reply #2 on: 2014 December 03 11:04:29 »
Hi Juan,
Thanks! I replaced the call of the Copy function by CopyOverlapped and it worked fine then.

Everything else works perfectly, Thanks!