Hi!

On Sat, Jun 5, 2010 at 8:31 AM, Andreas Florath <[email protected]> wrote:
>
> +/* This is a copy of the strncpy(3) function but it returns the
> + * index to the end of the dest string.
> + * This eliminates the need for additional strlen() calls. */
> +static size_t mstrncpy(char *dest, const char *src, size_t n)
> +{
> +       size_t i;
> +
> +       for (i=0; i<n && src[i]!='\0'; ++i)
> +               dest[i] = src[i];
> +       dest[i] = '\0';
> +
> +       return i;
> +}

It seems that mstrncpy() behaves more like strlcpy() in that it always
appends a terminating 0 to the destination, which is fine, however if
the loop does not exhaust src, won't dest[i] = 0 will execute with i
== n, i.e. write one byte beyond the end of dest?

> +/* Appends exactly one string to the buffer.
> + * It takes care about the maximal size of buf and the comma
> + * handling between the words.
> + * The function recognizes the fact, that a comma must be inserted, on
> + * the condition whether the used_buf is 0 (->no comma prepended) or
> + * 1 (->comma prepended). */
> +static void decode_mode_append(
> +       unsigned int const mode, char * const buf, size_t const buflen,
> +       struct st_ls2str const * const l2si, size_t * const used_buf)
> +{
> +       if (mode & l2si->flag)
> +       {
> +               if (*used_buf) buf[(*used_buf)++] = ',';
> +               *used_buf += mstrncpy(buf+*used_buf, l2si->name,
> +                                     buflen-*used_buf-1);
> +       }
> +}

(Wouldn't this be easier if used_buf were a plain size_t, and it
returned the new used_buf as return value, rather than fiddling with
the pointer-to-size_t?)

Ciao, Colin
_______________________________________________
Openais mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais

Reply via email to