On Thu, 3 Aug 2023 23:30:31 +0900
Rin Okuyama <rokuyama...@gmail.com> wrote:

> On 2023/08/03 23:23, Valery Ushakov wrote:
> > On Thu, Aug 03, 2023 at 13:33:27 +0000, Rin Okuyama wrote:
> > 
> >> -Wuse-after-free for GCC 12 is premature. It fires on a common idiom:
> >>
> >>    newbuf = realloc(buf, size);
> >>    p = newbuf + (p - buf);
> >>
> >> Let shut this up for GCC 12 (with hoping it gets improved for 13!).
> > 
> > C99 says
> > 
> >         J.2  Undefined behavior
> > 
> >         [#1]   The   behavior   is   undefined   in   the  following
> >         circumstances:
> > [...]
> >           -- The  value of a pointer to an object whose lifetime has
> >              ended is used (6.2.4).
> > 
> > 
> > Yes, for the "obvious" implementation of pointers as addresses the
> > above idiom happens to work, but it doesn't make that idiom any less
> > UB.
> 
> Ah, I only thought about "obvious" impl. Thank you for kind
> explanation! I will revert them for now.

Hi,

Is this sort of fix acceptable for the above cases?

-Tobias

RCS file: /cvsroot/src/usr.bin/sort/files.c,v
retrieving revision 1.42
diff -p -u -r1.42 files.c
--- files.c     5 Aug 2015 07:10:03 -0000       1.42
+++ files.c     7 Aug 2023 21:53:45 -0000
@@ -199,13 +199,14 @@ seq(FILE *fp, u_char **line)
                        /* Long line - double size of buffer */
                        /* XXX: Check here for stupidly long lines */
                        buf_size *= 2;
+                       ptrdiff_t offset = pos - buf;
                        new_buf = realloc(buf, buf_size);
                        if (!new_buf)
                                err(2, "realloc of linebuf to %zu bytes failed",
                                        buf_size);
-               
+
                        end = new_buf + buf_size;
-                       pos = new_buf + (pos - buf);
+                       pos = new_buf + offset;
                        buf = new_buf;
                }
        }

Reply via email to