From: Peter Stephenson <p.stephenson@samsung.com>
To: zsh-workers@zsh.org
Subject: Re: zsh 5.3.1 crashes on completion
Date: Tue, 16 May 2017 12:05:27 +0100 [thread overview]
Message-ID: <20170516120527.572a5cc9@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <20170516104831.14aa23c6@pwslap01u.europe.root.pri>
On Tue, 16 May 2017 10:48:31 +0100
Peter Stephenson <p.stephenson@samsung.com> wrote:
> zcontext_save() in loop() will run, since this isn't a top-level loop,
> which will save and clear chline. So it should be the case the the loop
> we have for fc has a clean history state. So I wonder if something else to do
> with the state (that maybe we should be saving and clearing at the same
> time?) is screwy owing to not having finished the top-level history
> line.
(BTW I needed to use standard malloc for this, it wasn't showing up with
zsh's own.)
Hmm... I think the bad free comes because curline, which points to
chline, is part of the history ring and is being freed when we free
that. So at hend() chline is already free.
Background: curline is a static structure with direct references to the
current history line. We insert it into the history ring for
convenience in searching as necessary. However, its memory status is
different from that of other history ring entries, which is obviously
a hostage to fortune.
This attempts to be smarter about linking curline into the history ring
and unlinking it. This seems to have good effects, but definitely needs
a further look over.
pws
diff --git a/Src/hashtable.c b/Src/hashtable.c
index ba5faad..c34744c 100644
--- a/Src/hashtable.c
+++ b/Src/hashtable.c
@@ -1448,6 +1448,7 @@ freehistdata(Histent he, int unlink)
if (!(he->node.flags & (HIST_DUP | HIST_TMPSTORE)))
removehashnode(histtab, he->node.nam);
+ DPUTS(he->node.nam == chline, "Attempt to free chline in history data");
zsfree(he->node.nam);
if (he->nwords)
zfree(he->words, he->nwords*2*sizeof(short));
diff --git a/Src/hist.c b/Src/hist.c
index 350688d..636c315 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -87,6 +87,9 @@ mod_export zlong curhist;
/**/
struct histent curline;
+/***/
+int curline_linked;
+
/* current line count of allocated history entries */
/**/
@@ -261,6 +264,9 @@ hist_context_save(struct hist_stack *hs, int toplevel)
*/
hs->cstack = cmdstack;
hs->csp = cmdsp;
+ hs->curline_linked = curline_linked;
+
+ unlinkcurline();
stophist = 0;
chline = NULL;
@@ -300,6 +306,10 @@ hist_context_restore(const struct hist_stack *hs, int toplevel)
zfree(cmdstack, CMDSTACKSZ);
cmdstack = hs->cstack;
cmdsp = hs->csp;
+ unlinkcurline();
+ curline_linked = hs->curline_linked;
+ if (curline_linked)
+ linkcurline();
}
/*
@@ -990,6 +1000,7 @@ nohwe(void)
/* these functions handle adding/removing curline to/from the hist_ring */
+/**/
static void
linkcurline(void)
{
@@ -1002,11 +1013,15 @@ linkcurline(void)
hist_ring = &curline;
}
curline.histnum = ++curhist;
+ curline_linked = 1;
}
+/**/
static void
unlinkcurline(void)
{
+ if (!curline_linked)
+ return;
curline.up->down = curline.down;
curline.down->up = curline.up;
if (hist_ring == &curline) {
@@ -1016,6 +1031,7 @@ unlinkcurline(void)
hist_ring = curline.up;
}
curhist--;
+ curline_linked = 0;
}
/* initialize the history mechanism */
@@ -1035,6 +1051,7 @@ hbegin(int dohist)
stophist = (!interact || unset(SHINSTDIN)) ? 2 : 0;
else
stophist = 0;
+ DPUTS(chline != NULL, "chline set at start of history");
/*
* pws: We used to test for "|| (inbufflags & INP_ALIAS)"
* in this test, but at this point we don't have input
@@ -1292,11 +1309,10 @@ putoldhistentryontop(short keep_going)
Histent
prepnexthistent(void)
{
- Histent he;
- int curline_in_ring = hist_ring == &curline;
+ Histent he;
+ int relink_curline = curline_linked;
- if (curline_in_ring)
- unlinkcurline();
+ unlinkcurline();
if (hist_ring && hist_ring->node.flags & HIST_TMPSTORE) {
curhist--;
freehistnode(&hist_ring->node);
@@ -1320,7 +1336,7 @@ prepnexthistent(void)
he = hist_ring;
}
he->histnum = ++curhist;
- if (curline_in_ring)
+ if (relink_curline)
linkcurline();
return he;
}
@@ -1395,8 +1411,7 @@ hend(Eprog prog)
queue_signals();
if (histdone & HISTFLAG_SETTY)
settyinfo(&shttyinfo);
- if (!(histactive & HA_NOINC))
- unlinkcurline();
+ unlinkcurline();
if (histactive & HA_NOINC) {
zfree(chline, hlinesz);
zfree(chwords, chwordlen*sizeof(short));
@@ -3624,7 +3639,7 @@ int
pushhiststack(char *hf, zlong hs, zlong shs, int level)
{
struct histsave *h;
- int curline_in_ring = (histactive & HA_ACTIVE) && hist_ring == &curline;
+ int relink_curline = curline_linked;
if (histsave_stack_pos == histsave_stack_size) {
histsave_stack_size += 5;
@@ -3632,8 +3647,7 @@ pushhiststack(char *hf, zlong hs, zlong shs, int level)
histsave_stack_size * sizeof (struct histsave));
}
- if (curline_in_ring)
- unlinkcurline();
+ unlinkcurline();
h = &histsave_stack[histsave_stack_pos++];
@@ -3668,7 +3682,7 @@ pushhiststack(char *hf, zlong hs, zlong shs, int level)
savehistsiz = shs;
inithist(); /* sets histtab */
- if (curline_in_ring)
+ if (relink_curline)
linkcurline();
return histsave_stack_pos;
@@ -3680,13 +3694,12 @@ int
pophiststack(void)
{
struct histsave *h;
- int curline_in_ring = (histactive & HA_ACTIVE) && hist_ring == &curline;
+ int relink_curline = curline_linked;
if (histsave_stack_pos == 0)
return 0;
- if (curline_in_ring)
- unlinkcurline();
+ unlinkcurline();
deletehashtable(histtab);
zsfree(lasthist.text);
@@ -3709,7 +3722,7 @@ pophiststack(void)
histsiz = h->histsiz;
savehistsiz = h->savehistsiz;
- if (curline_in_ring)
+ if (relink_curline)
linkcurline();
return histsave_stack_pos + 1;
diff --git a/Src/zsh.h b/Src/zsh.h
index 22f73f8..405b274 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -2931,6 +2931,7 @@ struct hist_stack {
void (*addtoline) _((int));
unsigned char *cstack;
int csp;
+ int curline_linked;
};
/*
next prev parent reply other threads:[~2017-05-16 11:05 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-11 2:35 ChenYao
2017-05-11 21:19 ` Bart Schaefer
2017-05-12 16:16 ` Daniel Shahaf
2017-05-12 16:19 ` Bart Schaefer
2017-05-13 2:02 ` ChenYao
2017-05-13 18:23 ` Bart Schaefer
2017-05-15 9:28 ` Peter Stephenson
2017-05-15 20:33 ` Bart Schaefer
2017-05-16 9:48 ` Peter Stephenson
2017-05-16 11:05 ` Peter Stephenson [this message]
2017-05-17 18:18 ` Bart Schaefer
2017-05-17 18:41 ` Peter Stephenson
2017-05-25 16:01 ` Jun T.
2017-05-25 18:05 ` Bart Schaefer
2017-05-26 9:19 ` Peter Stephenson
2017-05-26 23:56 ` Bart Schaefer
2017-05-29 4:04 ` Jun T.
2017-05-17 18:07 ` Bart Schaefer
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=20170516120527.572a5cc9@pwslap01u.europe.root.pri \
--to=p.stephenson@samsung.com \
--cc=zsh-workers@zsh.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.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
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).