Fixing a bug in vi

While I was studying at university between 2011-2014, I used OpenBSD as my main work and development environment. My Lenovo X61s laptop had trouble with suspend while docked on Linux, but OpenBSD worked fine. So I ran OpenBSD. Occasionally, I would use the vi editor (aka nvi) for writing emails and reports — there was something in its crude simplicity.

I remember typing an email one morning at the university’s hallway when vi just crashed. I was stunned. vi isn’t exactly a piece of software that gets features (bugs) added to it on yearly basis nor is it a program void of users. vi was crashing in the year 2013.

When you hit a bug, you try to reproduce it. I built vi with debug symbols and tried to reproduce the issue. This wasn’t a trivial task. I remember spending the entire afternoon trying to trace my steps backwards on what I had done. In the evening I had traced the bug to word-erase (^W). In some cases hitting ^W would result in off-by-one and crash the editor.

The next day I created the patch and submitted it to the openbsd-tech mailing list. While writing the patch I started wondering how old was this issue. Luckily the history of nvi was available as a Git repo and I was able to trace the issue to a commit from 13th of April 1995!

After a round of review, a second off-by-one was spotted in the same handler. The version 2 of the patch was merged a few days later.