]> git.cameronkatri.com Git - mandoc.git/blobdiff - main.c
In man(1) mode, properly clean up the resn[] result array
[mandoc.git] / main.c
diff --git a/main.c b/main.c
index a01b0cb8c259f25c33fefd60b9babe8566c4d70a..c23a1a7fdf0d4a352458f998e5a6a774ede0b41b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.357 2021/09/04 12:52:57 schwarze Exp $ */
+/* $Id: main.c,v 1.359 2021/10/04 20:24:06 schwarze Exp $ */
 /*
  * Copyright (c) 2010-2012, 2014-2021 Ingo Schwarze <schwarze@openbsd.org>
  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
@@ -132,7 +132,7 @@ main(int argc, char *argv[])
        struct mparse   *mp;            /* Opaque parser object. */
        const char      *conf_file;     /* -C: alternate config file. */
        const char      *os_s;          /* -I: Operating system for display. */
-       const char      *progname, *sec;
+       const char      *progname, *sec, *ep;
        char            *defpaths;      /* -M: override manpaths. */
        char            *auxpaths;      /* -m: additional manpaths. */
        char            *oarg;          /* -O: output option string. */
@@ -516,6 +516,9 @@ main(int argc, char *argv[])
                                memcpy(res + ressz, resn,
                                    sizeof(*resn) * resnsz);
                                ressz += resnsz;
+                               free(resn);
+                               resn = NULL;
+                               resnsz = 0;
                                continue;
                        }
 
@@ -536,11 +539,16 @@ main(int argc, char *argv[])
                                        sec++; /* Prefer without suffix. */
                                if (*sec != '/')
                                        prio += 10; /* Wrong dir name. */
-                               if (search.sec != NULL &&
-                                   (strlen(sec) <= ssz  + 3 ||
-                                    strcmp(sec + strlen(sec) - ssz,
-                                     search.sec) != 0))
-                                       prio += 20; /* Wrong file ext. */
+                               if (search.sec != NULL) {
+                                       ep = strchr(sec, '\0');
+                                       if (ep - sec > 3 &&
+                                           strncmp(ep - 3, ".gz", 3) == 0)
+                                               ep -= 3;
+                                       if ((size_t)(ep - sec) < ssz + 3 ||
+                                           strncmp(ep - ssz, search.sec,
+                                            ssz) != 0)      /* Wrong file */
+                                               prio += 20;  /* extension. */
+                               }
                                if (prio >= best_prio)
                                        continue;
                                best_prio = prio;
@@ -549,6 +557,10 @@ main(int argc, char *argv[])
                        res = mandoc_reallocarray(res, ressz + 1,
                            sizeof(*res));
                        memcpy(res + ressz++, resn + ib, sizeof(*resn));
+                       memset(resn + ib, 0, sizeof(*resn));
+                       mansearch_free(resn, resnsz);
+                       resn = NULL;
+                       resnsz = 0;
                }
 
        /* apropos(1), whatis(1): Process the full search expression. */