[hackers] [sbase] tail: Don't print garbage when input contains no newlines. || Wolfgang Corcoran-Mathe

From: <git_AT_suckless.org>
Date: Sun, 30 Aug 2015 19:36:24 +0200 (CEST)

commit 438d2542e4bb731c65d10f908cd0519cf502750c
Author: Wolfgang Corcoran-Mathe <first.lord.of.teal_AT_gmail.com>
AuthorDate: Fri Aug 28 00:10:02 2015 -0400
Commit: sin <sin_AT_2f30.org>
CommitDate: Sun Aug 30 18:36:19 2015 +0100

    tail: Don't print garbage when input contains no newlines.
    
    getline(3) expects newline-terminated input. While glibc's
    implementation seems to catch unterminated input and zero the
    buffer, other versions (notably musl's) do not.
    
    This is a workaround. Garbage will still be read, but
    not printed.

diff --git a/tail.c b/tail.c
index 306bbc1..417289f 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -39,6 +39,7 @@ taketail(FILE *fp, const char *str, size_t n)
         Rune *r = NULL;
         char **ring = NULL;
         size_t i, j, *size = NULL;
+ int seenln = 0;
 
         if (!n)
                 return;
_AT_@ -47,7 +48,7 @@ taketail(FILE *fp, const char *str, size_t n)
                 ring = ecalloc(n, sizeof(*ring));
                 size = ecalloc(n, sizeof(*size));
 
- for (i = j = 0; getline(ring + i, size + i, fp) > 0; )
+ for (i = j = 0; getline(ring + i, size + i, fp) > 0; seenln = 1)
                         i = j = (i + 1) % n;
         } else {
                 r = ecalloc(n, sizeof(*r));
_AT_@ -59,7 +60,7 @@ taketail(FILE *fp, const char *str, size_t n)
                 eprintf("%s: read error:", str);
 
         do {
- if (ring && ring[j]) {
+ if (seenln && ring && ring[j]) {
                         fputs(ring[j], stdout);
                         free(ring[j]);
                 } else if (r) {
Received on Sun Aug 30 2015 - 19:36:24 CEST

This archive was generated by hypermail 2.3.0 : Sun Aug 30 2015 - 19:48:10 CEST