From owner-svn-src-all@freebsd.org Wed Jan 11 16:09:59 2017 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 9F19ACAB91C; Wed, 11 Jan 2017 16:09:59 +0000 (UTC) (envelope-from asomers@gmail.com) Received: from mail-yw0-x243.google.com (mail-yw0-x243.google.com [IPv6:2607:f8b0:4002:c05::243]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 58F681AF2; Wed, 11 Jan 2017 16:09:59 +0000 (UTC) (envelope-from asomers@gmail.com) Received: by mail-yw0-x243.google.com with SMTP id k6so16577356ywk.0; Wed, 11 Jan 2017 08:09:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=V6A/QHQ3s+vseQCt0ws9lSoKaoAsJSJ72HIyxHhKBu4=; b=aPExZkzkmllwlIh2hXbzmn4DuWhtV9vyEIPNZp5aaen7+Ds8eQZJqCgOcVOamvG5R8 ZCqJ7l32n8Rb1/U6RNEp01efJ2agCBd/yzlTtrqjiVul3KbIXbvfWPHX4QZ+5d7K3Poy plR5L+H5a3QGClH+R64rHw+TqEpIw0TgAzWO3Z8TMEukPAJR9/jt5J19TqlXNKILidgp A7uWSulSSWwiVz1K265qeiVIs4ewBBlpklzZxWAzGR/23uRxSXHeDoCA/DtUhHb90WlS xpXkTFKbZSnq4+zuC/P9O6vc3DUUYBtzMku0vxQBkvIEt6Tjkf6SGzszFKLJZv2Dlou+ P7GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=V6A/QHQ3s+vseQCt0ws9lSoKaoAsJSJ72HIyxHhKBu4=; b=dLLf9kNoxK+OL6I3Rm+3cOzXkmFX6AvFcowfxR7mM9hC6HCn1HR5Ola+gSS1PtfPva K62Z+uCwU/lTIBdzqoZqutKAhOjYRZX/fI/bjujT3upGrh5mABtyKdY8yfs5KPhvA353 KO9hhCe508TZismwyfpTtSB/Oj9G/y4flBepbCjZuCtFZBEkNBsvoNUUJhlUN+Ze/qYZ j0n3C4MXIXPZ2CMGU7vTQW+jjR9JVy6Nju3nTdkBL04TAU4xmWlTigkcz+K8aSdJMHE2 TH/gDAARqhKoIW3zqhGh9bpQ++vgA42/+barUcw3YR43JPaaTj8Ei/geYn9oZamwFpjs VyOA== X-Gm-Message-State: AIkVDXK/P5m6p1Yen5SgUU3wUwqBfJ4sTzyffmY2dieJPWovYYCDRCRDO7wiPhn+13ZJIDJlopVt1N+sr7nUCw== X-Received: by 10.129.90.67 with SMTP id o64mr7677531ywb.188.1484150998221; Wed, 11 Jan 2017 08:09:58 -0800 (PST) MIME-Version: 1.0 Sender: asomers@gmail.com Received: by 10.129.137.135 with HTTP; Wed, 11 Jan 2017 08:09:57 -0800 (PST) In-Reply-To: References: <201701102043.v0AKhWuv073169@repo.freebsd.org> From: Alan Somers Date: Wed, 11 Jan 2017 09:09:57 -0700 X-Google-Sender-Auth: SrQNADLRjgHE72LM8US3c4KxXKY Message-ID: Subject: Re: svn commit: r311895 - in head: etc/mtree usr.bin/tail usr.bin/tail/tests To: Sergey Kandaurov Cc: "src-committers@freebsd.org" , "svn-src-all@freebsd.org" , "svn-src-head@freebsd.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Jan 2017 16:09:59 -0000 On Wed, Jan 11, 2017 at 5:53 AM, Sergey Kandaurov wrote: > On 10 January 2017 at 23:43, Alan Somers wrote: >> >> Author: asomers >> Date: Tue Jan 10 20:43:32 2017 >> New Revision: 311895 >> URL: https://svnweb.freebsd.org/changeset/base/311895 >> >> Log: >> Fix memory leaks during "tail -r" of an irregular file >> [..] >> >> -typedef struct bf { >> - struct bf *next; >> - struct bf *prev; >> - int len; >> - char *l; >> -} BF; >> +static const size_t bsz = 128 * 1024; >> +typedef struct bfelem { >> + TAILQ_ENTRY(bfelem) entries; >> + size_t len; >> + char l[bsz]; >> +} bfelem_t; >> > > This breaks on gcc that doesn't respect const for some reason: > reverse.c:177: error: variably modified 'l' at file scope > >> >> /* >> * r_buf -- display a non-regular file in reverse order by line. >> @@ -189,64 +190,42 @@ typedef struct bf { >> static void >> r_buf(FILE *fp, const char *fn) >> { >> - BF *mark, *tl, *tr; >> - int ch, len, llen; >> + struct bfelem *tl, *temp, *first = NULL; >> + size_t len, llen; > > > reverse.c:194: warning: 'len' may be used uninitialized in this function > > I suspect this is due to a typo on line 254. > > [..] > >> >> /* >> - * Step through the blocks in the reverse order read. The last >> char >> - * is special, ignore whether newline or not. >> + * Now print the lines in reverse order >> + * Outline: >> + * Scan backward for "\n", >> + * print forward to the end of the buffers >> + * free any buffers that start after the "\n" just found >> + * Loop >> */ >> - for (mark = tl;;) { >> - for (p = tl->l + (len = tl->len) - 1, llen = 0; len--; >> - --p, ++llen) >> - if (*p == '\n') { >> - if (llen) { >> + tl = TAILQ_LAST(&head, bfhead); >> + first = TAILQ_FIRST(&head); >> + while (tl != NULL) { >> + for (p = tl->l + tl->len - 1, llen = 0; p >= tl->l; >> + --p, ++llen) { >> + int start = (tl == first && p == tl->l); >> + >> + if ((*p == '\n') || start) { >> + struct bfelem *tr; >> + >> + if (start && len) > > > here > > joint patch to fix build on gcc (not tested, although tests pass) > > Index: reverse.c > =================================================================== > --- reverse.c (revision 311927) > +++ reverse.c (working copy) > @@ -170,11 +170,11 @@ > ierr(fn); > } > > -static const size_t bsz = 128 * 1024; > +#define BSZ (128 * 1024) > typedef struct bfelem { > TAILQ_ENTRY(bfelem) entries; > size_t len; > - char l[bsz]; > + char l[BSZ]; > } bfelem_t; > > /* > @@ -216,9 +216,9 @@ > > /* Fill the block with input data. */ > len = 0; > - while ((!feof(fp)) && len < bsz) { > + while ((!feof(fp)) && len < BSZ) { > p = tl->l + len; > - len += fread(p, 1, bsz - len, fp); > + len += fread(p, 1, BSZ - len, fp); > if (ferror(fp)) { > ierr(fn); > return; > @@ -251,7 +251,7 @@ > if ((*p == '\n') || start) { > struct bfelem *tr; > > - if (start && len) > + if (start && llen) > WR(p, llen + 1); > else if (llen) > WR(p + 1, llen); > > -- > wbr, > pluknet Thanks for the tip, Sergey. Fixed in 311928.