When PATH is absent from the environment (for example, when running
under `env -i`), getenv("PATH") returns NULL.  xsetenv() passed that
NULL to setenv(), searchpath() passed it to strchr(), and relocatep()
then used a NULL curr_prefix.

Set GROFF_PATH__ only when PATH is present, treat a null search path
as "not found" in searchpath(), and fall back to the compiled-in path
in relocatep() when the current prefix cannot be determined.

This fixes the SIGSEGV seen when running pic2graph, eqn2graph, mmroff,
or a bare-name groff invocation with PATH unset.
---
 ChangeLog                      | 13 +++++++++++++
 src/libs/libgroff/relocate.cpp |  4 ++++
 src/roff/groff/groff.cpp       |  3 ++-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index aaf467c99..a9a1e8530 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2026-12-03  zhuofeng <[email protected]>
+
+       [groff,libgroff]: Handle unset PATH without crashing.
+
+       Fix a segmentation fault when PATH is not set in the environment
+       and groff is invoked by a name without a directory component.
+       * src/libs/libgroff/relocate.cpp (searchpath): Return early when
+       the search path is null.
+       (relocatep): Fall back to the compiled-in installation prefix if
+       the current prefix cannot be determined.
+       * src/roff/groff/groff.cpp (main): Do not pass a null value to
+       setenv(3).
+
 2026-12-01  G. Branden Robinson <[email protected]>
 
        * src/roff/troff/env.cpp (print_environment_request): Drop colon
diff --git a/src/libs/libgroff/relocate.cpp b/src/libs/libgroff/relocate.cpp
index 58da6b1dd..7b1cc8cdd 100644
--- a/src/libs/libgroff/relocate.cpp
+++ b/src/libs/libgroff/relocate.cpp
@@ -81,6 +81,8 @@ char *searchpath(const char *name, const char *pathp)
   char *path;
   if (!name || !*name)
     return 0;
+  if (!pathp)
+    return 0;
 #if DEBUG
   fprintf(stderr, "searchpath: pathp: '%s'\n", pathp);
   fprintf(stderr, "searchpath: trying '%s'\n", name);
@@ -220,6 +222,8 @@ char *relocatep(const char *path)
 #endif
   if (!curr_prefix)
     set_current_prefix();
+  if (!curr_prefix)
+    return strsave(path);
   if (strncmp(INSTALLPATH, path, INSTALLPATHLEN))
     return strsave(path);
   char *relative_path = (char *)path + INSTALLPATHLEN;
diff --git a/src/roff/groff/groff.cpp b/src/roff/groff/groff.cpp
index 45ae102d9..b176517de 100644
--- a/src/roff/groff/groff.cpp
+++ b/src/roff/groff/groff.cpp
@@ -534,7 +534,8 @@ int main(int argc, char **argv)
     // we save the original path in GROFF_PATH__ and put it into the
     // environment -- troff will pick it up later.
     char *path = getenv("PATH");
-    xsetenv("GROFF_PATH__", path, 1 /* overwrite */);
+    if (path != 0 /* nullptr */)
+      xsetenv("GROFF_PATH__", path, 1 /* overwrite */);
     char *binpath = getenv("GROFF_BIN_PATH");
     string newpath;
     if ((binpath != 0 /* nullptr */ && (*binpath != '\0')))
-- 
2.43.0


Reply via email to