On 2022-07-28 13:27, Siddhesh Poyarekar wrote:
Interesting, I'll take a closer look at this from the gcc context.  I obviously don't have any strong opinions about the elfutils patch :)

I reduced this to the below program and I see it warns with `-D_FORTIFY_SOURCE=3 -O2 -fsanitize=undefined` as well as `-D_FORTIFY_SOURCE=2 -O2 -fsanitize=undefined`. This is definitely a false positive on unreachable code; __pread_alias will never be called when nbytes is greater than object size.

So I can confirm that this is harmless.

Sid

~~~

typedef long ssize_t;
typedef unsigned long size_t;
typedef long off_t;


struct ar_hdr
{
  char ar_mode;
  char ar_size[10];
};

extern ssize_t __pread_chk (int fd, void *buf, size_t nbytes, off_t offset,
                            size_t bufsize)
  __attribute__((__access__ (__write_only__, 2, 3)));
extern ssize_t __pread_alias (int fd, void *buf, size_t nbytes, off_t offset)
  __attribute__((__access__ (__write_only__, 2, 3)));
extern ssize_t __pread_chk_warn (int fd, void *buf, size_t nbytes,
                                 off_t offset, size_t bufsize);

extern __inline __attribute__((__always_inline__))
  __attribute__((__gnu_inline__)) ssize_t
pread (int fd, void *buf, size_t nbytes, off_t offset)
{
  size_t osz = __builtin_dynamic_object_size (buf, 0);
  if (__builtin_constant_p (osz) && osz == (size_t) -1)
    return __pread_alias (fd, buf, nbytes, offset);
  return (((__typeof (nbytes)) 0 < (__typeof (nbytes)) - 1
           || (__builtin_constant_p (nbytes) && (nbytes) > 0))
          && __builtin_constant_p (nbytes <= osz / 1)
          && nbytes <= osz / 1)
    ? __pread_alias (fd, buf, nbytes, offset)
    : __pread_chk (fd, buf, nbytes, offset, osz);
}

ssize_t
pread_retry (int fd, off_t start_offset, off_t offset)
{
  ssize_t recvd = 0;
  struct ar_hdr h = {.ar_size = {0} };
  void *buf = h.ar_size;
  size_t len = sizeof (h.ar_size);
  off_t off =
    start_offset + offset + __builtin_offsetof (struct ar_hdr, ar_size);

  do
    {
      long int res;
      do
        {
res = pread (fd, ((char *) buf) + recvd, len - recvd, off + recvd);
        }
      while (res == -1L);
      recvd += res;
    }
  while ((size_t) recvd < len);

  return recvd;
}

Reply via email to