This does not fix all "leaks" that may be detected. In particular during program initialization copies of a few strings and such are made and those copies may persist. However, those are benign as the program does not continuously leak memory. Furthermore, fixing all of them would be substantially more effort than its worth -- in fact the code to "fix" this would grow the text size of the program by more than the couple of bytes of leaked memory!
Patch from Garrett D'Amore https://github.com/gdamore/less-fork/commit/b2f362eb4dea171265ab2aff059cbbeca075664e Index: screen.c =================================================================== RCS file: /cvs/src/usr.bin/less/screen.c,v retrieving revision 1.24 diff -u -r1.24 screen.c --- screen.c 8 Jul 2016 15:23:44 -0000 1.24 +++ screen.c 4 Jan 2017 17:36:00 -0000 @@ -396,9 +396,9 @@ if (*sc_move == '\0') { t2 = ""; } else { - t2 = estrdup(tparm(sc_move, 0, 0, 0, 0, 0, 0, 0, 0, 0)); + t2 = tparm(sc_move, 0, 0, 0, 0, 0, 0, 0, 0, 0); } - sc_home = cheaper(t1, t2, "|\b^"); + sc_home = estrdup(cheaper(t1, t2, "|\b^")); /* * Choose between using "ll" and "cm" ("lower left" and "cursor move") @@ -410,10 +410,9 @@ if (*sc_move == '\0') { t2 = ""; } else { - t2 = estrdup(tparm(sc_move, sc_height-1, - 0, 0, 0, 0, 0, 0, 0, 0)); + t2 = tparm(sc_move, sc_height-1, 0, 0, 0, 0, 0, 0, 0, 0); } - sc_lower_left = cheaper(t1, t2, "\r"); + sc_lower_left = estrdup(cheaper(t1, t2, "\r")); /* * Get carriage return string. Index: search.c =================================================================== RCS file: /cvs/src/usr.bin/less/search.c,v retrieving revision 1.18 diff -u -r1.18 search.c --- search.c 17 Sep 2016 15:06:41 -0000 1.18 +++ search.c 4 Jan 2017 17:36:00 -0000 @@ -92,9 +92,10 @@ static int set_pattern(struct pattern_info *info, char *pattern, int search_type) { - if (pattern == NULL) + if (pattern == NULL) { + uncompile_pattern(&info->compiled); info->compiled = NULL; - else if (compile_pattern(pattern, search_type, &info->compiled) < 0) + } else if (compile_pattern(pattern, search_type, &info->compiled) < 0) return (-1); /* Pattern compiled successfully; save the text too. */ free(info->text);