From owner-svn-src-all@freebsd.org Wed Jan 11 12:53:32 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 A429BCA8CCF; Wed, 11 Jan 2017 12:53:32 +0000 (UTC) (envelope-from pluknet@gmail.com) Received: from mail-qk0-x233.google.com (mail-qk0-x233.google.com [IPv6:2607:f8b0:400d:c09::233]) (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 5C9D8167C; Wed, 11 Jan 2017 12:53:32 +0000 (UTC) (envelope-from pluknet@gmail.com) Received: by mail-qk0-x233.google.com with SMTP id u25so591144645qki.2; Wed, 11 Jan 2017 04:53:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=VLtTGN6SQis6cnoNnQy/ZAIp0Fi7AKYRBAORyQqSHEs=; b=Aqca+DwZkP6MGZ/vd2Z866vW88B0gH9kStzzP7bOYrq1N538aLe6+GJc3Y1vwMGUW5 uwKrtclaCKb8JUCWiEHQpv3gNDuiDG9vBNG5VA/74QbncpGQVLjG8t+v6q3x0gOeiBWZ U1v3FdfQAHNKOQifMr3aVsTymFDxbjOUmnZufTUFl4czLy4jcuJ6HZubv5etAVRG0dBH esmrU20IVm6iXC9ptfDNtj4eaS+9D4XLJnnho3pAzalk7a456hmJ0S7GF7N2Su0iPAq8 oqB0DHstzW4T62y2nETWLQicnF6tSL8yBqhSHyWQ2z3bNvPQ1g0tP7Vg1u/B+mopa3Cm 2ANg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=VLtTGN6SQis6cnoNnQy/ZAIp0Fi7AKYRBAORyQqSHEs=; b=hW+jptfeKY3An1NbJ+ruOBjshUZKZc9dQNqHTsPz3wxL6+EZRVW7BvotKLF8bpUbjz vRgwxab3HhvSfr4weT41nLEFk06smDFxz+K/z9d3spLDVe4OHQZuT0JzV0AM0E8WMQWw HKJljA6CQYFtbZocRSpky0muqx7oR+qyNyMAzhbLFVpHmDYWTxUm7pYGgT5r/UxynTYf gv3c3Re/DXBN0UIueFVAZTtpSlNd+aMB4pwjSH6vv0LW6t0xeqnmCNLZqBchEBCgfQBV 0JN0KUDffRVfcZH7rALaeKFKwsagDhJTINiaC8DFcMAXhOVqLbPz7/NSUBJmlux5hh05 sL1g== X-Gm-Message-State: AIkVDXIA3gJoLSHYqnMNgttGeq7UlYfshlr1Bq/Rcil83YdxU2VihXegRAHqmFFvvpgHtLJHozFK+SS/irwFJw== X-Received: by 10.55.159.206 with SMTP id i197mr7179303qke.175.1484139211363; Wed, 11 Jan 2017 04:53:31 -0800 (PST) MIME-Version: 1.0 Received: by 10.12.150.188 with HTTP; Wed, 11 Jan 2017 04:53:31 -0800 (PST) In-Reply-To: <201701102043.v0AKhWuv073169@repo.freebsd.org> References: <201701102043.v0AKhWuv073169@repo.freebsd.org> From: Sergey Kandaurov Date: Wed, 11 Jan 2017 15:53:31 +0300 Message-ID: Subject: Re: svn commit: r311895 - in head: etc/mtree usr.bin/tail usr.bin/tail/tests To: Alan Somers Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Content-Type: text/plain; charset=UTF-8 X-Content-Filtered-By: Mailman/MimeDel 2.1.23 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 12:53:32 -0000 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