Index | Thread | Search

From:
"Omar Polo" <op@omarpolo.com>
Subject:
mg: avoid accessing curbp in ttinit()
To:
tech@openbsd.org
Cc:
Han Boetes <hboetes@gmail.com>
Date:
Mon, 23 Mar 2026 11:56:56 +0100

Download raw body.

Thread
  • Omar Polo:

    mg: avoid accessing curbp in ttinit()

Hello,

I got a bug report from Han Boetes while he was porting mg to NetBSD, mg
would crash in ttinit().

Due to some peculiarities in howe they have terminfo entries for xterm,
we enter in a branch where we dereference curbp which is NULL during
ttinit().

In main we do

	vtinit();		/* Virtual terminal.		*/
	[...]
	edinit(bp);		/* Buffers, windows.		*/

vtinit() calls ttinit(), but curbp (and curwp) are set in edinit(), so
they can only be NULL during vtinit().

It doesn't seem trivial to me to swap these, since edinit() is going to
touch the tty layer (bfind() calls bnew() which calls into the tty code)
and I don't currently have the time to review properly and/or adjust so
that it could happen.

As of now, even if we have the b_nlchr knob, it's always set to "\n"
(see buffer.c:bnew(), around the end of the function, grep for "use unix
default") and never touched, only inspected, so that I think it's safe
for now to hardcode "\n" as scroll_fwd sequence.  I don't have plans in
the near term to port mg to windows or to classic mac where the nlchr
sequence would need to be changed :-)

okay?

diff /home/op/w/mg
path + /home/op/w/mg
commit - 56d54b2a7cc014527ea8ba0e2bacd270917079ed
blob - b939399da5798f745f37765cd4205174a0ed2801
file + tty.c
--- tty.c
+++ tty.c
@@ -84,7 +84,7 @@ ttinit(void)
 		/* this is what GNU Emacs does */
 		scroll_fwd = parm_down_cursor;
 		if (scroll_fwd == NULL || *scroll_fwd == '\0')
-			scroll_fwd = curbp->b_nlchr;
+			scroll_fwd = "\n";
 	}
 
 	if (cursor_address == NULL || cursor_up == NULL)