This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Implement strlcat [BZ#178]


On 12/10/2015 11:38 AM, Florian Weimer wrote:

>> the OpenBSD strlcpy
>> implementation always has well-defined behavior when source and
>> destination overlap, but the proposed implementation does not.
>
> The OpenBSD implementation is defined to be undefined with overlapping
> inputs, too.

No, the OpenBSD implementation has well-defined behavior then. strlcpy is declared like this:

size_t strlcpy (char *, const char *, size_t) __attribute__ ((__bounded__ (__string__, 1, 3)));

http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/include/string.h

and is implemented like this:

  size_t
  strlcpy (char *dst, const char *src, size_t size)
  {
    const char *s = src;
    size_t n = size;

    if (n)
      while (--n && (*dst++ = *s++))
        continue;

    if (!n)
      {
        if (size)
          *dst = '\0';
        while (*s++)
          continue;
      }

    return s - src - 1;
  }

http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/lib/libc/string/strlcpy.c

Nothing in this implementation prohibits overlapping the source and destination. There is no use of 'restrict' or of 'memcpy' or anything like that. Behavior is perfectly well-defined when source and destination overlap.


>> Third, the OpenBSD implementation declares strlcpy and strlcat to have
>> __attribute__ ((__bounded__ ...)), an OpenBSD extension that generates
>> warnings when compiling with gcc -Wbounded (an OpenBSD GCC option that
>> is on by default). The proposed implementation doesn't do that so it by
>> default does not diagnose bugs that the OpenBSD implementation does
>> diagnose.
>
> Doesn't the _FORTIFY_SOURCE wrapper do something similar?

Yes, but the proposed glibc implementation is not "exactly matching the OpenBSD semantics" as Zack insisted upon. For example, whether diagnostics are issued differs by default. There are probably other differences (sorry, I don't know what the __bounded__ attribute does, exactly).


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]