Hi Kevin,

> Date: 2026-09-30 15:45:43+0800
> From: "Kevin J. McCarthy" <[email protected]>
>
> On Wed, Sep 30, 2026 at 09:30:01AM +0200, Alejandro Colomar via Mutt-dev 
> wrote:
> > > diff --git a/buffer.c b/buffer.c
> > > index 24815b95..75ac28fb 100644
> > > --- a/buffer.c
> > > +++ b/buffer.c
> > > @@ -191,7 +191,8 @@ void mutt_buffer_addstr_n(BUFFER *buf, const char *s, 
> > > size_t len)
> > >    if (!buf->data ||
> > >        (buf->dptr + len + 1 > buf->data + buf->dsize))
> > >      mutt_buffer_increase_size(buf, buf->dsize + (len < 128 ? 128 : len + 
> > > 1));
> > > -  memcpy(buf->dptr, s, len);
> > > +  if (s && len)
> > 
> > You don't need to test whether 'len' is nonzero.  memcpy(,,0) is defined
> > to be a no-op.
> > 
> > BTW, out of curiosity, can 's' be NULL here in some path?  It would be
> > interesting to note in the commit message which conditions could make it
> > happen.  Or is it just out of caution?
> 
> I noticed some of the ACCOUNT fields were being used in buffer add
> operations, so I added this commit out of caution.  In those cases, len
> would be 0.  If you're sure that's safe then this is an optional patch that
> I can drop.

ISO C2y contains this paragraph in the specification of <string.h>:

        Where an argument declared as size_t n
        specifies the length of the array for a function,
        n can have the value zero on a call to that function.
        Unless explicitly stated otherwise
        in the description of a particular function in this subclause,
        pointer arguments on such a call may be null pointers.
        On such a call,
        a function that locates a character finds no occurrence,
        a function that compares two character sequences returns zero,
        and a function that copies characters copies zero characters.

This wasn't present in earlier standards, but it has always de-facto
worked, AFAIK.  The C++ library needs this behavior to work.  If a
compiler wouldn't have worked correctly, it would probably have been
noticed.  I've also seen other projects use this before it was accepted
by C2y, and the code had always worked.

> > BTW, memcpy(NULL,NULL,0) is also defined to be valid and a no-op, as
> > accorded by the C Committee for ISO C2y (to become C29, most likely),
> > and in practice, it already works in every compiler because C++ requires
> > it.  NULL pointers are invalid only if len!=0.
> 
> Thank you!


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>

Attachment: signature.asc
Description: PGP signature

Reply via email to