[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

does size include the null?

From: Sam Roberts
Subject: does size include the null?
Date: Mon, 21 Jan 2002 11:29:32 -0500
User-agent: Mutt/1.3.16i

Bon soir!

I don't know why I haven't noticed this before (possibly because I
tend to use cheesy fixed-size buffers, or the _aget_ functions), but
is this really the intention:

if((m->rc = envelope_sender(envelope, NULL, 0, &sz)))

if(!(fromaddr = malloc(sz + 1)))

if((m->rc = envelope_sender(envelope, fromaddr, sz + 1, &osz)))

The comment on mu_cpystr() suggest that that wasn't the intention:

/* Smart strncpy that always add the null and returns the number of bytes
   written.  */
mu_cpystr (char *dst, const char *src, size_t size)
  size_t len = src ? strlen (src) : 0 ;
  if (dst == NULL || size == 0)
    return len;
  if (len >= size)
    len = size - 1;
  memcpy (dst, src, len);
  dst[len] = '\0';
  return len;

The comment is wrong, it always returns one less than the number
of bytes written.

Since the purpose of the argument is tell people how long to make
their buffers, I think it should tell them that directly. The buffer
length will be one more than the strlen() of the buffer, but
that's obvious... after enough core dumps.


Sam Roberts <address@hidden> (Vivez sans temps mort!)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]