From a754ce08134f92c6f07e28ce7ec95f33135f2074 Mon Sep 17 00:00:00 2001
From: Kurtis Rader <krader@skepticism.us>
Date: Thu, 7 Nov 2019 17:48:40 -0800
Subject: [PATCH] Fix hist_nearend()
 
The original code, prior to the fix for issue #1271, called sfreserve()
with just the SF_LOCKR. While fixing the core issue of issue #1271 I
noticed the buffer did not have a read/write mode. Which is wrong. What
I didn't realize was that is equivalent to read+write. I incorrectly
included SF_WRITE rather than SF_RDWR.
 
Fixes #1432
 
(cherry picked from commit b2e48a99ec6e2622c9e653bc64f334b2bdcff0f9)
 
---
 CHANGELOG.md                 | 7 +++++++
 src/cmd/ksh93/edit/history.c | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
 
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0f1fcaf3..0e38b780 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,10 @@
+# ksh 2020.0.1
+
+## Notable fixes and improvements
+
+- Fix `history` command behavior when the *~/.sh_history* file has
+  specific content (issue #1432).
+
 # ksh 2020.0.0
 
 This documents changes since the AST code, which includes the `ksh` program,
diff --git a/src/cmd/ksh93/edit/history.c b/src/cmd/ksh93/edit/history.c
index 50ac06a6..7c207c26 100644
--- a/src/cmd/ksh93/edit/history.c
+++ b/src/cmd/ksh93/edit/history.c
@@ -365,7 +365,7 @@ static int hist_nearend(History_t *hp, Sfio_t *iop, off_t size) {
     // Skip to marker command and return the number. Numbering commands occur after a null and begin
     // with HIST_CMDNO.
     while (true) {
-        cp = buff = (unsigned char *)sfreserve(iop, SF_UNBOUND, SF_LOCKR | SF_WRITE);
+        cp = buff = (unsigned char *)sfreserve(iop, SF_UNBOUND, SF_LOCKR | SF_RDWR);
         if (!cp) break;
 
         n = sfvalue(iop);
-- 
2.19.1
 
