On 08/11/08 14:24, Bram Moolenaar wrote:
>
> Dominique Pelle wrote:
>
>> While using Vim-7.2.30 built with GUI GTK (huge), I stumbled
>> upon this error with Valgrind memory checker:
>>
>> ==13326== Source and destination overlap in memcpy(0x4B5D8D8, 0x4B5D8E4, 13)
>> ==13326== at 0x4024C92: memcpy (mc_replace_strmem.c:402)
>> ==13326== by 0x8102E99: LookupName (if_xcmdsrv.c:1021)
>> ==13326== by 0x810197E: DoRegisterName (if_xcmdsrv.c:303)
>> ==13326== by 0x8101718: serverRegisterName (if_xcmdsrv.c:225)
>> ==13326== by 0x81076D1: prepare_server (main.c:3379)
>> ==13326== by 0x8104253: main (main.c:721)
>>
>> Attached patch fixes it by calling mch_memmove() instead of memcpy().
>> Looking at the code, I found another place where the same problem would
>> also happen (also fixed in patch).
>
> Some versions of memcpy() can handle overlaps, but I suppose it's not
> guaranteed. Thanks for another patch.
>
Configure is supposed to check whether one of the system provided
string-move operations handle overlap. Here's what I see in the logs and
files produced by configure on my system:
-- in configure stdout/stderr:
checking whether memmove handles overlaps... yes
-- in src/auto/config.log
configure:15252: checking whether memmove handles overlaps
configure:15274: gcc -o conftest -O2 -fno-strength-reduce -Wall -L.
-rdynamic -Wl,-export-dynamic -Wl,-E
-Wl,-rpath,/usr/lib/perl5/5.10.0/i586-linux-thread-multi/CORE
-L/usr/local/lib conftest.c -lm -lncurses -lnsl -lacl -lattr -lgpm >&5
conftest.c:10: warning: return type defaults to 'int'
configure:15278: $? = 0
configure:15284: ./conftest
configure:15288: $? = 0
configure:15310: result: yes
-- in src/auto/config.cache:
vim_cv_memmove_handles_overlap=${vim_cv_memmove_handles_overlap=yes}
-- in src/auto/config.h
/*
* If we cannot trust one of the following from the libraries, we use our
* own safe but probably slower vim_memmove().
*/
/* #undef USEBCOPY */
#define USEMEMMOVE 1
/* #undef USEMEMCPY */
-- in src/os_unix.h:
/* memmove is not present on all systems, use memmove, bcopy, memcpy or our
* own version */
/* Some systems have (void *) arguments, some (char *). If we use (char
*) it
* works for all */
#ifdef USEMEMMOVE
# define mch_memmove(to, from, len) memmove((char *)(to), (char
*)(from), len)
#else
# ifdef USEBCOPY
# define mch_memmove(to, from, len) bcopy((char *)(from), (char *)(to),
len)
# else
# ifdef USEMEMCPY
# define mch_memmove(to, from, len) memcpy((char *)(to), (char
*)(from), len)
# else
# define VIM_MEMMOVE /* found in misc2.c */
# endif
# endif
#endif
-- in src/misc2.c:
#ifdef VIM_MEMMOVE
/*
* Version of memmove() that handles overlapping source and destination.
* For systems that don't have a function that is guaranteed to do that
(SYSV).
*/
void
mch_memmove(dst_arg, src_arg, len)
void *src_arg, *dst_arg;
size_t len;
{
/*
* A void doesn't have a size, we use char pointers.
*/
char *dst = dst_arg, *src = src_arg;
/* overlap, copy backwards */
if (dst > src && dst < src + len)
{
src += len;
dst += len;
while (len-- > 0)
*--dst = *--src;
}
else /* copy forwards */
while (len-- > 0)
*dst++ = *src++;
}
#endif
So I suppose mch_memmove should be used everywhere for moves of byte
strings (overlapping or not), and it will be resolved by bcopy, memmove,
memcpy or the owncoded function according to what configure has found.
Best regards,
Tony.
--
This Fortue Examined By INSPECTOR NO. 2-14
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---