whoops...

memcpy(&buf[MAX(0, len - 3)], "...", MIN(3, len));

2008/6/1, Premysl Hruby <[EMAIL PROTECTED]>:
> On (01/06/08 19:30), Enno Gottox Boland wrote:
>  > To: dynamic window manager <[email protected]>
>  > From: Enno Gottox Boland <[EMAIL PROTECTED]>
>
> > Subject: Re: [dwm] [patch] simplification to drawtext
>
> > Reply-To: dynamic window manager <[email protected]>
>  > List-Id: dynamic window manager <dwm.suckless.org>
>  >
>
> > beware that it should be
>  >
>  > memcpy(&buf[MAX(0, len - 3)], "...", 4);
>  >
>  > not
>  >
>  > memcpy(&buf[MAX(0, len - 3)], "...", 3);
>  >
>  > otherwise it will overwrite the zero-terminator if len < 3.
>
>
> This is REALLY OK, as that Xlib calls doesn't use zero-terminated
>  string, but string with length argument!
>
>
>  >
>  > Furthermore it will change the length of the string if len < 3, which
>  > has no impact on the output, as XDrawString respects len, but it's
>  > still ugly.
>  >
>  > So I still prefer strncpy as the result doing the same with memcopy
>  > should look like this to work properly:
>  > memcpy(&buf[MAX(0, len - 3)], "...", MAX(len, 3));
>
>
> That MAX is baaad, as if len > 4 it will copy some random data after
>  string "..." which is really bad.
>
>
>  >
>  > I also prefer strncpy over memcpy because we're not copy plain data,
>  > but strings.
>
>
> No, at that point, we are copying plain data. Not strings[1]
>
>  >
>  > regards
>  > Gottox
>  >
>
>  [1]: meaned as C string == char[] which ends with zero-terminator
>
>
>  --
>  Premysl "Anydot" Hruby, http://www.redrum.cz/
>
>


-- 
http://www.gnuffy.org - Real Community Distro
http://www.gnuffy.org/index.php/GnuEm - Gnuffy on Ipaq (Codename Peggy)

Reply via email to