Re: PATH_MAX and LOGIN_NAME_MAX are not portable

Stéphane Glondu <[email protected]>
Newsgroups gmane.linux.debian.ports.hurd
Message-ID <[email protected]>
Control: tags -1 + patch

On Fri, 1 May 2015 15:26:10 +0200 Harald Dunkel <[email protected]> wrote:
> Package: mg
> Version: 20150323-2
> 
> mg fails to build on kfreebsd and hurd due to PATH_MAX and
> LOGIN_NAME_MAX. Sample:
> [...]
As of today (version 20240709-1), mg still FTBFS on hurd-any.

I had a look, and propose the attached patch. Although it adds a 
dependency on libgc, I made the patch minimal. Hard-coded buffer length 
could be removed in more places, but I'm waiting to see if there is 
interest in using libgc.

With this patch, I can compile the mg package on hurd-amd64 and it seems 
to work.


Cheers,

-- 
Stéphane
0001-Fix-FTBFS-on-hurd-any-Closes-783943.patch (text/x-patch, 9 KB)
From 5bea66fa192d94d27900a9e84ffae2a644bbc81f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Glondu?= <[email protected]>
Date: Sat, 9 Aug 2025 15:00:28 +0100
Subject: [PATCH] Fix FTBFS on hurd-any (Closes: #783943)

---
 debian/changelog                              |   8 +
 debian/control                                |   1 +
 ...-not-use-PATH_MAX-and-LOGIN_NAME_MAX.patch | 262 ++++++++++++++++++
 debian/patches/series                         |   1 +
 4 files changed, 272 insertions(+)
 create mode 100644 debian/patches/0001-Do-not-use-PATH_MAX-and-LOGIN_NAME_MAX.patch
 create mode 100644 debian/patches/series

diff --git a/debian/changelog b/debian/changelog
index 93c007e..f2d532a 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,11 @@
+mg (20240709-1.1) UNRELEASED; urgency=medium
+
+  * Non-maintainer upload.
+  * Fix FTBFS on hurd-any (Closes: #783943)
+    - add dependency on libgc-dev
+
+ -- Stéphane Glondu <[email protected]>  Sat, 09 Aug 2025 14:58:23 +0100
+
 mg (20240709-1) unstable; urgency=medium
 
   [ Harald Dunkel ]
diff --git a/debian/control b/debian/control
index 54bfaa2..6aa2c03 100644
--- a/debian/control
+++ b/debian/control
@@ -7,6 +7,7 @@ Build-Depends: debhelper (>= 13),
                debhelper-compat (= 13),
                libbsd-dev,
                libncurses-dev,
+               libgc-dev,
                pkgconf
 Standards-Version: 4.6.2
 Vcs-Browser: https://salsa.debian.org/debian/mg
diff --git a/debian/patches/0001-Do-not-use-PATH_MAX-and-LOGIN_NAME_MAX.patch b/debian/patches/0001-Do-not-use-PATH_MAX-and-LOGIN_NAME_MAX.patch
new file mode 100644
index 0000000..6b55def
--- /dev/null
+++ b/debian/patches/0001-Do-not-use-PATH_MAX-and-LOGIN_NAME_MAX.patch
@@ -0,0 +1,262 @@
+From: =?utf-8?q?St=C3=A9phane_Glondu?= <[email protected]>
+Date: Sat, 9 Aug 2025 14:16:20 +0100
+Subject: Do not use PATH_MAX and LOGIN_NAME_MAX
+
+This fixes compilation on the Hurd, at the cost of an extra dependency
+to libgc.
+
+Bug-Debian: https://bugs.debian.org/783943
+---
+ GNUmakefile |  2 +-
+ dired.c     | 44 +++++++++++++++++++++++++-------------------
+ fileio.c    | 28 ++++++++++++++++++----------
+ 3 files changed, 44 insertions(+), 30 deletions(-)
+
+diff --git a/GNUmakefile b/GNUmakefile
+index 9afa1b8..d01bc7f 100644
+--- a/GNUmakefile
++++ b/GNUmakefile
+@@ -48,7 +48,7 @@ CFLAGS+=	-g -Wall
+ CPPFLAGS=	-DREGEX
+ CPPFLAGS+=	-D_GNU_SOURCE
+ CPPFLAGS+=	$(BSD_CPPFLAGS)
+-LIBS=		$(CURSES_LIBS) $(BSD_LIBS)
++LIBS=		$(CURSES_LIBS) $(BSD_LIBS) -lgc
+ 
+ 
+ OBJS=	autoexec.o basic.o bell.o buffer.o cinfo.o dir.o display.o \
+diff --git a/dired.c b/dired.c
+index fc16c35..56a1f50 100644
+--- a/dired.c
++++ b/dired.c
+@@ -23,6 +23,7 @@
+ #include <stdlib.h>
+ #include <string.h>
+ #include <unistd.h>
++#include <gc.h>
+ 
+ #include "def.h"
+ #include "funmap.h"
+@@ -43,7 +44,7 @@ static int	 d_rename(int, int);
+ static int	 d_exec(int, struct buffer *, const char *, const char *, ...);
+ static int	 d_shell_command(int, int);
+ static int	 d_create_directory(int, int);
+-static int	 d_makename(struct line *, char *, size_t);
++static int	 d_makename(struct line *, char **);
+ static int	 d_warpdot(struct line *, int *);
+ static int	 d_forwpage(int, int);
+ static int	 d_backpage(int, int);
+@@ -354,9 +355,9 @@ d_findfile(int f, int n)
+ {
+ 	struct buffer	*bp;
+ 	int		 s;
+-	char		 fname[NFILEN];
++	char		*fname;
+ 
+-	if ((s = d_makename(curwp->w_dotp, fname, sizeof(fname))) == ABORT)
++	if ((s = d_makename(curwp->w_dotp, &fname)) == ABORT)
+ 		return (FALSE);
+ 	if (s == TRUE)
+ 		bp = dired_(fname);
+@@ -397,12 +398,12 @@ d_updirectory(int f, int n)
+ int
+ d_ffotherwindow(int f, int n)
+ {
+-	char		 fname[NFILEN];
++	char		*fname;
+ 	int		 s;
+ 	struct buffer	*bp;
+ 	struct mgwin	*wp;
+ 
+-	if ((s = d_makename(curwp->w_dotp, fname, sizeof(fname))) == ABORT)
++	if ((s = d_makename(curwp->w_dotp, &fname)) == ABORT)
+ 		return (FALSE);
+ 	if ((bp = (s ? dired_(fname) : findbuffer(fname))) == NULL)
+ 		return (FALSE);
+@@ -419,7 +420,7 @@ int
+ d_expunge(int f, int n)
+ {
+ 	struct line	*lp, *nlp;
+-	char		 fname[NFILEN], sname[NFILEN];
++	char		*fname, sname[NFILEN];
+ 	int		 tmp;
+ 
+ 	tmp = curwp->w_dotline;
+@@ -429,7 +430,7 @@ d_expunge(int f, int n)
+ 		curwp->w_dotline++;
+ 		nlp = lforw(lp);
+ 		if (llength(lp) && lgetc(lp, 0) == 'D') {
+-			switch (d_makename(lp, fname, sizeof(fname))) {
++			switch (d_makename(lp, &fname)) {
+ 			case ABORT:
+ 				dobeep();
+ 				ewprintf("Bad line in dired buffer");
+@@ -475,13 +476,13 @@ int
+ d_copy(int f, int n)
+ {
+ 	struct stat      statbuf;
+-	char		 frname[NFILEN], toname[NFILEN], sname[NFILEN];
++	char		*frname, toname[NFILEN], sname[NFILEN];
+ 	char		*topath, *bufp;
+ 	int		 ret;
+ 	size_t		 off;
+ 	struct buffer	*bp;
+ 
+-	if (d_makename(curwp->w_dotp, frname, sizeof(frname)) != FALSE) {
++	if (d_makename(curwp->w_dotp, &frname) != FALSE) {
+ 		dobeep();
+ 		ewprintf("Not a file");
+ 		return (FALSE);
+@@ -533,14 +534,14 @@ int
+ d_rename(int f, int n)
+ {
+ 	struct stat      statbuf;
+-	char		 frname[NFILEN], toname[NFILEN];
++	char		*frname, toname[NFILEN];
+ 	char		*topath, *bufp;
+ 	int		 ret;
+ 	size_t		 off;
+ 	struct buffer	*bp;
+ 	char		 sname[NFILEN];
+ 
+-	if (d_makename(curwp->w_dotp, frname, sizeof(frname)) != FALSE) {
++	if (d_makename(curwp->w_dotp, &frname) != FALSE) {
+ 		dobeep();
+ 		ewprintf("Not a file");
+ 		return (FALSE);
+@@ -604,7 +605,7 @@ reaper(int signo __attribute__((unused)))
+ int
+ d_shell_command(int f, int n)
+ {
+-	char		 command[512], fname[PATH_MAX], *bufp;
++	char		 command[512], *fname, *bufp;
+ 	struct buffer	*bp;
+ 	struct mgwin	*wp;
+ 	char		 sname[NFILEN];
+@@ -613,7 +614,7 @@ d_shell_command(int f, int n)
+ 	if (bclear(bp) != TRUE)
+ 		return (ABORT);
+ 
+-	if (d_makename(curwp->w_dotp, fname, sizeof(fname)) != FALSE) {
++	if (d_makename(curwp->w_dotp, &fname) != FALSE) {
+ 		dobeep();
+ 		ewprintf("bad line");
+ 		return (ABORT);
+@@ -836,19 +837,24 @@ refreshbuffer(struct buffer *bp)
+ }
+ 
+ static int
+-d_makename(struct line *lp, char *fn, size_t len)
++d_makename(struct line *lp, char **fn)
+ {
+ 	int	 start, nlen, ret;
+-	char	*namep;
++	char	*namep, *tmp;
+ 
+ 	if (d_warpdot(lp, &start) == FALSE)
+ 		return (ABORT);
+ 	namep = &lp->l_text[start];
+ 	nlen = llength(lp) - start;
+ 
+-	ret = snprintf(fn, len, "%s%.*s", curbp->b_fname, nlen, namep);
+-	if (ret < 0 || ret >= (int)len)
++	ret = asprintf(&tmp, "%s%.*s", curbp->b_fname, nlen, namep);
++	if (ret < 0)
+ 		return (ABORT); /* Name is too long. */
++	if ((*fn = GC_MALLOC(ret + 1)) != NULL)
++		memcpy(*fn, tmp, ret + 1);
++	free(tmp);
++	if (*fn == NULL)
++		return (ABORT);
+ 
+ 	/* Return TRUE if the entry is a directory. */
+ 	return ((lgetc(lp, 2) == 'd') ? TRUE : FALSE);
+@@ -914,9 +920,9 @@ d_backline (int f, int n)
+ int
+ d_filevisitalt (int f, int n)
+ {
+-	char	 fname[NFILEN];
++	char	*fname;
+ 
+-	if (d_makename(curwp->w_dotp, fname, sizeof(fname)) == ABORT)
++	if (d_makename(curwp->w_dotp, &fname) == ABORT)
+ 		return (FALSE);
+ 
+ 	return(do_filevisitalt(fname));
+diff --git a/fileio.c b/fileio.c
+index 1184ac8..3486fea 100644
+--- a/fileio.c
++++ b/fileio.c
+@@ -22,6 +22,7 @@
+ #include <stdlib.h>
+ #include <string.h>
+ #include <unistd.h>
++#include <gc.h>
+ 
+ #include "def.h"
+ #include "kbd.h"
+@@ -303,9 +304,9 @@ fbackupfile(const char *fn)
+ char *
+ adjustname(const char *fn, int slashslash)
+ {
+-	static char	 fnb[PATH_MAX];
+ 	const char	*cp, *ep = NULL;
+ 	char		*path;
++	char		*fnb, *result;
+ 
+ 	if (slashslash == TRUE) {
+ 		cp = fn + strlen(fn) - 1;
+@@ -323,11 +324,19 @@ adjustname(const char *fn, int slashslash)
+ 	if ((path = expandtilde(fn)) == NULL)
+ 		return (NULL);
+ 
+-	if (realpath(path, fnb) == NULL)
+-		(void)strlcpy(fnb, path, sizeof(fnb));
++	if ((fnb = realpath(path, NULL)) == NULL) {
++		int len = strlen(path) + 1;
++		if ((result = GC_MALLOC(len)) != NULL)
++			(void)strlcpy(result, path, len);
++	} else {
++		int len = strlen(fnb) + 1;
++		if ((result = GC_MALLOC(len)) != NULL)
++			(void)strlcpy(result, fnb, len);
++		free(fnb);
++	}
+ 
+ 	free(path);
+-	return (fnb);
++	return (result);
+ }
+ 
+ /*
+@@ -713,7 +722,7 @@ expandtilde(const char *fn)
+ 	struct passwd	*pw;
+ 	struct stat	 statbuf;
+ 	const char	*cp;
+-	char		 user[LOGIN_NAME_MAX], path[NFILEN];
++	char		 path[NFILEN];
+ 	char		*ret;
+ 	size_t		 ulen, plen;
+ 
+@@ -728,17 +737,16 @@ expandtilde(const char *fn)
+ 	if (cp == NULL)
+ 		cp = fn + strlen(fn); /* point to the NUL byte */
+ 	ulen = cp - &fn[1];
+-	if (ulen >= sizeof(user)) {
+-		if ((ret = strndup(fn, NFILEN)) == NULL)
+-			return (NULL);
+-		return(ret);
+-	}
+ 	if (ulen == 0) /* ~/ or ~ */
+ 		pw = getpwuid(geteuid());
+ 	else { /* ~user/ or ~user */
++		char *user;
++		if ((user = malloc(ulen + 1)) == NULL)
++			return (NULL);
+ 		memcpy(user, &fn[1], ulen);
+ 		user[ulen] = '\0';
+ 		pw = getpwnam(user);
++		free(user);
+ 	}
+ 	if (pw != NULL) {
+ 		plen = strlcpy(path, pw->pw_dir, sizeof(path));
diff --git a/debian/patches/series b/debian/patches/series
new file mode 100644
index 0000000..23f6ad2
--- /dev/null
+++ b/debian/patches/series
@@ -0,0 +1 @@
+0001-Do-not-use-PATH_MAX-and-LOGIN_NAME_MAX.patch
-- 
2.47.2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.