Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Commit cf16bcc

Browse files
committed
Fix incorrect assumption about string stream buf
It is incorrect to assume the buffer associated with a string stream SFIO object has been initialized to all zeros. Fixes #1369
1 parent 232f08b commit cf16bcc

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/cmd/ksh93/edit/hexpand.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@ int hist_expand(Shell_t *shp, const char *ln, char **xp) {
143143
Histloc_t hl; // history location
144144
static Namval_t *np = NULL; // histchars variable
145145
static struct subst sb = {{NULL, NULL}}; // substition strings
146-
static Sfio_t *wm = NULL; // word match from !?string? event designator
147-
148-
if (!wm) wm = sfopen(NULL, NULL, "swr");
146+
static Sfio_t *wm; // word match from !?string? event designator
149147

148+
wm = sfopen(NULL, NULL, "swr");
150149
hc[0] = '!';
151150
hc[1] = '^';
152151
hc[2] = 0;
@@ -522,7 +521,13 @@ int hist_expand(Shell_t *shp, const char *ln, char **xp) {
522521

523522
if (c == 's') {
524523
// Preset old with match from !?string?.
525-
if (!sb.str[0] && wm) sb.str[0] = strdup(sfgetbuf(wm));
524+
if (!sb.str[0] && wm) {
525+
char *sbuf = sfgetbuf(wm);
526+
int n = sftell(wm);
527+
sb.str[0] = malloc(n + 1);
528+
sb.str[0][n] = '\0';
529+
memcpy(sb.str[0], sbuf, n);
530+
}
526531
cp = parse_subst(shp, cp, &sb);
527532
}
528533

0 commit comments

Comments
 (0)