9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Anthony Martin <ality@pbrane.org>
To: 9front@9front.org
Subject: Re: [9front] page epub support
Date: Thu, 19 Nov 2020 23:11:00 -0800	[thread overview]
Message-ID: <X7dsBNW6IEYpvnYe@alice> (raw)
In-Reply-To: <AF5EB9DB8255357561A60E1EAFD6422A@wopr.sciops.net>

# HG changeset patch
# User Anthony Martin <ality@pbrane.org>
# Date 1605855926 28800
#      Thu Nov 19 23:05:26 2020 -0800
# Node ID 3d602d959da7944e6844100b536f5442e0970370
# Parent  27e5e30d3c30b64a9fb6455d81f10083a526972f
awk: fix truncated input after fflush

Before the "native" awk work, a call to the fflush function resulted
in one or more calls to the APE fflush(2).

Calling fflush on a stream open for reading has different behavior
based on the environment: within APE, it's a no-op¹; on OpenBSD, it's
an error²; in musl, it depends on whether or not the underlying file
descriptor is seekable³; etc. I'm sure glibc is subtly different.

Now that awk uses libbio, things are different: calling Bflush(2) on a
file open for reading simply discards any data in the buffer. This
explains why we're seeing truncated input. When awk attempts to read
in the next record, there's nothing in the buffer and no more data to
read so it gets EOF and exits normally. Note that this behavior is not
documented in bio(2). It was added in the second edition but I haven't
figured out why or what depends on it.

The simple fix is to have awk only call Bflush on files that were
opened for writing. You could argue that this is the only correct
behavior according to the awk(1) manual and it is, in fact, how GNU
awk behaves⁴.

1. /sys/src/ape/lib/ap/stdio/fflush.c
2. https://cvsweb.openbsd.org/src/lib/libc/stdio/fflush.c?rev=1.9
3. https://git.musl-libc.org/cgit/musl/tree/src/stdio/fflush.c
4. https://git.savannah.gnu.org/cgit/gawk.git/tree/io.c#n1492

diff --git a/sys/src/cmd/awk/run.c b/sys/src/cmd/awk/run.c
--- a/sys/src/cmd/awk/run.c
+++ b/sys/src/cmd/awk/run.c
@@ -1707,6 +1707,8 @@
 	files[2].fp = &stderr;
 }

+#define writing(m) ((m) != LT && (m) != LE)
+
 Biobuf *openfile(int a, char *us)
 {
 	char *s = us;
@@ -1719,8 +1721,11 @@
 		if (files[i].fname && strcmp(s, files[i].fname) == 0) {
 			if (a == files[i].mode || (a==APPEND && files[i].mode==GT))
 				return files[i].fp;
-			if (a == FFLUSH)
+			if (a == FFLUSH) {
+				if(!writing(files[i].mode))
+					return nil;
 				return files[i].fp;
+			}
 		}
 	if (a == FFLUSH)	/* didn't find it, so don't create it! */
 		return nil;
@@ -1815,7 +1820,7 @@
 	int i;

 	for (i = 0; i < FOPEN_MAX; i++)
-		if (files[i].fp)
+		if (files[i].fp && writing(files[i].mode))
 			Bflush(files[i].fp);
 }



  reply	other threads:[~2020-11-20  7:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-16  8:31 umbraticus
2020-11-16 17:27 ` [9front] " ori
2020-11-17 12:21   ` qwx
2020-11-20  7:11     ` Anthony Martin [this message]
2020-11-20  8:28       ` umbraticus
2020-11-20 23:29         ` qwx
2020-11-20 15:43       ` ori
2020-11-17  2:42 ` sl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=X7dsBNW6IEYpvnYe@alice \
    --to=ality@pbrane.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).