[lxc-devel] [PATCH lxcfs 4/5] cgfs: fix dorealloc's batch allocation
Wolfgang Bumiller
w.bumiller at proxmox.com
Thu Jan 7 19:17:23 UTC 2016
> On January 7, 2016 at 7:42 PM Serge Hallyn <serge.hallyn at ubuntu.com> wrote:
>
>
> Quoting Wolfgang Bumiller (w.bumiller at proxmox.com):
> > The initial check should use real lengths as with modulo a
> > new required length of eg. 52 would be considered smaller
> > than an old length of 48 (2 < 48).
> >
> > To get the 'batches' count 'newlen' must be divided and not
> > taken modulo BATCH_SIZE. Otherwise '101', which would need a
> > 3rd batch to reach 150, would end up with two (2*50 = 100
> > bytes) and thereby be truncated instead.
> >
> > Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
> > ---
> > cgfs.c | 4 ++--
> > 1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/cgfs.c b/cgfs.c
> > index 0659e9e..681a478 100644
> > --- a/cgfs.c
> > +++ b/cgfs.c
> > @@ -75,9 +75,9 @@ static inline void drop_trailing_newlines(char *s)
> > static void dorealloc(char **mem, size_t oldlen, size_t newlen)
> > {
> > int batches;
> > - if (newlen % BATCH_SIZE <= oldlen % BATCH_SIZE)
> > + if (newlen <= oldlen)
>
> This will result in extra reallocs (though innocuous).
> Is there any reason not to just do
>
> if (newlen / BATCH_SIZE <= oldlen / BATCH_SIZE)
Ah yes I missed that the old length is a string length not the
capacity, so yes, the division makes sense. Just not the modulus.
More information about the lxc-devel
mailing list