summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormartijn <martijn@openbsd.org>2018-11-20 07:02:23 +0000
committermartijn <martijn@openbsd.org>2018-11-20 07:02:23 +0000
commitc722fcade02d60d69616c62c4bc52ef25202ac56 (patch)
tree0d5c116247d6302ece1f40cb513b428631a8a989
parentmove a magic constant into a magic define (diff)
downloadwireguard-openbsd-c722fcade02d60d69616c62c4bc52ef25202ac56.tar.xz
wireguard-openbsd-c722fcade02d60d69616c62c4bc52ef25202ac56.zip
Fix the case where the recursion detection isn't reset when the command is
interrupted. Lots of back and forth with anton@ OK jca@, tb@, anton@
-rw-r--r--bin/ksh/history.c24
-rw-r--r--bin/ksh/main.c3
-rw-r--r--bin/ksh/sh.h3
3 files changed, 21 insertions, 9 deletions
diff --git a/bin/ksh/history.c b/bin/ksh/history.c
index 67735c90964..1448f8abc58 100644
--- a/bin/ksh/history.c
+++ b/bin/ksh/history.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: history.c,v 1.80 2018/01/15 22:30:38 jca Exp $ */
+/* $OpenBSD: history.c,v 1.81 2018/11/20 07:02:23 martijn Exp $ */
/*
* command history
@@ -48,6 +48,8 @@ static uint32_t line_co;
static struct stat last_sb;
+static volatile sig_atomic_t c_fc_depth;
+
int
c_fc(char **wp)
{
@@ -58,9 +60,8 @@ c_fc(char **wp)
int optc, ret;
char *first = NULL, *last = NULL;
char **hfirst, **hlast, **hp;
- static int depth;
- if (depth != 0) {
+ if (c_fc_depth != 0) {
bi_errorf("history function called recursively");
return 1;
}
@@ -146,9 +147,9 @@ c_fc(char **wp)
hist_get_newest(false);
if (!hp)
return 1;
- depth++;
+ c_fc_depth++;
ret = hist_replace(hp, pat, rep, gflag);
- depth--;
+ c_fc_reset();
return ret;
}
@@ -268,13 +269,22 @@ c_fc(char **wp)
shf_close(shf);
*xp = '\0';
strip_nuls(Xstring(xs, xp), Xlength(xs, xp));
- depth++;
+ c_fc_depth++;
ret = hist_execute(Xstring(xs, xp));
- depth--;
+ c_fc_reset();
return ret;
}
}
+/* Reset the c_fc depth counter.
+ * Made available for when an fc call is interrupted.
+ */
+void
+c_fc_reset(void)
+{
+ c_fc_depth = 0;
+}
+
/* Save cmd in history, execute cmd (cmd gets trashed) */
static int
hist_execute(char *cmd)
diff --git a/bin/ksh/main.c b/bin/ksh/main.c
index 63508d4dc84..e74c12f3a37 100644
--- a/bin/ksh/main.c
+++ b/bin/ksh/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.95 2018/11/17 18:14:58 deraadt Exp $ */
+/* $OpenBSD: main.c,v 1.96 2018/11/20 07:02:23 martijn Exp $ */
/*
* startup, main loop, environments and error handling
@@ -557,6 +557,7 @@ shell(Source *volatile s, volatile int toplevel)
case LERROR:
case LSHELL:
if (interactive) {
+ c_fc_reset();
if (i == LINTR)
shellf("\n");
/* Reset any eof that was read as part of a
diff --git a/bin/ksh/sh.h b/bin/ksh/sh.h
index cf2c6c3a595..2efa875bb80 100644
--- a/bin/ksh/sh.h
+++ b/bin/ksh/sh.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: sh.h,v 1.73 2018/05/18 13:25:20 benno Exp $ */
+/* $OpenBSD: sh.h,v 1.74 2018/11/20 07:02:23 martijn Exp $ */
/*
* Public Domain Bourne/Korn shell
@@ -449,6 +449,7 @@ void hist_init(Source *);
void hist_finish(void);
void histsave(int, const char *, int);
int c_fc(char **);
+void c_fc_reset(void);
void sethistcontrol(const char *);
void sethistsize(int);
void sethistfile(const char *);