Valery Ushakov writes: > On Mon, Aug 07, 2023 at 23:58:50 +0200, Tobias Nygren wrote: > > > Is this sort of fix acceptable for the above cases? > [...] > > + ptrdiff_t offset = pos - buf; > [...] > > - pos = new_buf + (pos - buf); > > + pos = new_buf + offset; > > I think so. But e.g. in this particular case I don't see why pos > pointer is needed at all. If pos is made into a size_t pos; index > into the buf instead of a separate pointer, one avoids the whole thing > and "end" can be g/c'ed too, b/c you can just compare the index to the > "buf_size". But the above kind of fix has the advantage of being more > or less mechanical.
yup, i agree. if we can fix it better, please do, but i'm all for making it "less bad" if not properly fixed like the above. i've used this idiom in a couple of places, but didn't quite get it working in several others. these are the problematic files i've see: lib/libc/net/gethnamaddr.c lib/libedit/chartype.c lib/libkvm/kvm_proc.c usr.bin/find/misc.c usr.bin/mail/fio.c external/bsd/pdisk/dist/io.c usr.bin/rs/rs.c usr.bin/sort/files.c the first 3 i worked around, eg below, but i think i only tested the gethnamaddr.c portion so far. .mrg. Index: lib/libc/net/gethnamaddr.c =================================================================== RCS file: /cvsroot/src/lib/libc/net/gethnamaddr.c,v retrieving revision 1.94 diff -p -u -r1.94 gethnamaddr.c --- lib/libc/net/gethnamaddr.c 19 Apr 2022 20:32:15 -0000 1.94 +++ lib/libc/net/gethnamaddr.c 7 Aug 2023 23:41:44 -0000 @@ -110,10 +110,11 @@ __weak_alias(gethostent,_gethostent) #define addalias(d, s, arr, siz) do { \ if (d >= &arr[siz]) { \ + size_t _off = d - arr; \ char **xptr = realloc(arr, (siz + 10) * sizeof(*arr)); \ if (xptr == NULL) \ goto nospc; \ - d = xptr + (d - arr); \ + d = xptr + _off; \ arr = xptr; \ siz += 10; \ } \ Index: lib/libedit/chartype.c =================================================================== RCS file: /cvsroot/src/lib/libedit/chartype.c,v retrieving revision 1.36 diff -p -u -r1.36 chartype.c --- lib/libedit/chartype.c 30 Oct 2022 19:11:31 -0000 1.36 +++ lib/libedit/chartype.c 7 Aug 2023 23:41:44 -0000 @@ -235,17 +235,17 @@ ct_visual_string(const wchar_t *s, ct_bu } /* failed to encode, need more buffer space */ - used = dst - conv->wbuff; + size_t sused = (uintptr_t)dst - (uintptr_t)conv->wbuff; if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1) return NULL; - dst = conv->wbuff + used; + dst = conv->wbuff + sused; } if (dst >= (conv->wbuff + conv->wsize)) { /* sigh */ - used = dst - conv->wbuff; + size_t sused = (uintptr_t)dst - (uintptr_t)conv->wbuff; if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1) return NULL; - dst = conv->wbuff + used; + dst = conv->wbuff + sused; } *dst = L'\0'; Index: lib/libkvm/kvm_proc.c =================================================================== RCS file: /cvsroot/src/lib/libkvm/kvm_proc.c,v retrieving revision 1.98 diff -p -u -r1.98 kvm_proc.c --- lib/libkvm/kvm_proc.c 19 Apr 2022 20:32:16 -0000 1.98 +++ lib/libkvm/kvm_proc.c 7 Aug 2023 23:41:44 -0000 @@ -980,7 +980,7 @@ kvm_argv(kvm_t *kd, const struct minipro if (len + cc > kd->argspc_len) { ptrdiff_t off; char **pp; - char *op = kd->argspc; + uintptr_t op = (uintptr_t)kd->argspc; kd->argspc_len *= 2; kd->argspc = _kvm_realloc(kd, kd->argspc, @@ -991,7 +991,7 @@ kvm_argv(kvm_t *kd, const struct minipro * Adjust argv pointers in case realloc moved * the string space. */ - off = kd->argspc - op; + off = (uintptr_t)kd->argspc - op; for (pp = kd->argv; pp < argv; pp++) *pp += off; ap += off;