El día martes, enero 26, 2021 a las 02:40:26p. m. -0800, John Reiser escribió:

> > The reason was at the end of debugging that we replaced in some central
> > place of the server a function call (due to vg messages about
> > overlapping args)
> > 
> >     strncpy(dst, src, n);
> > 
> > by
> > 
> >     memset(dst, 0, n);
> >     memmove(dst, src, n);
> > 
> > which is not fully equivalent because strncpy(3) will stop at the first
> > \0 byte in src, while memmove(3) will copy n bytes, regardles if they
> > are valid bytes in src. As this was in some low level function, it
> > generated a mass of the above vg messages.
> [[snip]]
> > Any thoughts about this?
> 
> You may have a much bigger problem than you realize.  Take paper
> and pencil; write TEN TIMES (this is not a joke!):
> 
>      strncpy(dst, src, n) always over-writes exactly n bytes
>      (namely dst[0..(n-1)]), regardless of strlen(src).

I know this. strncpy(dst, src, n) writes strlen(src) bytes to dst and
fills the rest in dst until n with \0 bytes. This is clear but not the point of
the problem. The problem is (was) that strncpy(dst, src, n) only
reads(!) strlen(src) bytes from src, while memmove(dst, src, n) reads n
bytes from src and if n > strlen(src) it reads illegal bytes, raised as
an error by valgrind correctly.

        matthias


-- 
Matthias Apitz, ✉ g...@unixarea.de, http://www.unixarea.de/ +49-176-38902045
Public GnuPG key: http://www.unixarea.de/key.pub
¡Con Cuba no te metas!  «»  Don't mess with Cuba!  «»  Leg Dich nicht mit Kuba 
an!
http://www.cubadebate.cu/noticias/2020/12/25/en-video-con-cuba-no-te-metas/


_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to