git: 834a8fa1694d - main - enable to configure the locate path length at compile time

From: Wolfram Schneider <wosch_at_FreeBSD.org>
Date: Mon, 31 Jan 2022 17:36:19 UTC
The branch main has been updated by wosch:

URL: https://cgit.FreeBSD.org/src/commit/?id=834a8fa1694db39a356f1732d78f39ff76da422c

commit 834a8fa1694db39a356f1732d78f39ff76da422c
Author:     Wolfram Schneider <wosch@FreeBSD.org>
AuthorDate: 2022-01-31 17:32:46 +0000
Commit:     Wolfram Schneider <wosch@FreeBSD.org>
CommitDate: 2022-01-31 17:32:46 +0000

    enable to configure the locate path length at compile time
    
    The length has not changed and is 1024 chars (equals PATH_MAX).
    
    PR: 201243
    Submitted by: Willem Jan Withagen <wjw@digiware.nl>
---
 usr.bin/locate/bigram/locate.bigram.c |  5 ++---
 usr.bin/locate/code/locate.code.c     |  6 +++---
 usr.bin/locate/locate/fastfind.c      | 14 +++++++-------
 usr.bin/locate/locate/locate.h        |  6 ++++++
 usr.bin/locate/locate/util.c          | 11 +++++------
 5 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/usr.bin/locate/bigram/locate.bigram.c b/usr.bin/locate/bigram/locate.bigram.c
index d15f8ce170e8..a654448680a3 100644
--- a/usr.bin/locate/bigram/locate.bigram.c
+++ b/usr.bin/locate/bigram/locate.bigram.c
@@ -59,11 +59,10 @@ static char sccsid[] = "@(#)locate.bigram.c	8.1 (Berkeley) 6/6/93";
 #include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/param.h>			/* for MAXPATHLEN */
 #include "locate.h"
 
-u_char buf1[MAXPATHLEN] = " ";
-u_char buf2[MAXPATHLEN];
+u_char buf1[LOCATE_PATH_MAX] = " ";
+u_char buf2[LOCATE_PATH_MAX];
 unsigned long bigram[UCHAR_MAX + 1][UCHAR_MAX + 1];
 
 int
diff --git a/usr.bin/locate/code/locate.code.c b/usr.bin/locate/code/locate.code.c
index 9da78d55e622..5263d9ee8fb0 100644
--- a/usr.bin/locate/code/locate.code.c
+++ b/usr.bin/locate/code/locate.code.c
@@ -1,7 +1,7 @@
 /*
  * SPDX-License-Identifier: BSD-3-Clause
  *
- * Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
+ * Copyright (c) 1995-2022 Wolfram Schneider <wosch@FreeBSD.org>
  * Copyright (c) 1989, 1993
  *	The Regents of the University of California.  All rights reserved.
  *
@@ -102,8 +102,8 @@ static char sccsid[] = "@(#)locate.code.c	8.1 (Berkeley) 6/6/93";
 
 #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
 
-u_char buf1[MAXPATHLEN] = " ";	
-u_char buf2[MAXPATHLEN];
+u_char buf1[LOCATE_PATH_MAX] = " ";
+u_char buf2[LOCATE_PATH_MAX];
 u_char bigrams[BGBUFSIZE + 1] = { 0 };
 
 #define LOOKUP 1 /* use a lookup array instead a function, 3x faster */
diff --git a/usr.bin/locate/locate/fastfind.c b/usr.bin/locate/locate/fastfind.c
index 9a3324e20e44..8cc9af924531 100644
--- a/usr.bin/locate/locate/fastfind.c
+++ b/usr.bin/locate/locate/fastfind.c
@@ -49,7 +49,7 @@ statistic (fp, path_fcodes)
 	int c;
 	int count, longest_path;
 	int error = 0;
-	u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN];
+	u_char bigram1[NBG], bigram2[NBG], path[LOCATE_PATH_MAX];
 
 	for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) {
 		p[c] = check_bigram_char(getc(fp));
@@ -67,9 +67,9 @@ statistic (fp, path_fcodes)
 		} else
 			count += c - OFFSET;
 		
-		if (count < 0 || count >= MAXPATHLEN) {
+		if (count < 0 || count >= LOCATE_PATH_MAX) {
 			/* stop on error and display the statstics anyway */
-			warnx("corrupted database: %s", path_fcodes);
+			warnx("corrupted database: %s %d", path_fcodes, count);
 			error = 1;
 			break;
 		}
@@ -157,7 +157,7 @@ fastfind
 	int c, cc;
 	int count, found, globflag;
 	u_char *cutoff;
-	u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN + 2];
+	u_char bigram1[NBG], bigram2[NBG], path[LOCATE_PATH_MAX + 2];
 
 #ifdef FF_ICASE
 	/* use a lookup table for case insensitive search */
@@ -233,7 +233,7 @@ fastfind
 			count += c - OFFSET;
 		}
 
-		if (count < 0 || count >= MAXPATHLEN)
+		if (count < 0 || count >= LOCATE_PATH_MAX)
 			errx(1, "corrupted database: %s %d", database, count);
 
 		/* overlay old path */
@@ -295,8 +295,8 @@ fastfind
 				*p++ = bigram2[c];
 			}
 
-			if (p - path >= MAXPATHLEN) 
-				errx(1, "corrupted database: %s", database);
+			if (p - path >= LOCATE_PATH_MAX) 
+				errx(1, "corrupted database: %s %ld", database, p - path);
 
 		}
 		
diff --git a/usr.bin/locate/locate/locate.h b/usr.bin/locate/locate/locate.h
index 6393160d55b4..18187ca00d84 100644
--- a/usr.bin/locate/locate/locate.h
+++ b/usr.bin/locate/locate/locate.h
@@ -68,3 +68,9 @@ extern u_char myctype[UCHAR_MAX + 1];
 #define INTSIZE (sizeof(int))
 
 #define LOCATE_REG "*?[]\\"  /* fnmatch(3) meta characters */
+
+/* max. path length for locate. Should be at least 1024 (PATH_MAX), but can be longer */
+#ifndef LOCATE_PATH_MAX
+#define LOCATE_PATH_MAX (1*1024)
+#endif
+
diff --git a/usr.bin/locate/locate/util.c b/usr.bin/locate/locate/util.c
index 8482ec998f03..1d15f83b6826 100644
--- a/usr.bin/locate/locate/util.c
+++ b/usr.bin/locate/locate/util.c
@@ -35,11 +35,10 @@
  * $FreeBSD$
  */
 
-
+#include <sys/param.h>
 #include <stdlib.h>
 #include <string.h>
 #include <err.h>
-#include <sys/param.h>
 #include <arpa/inet.h>
 #include <stdio.h>
 
@@ -224,8 +223,8 @@ getwm(p)
 	int i, hi;
 
 	/* the integer is stored by an offset of 14 (!!!) */
-        int i_max = MAXPATHLEN + OFFSET;
-        int i_min = -(MAXPATHLEN - OFFSET);
+        int i_max = LOCATE_PATH_MAX + OFFSET;
+        int i_min = -(LOCATE_PATH_MAX - OFFSET);
 
 	for (i = 0; i < (int)INTSIZE; i++)
 		u.buf[i] = *p++;
@@ -255,8 +254,8 @@ getwf(fp)
 	FILE *fp;
 {
 	int word, hword;
-        int i_max = MAXPATHLEN + OFFSET;
-        int i_min = -(MAXPATHLEN - OFFSET);
+        int i_max = LOCATE_PATH_MAX + OFFSET;
+        int i_min = -(LOCATE_PATH_MAX - OFFSET);
 
 	word = getw(fp);