From 51f62d505e2aba66bf7870c7bd005cd32e7d0953 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 25 Oct 1999 03:08:03 +0000 Subject: [PATCH] Standardize on MAXPGPATH as the size of a file pathname buffer, eliminating some wildly inconsistent coding in various parts of the system. I set MAXPGPATH = 1024 in config.h.in. If anyone is really convinced that there ought to be a configure-time test to set the value, go right ahead ... but I think it's a waste of time. --- src/backend/access/transam/xlog.c | 16 +++---- src/backend/bootstrap/bootstrap.c | 8 ++-- src/backend/commands/dbcommands.c | 20 ++++----- src/backend/libpq/pqcomm.c | 5 ++- src/backend/postmaster/postmaster.c | 20 ++++----- src/backend/storage/smgr/md.c | 6 +-- src/backend/tcop/postgres.c | 16 +++---- src/backend/utils/adt/filename.c | 4 +- src/backend/utils/error/elog.c | 6 +-- src/backend/utils/init/postinit.c | 66 ++++++++++++++--------------- src/backend/utils/misc/database.c | 50 ++++++++++++---------- src/bin/psql/psql.c | 6 +-- src/include/config.h.in | 12 ++++++ src/include/port/sco.h | 6 --- src/include/port/win32.h | 3 -- src/include/postgres.h | 7 +-- src/include/utils/dynamic_loader.h | 5 +-- src/interfaces/ecpg/preproc/pgc.l | 11 ++--- src/interfaces/libpq/fe-auth.c | 6 +-- src/interfaces/odbc/gpps.c | 33 ++++++--------- src/interfaces/odbc/setup.c | 9 ++-- src/utils/version.c | 61 ++++++++++++-------------- 22 files changed, 183 insertions(+), 193 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index e8e85b1113..05eb5b0706 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -5,7 +5,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Header: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v 1.6 1999/10/24 20:42:27 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v 1.7 1999/10/25 03:07:42 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -33,8 +33,8 @@ void StartupXLOG(void); void ShutdownXLOG(void); void CreateCheckPoint(bool shutdown); -char XLogDir[MAXPGPATH+1]; -char ControlFilePath[MAXPGPATH+1]; +char XLogDir[MAXPGPATH]; +char ControlFilePath[MAXPGPATH]; uint32 XLOGbuffers = 0; XLogRecPtr MyLastRecPtr = {0, 0}; bool StopIfError = false; @@ -147,8 +147,8 @@ typedef struct CheckPoint #define XLogFileSize (XLogLastSeg * XLogSegSize) #define XLogFileName(path, log, seg) \ - sprintf(path, "%.*s%c%08X%08X", \ - MAXPGPATH, XLogDir, SEP_CHAR, log, seg) + snprintf(path, MAXPGPATH, "%s%c%08X%08X", \ + XLogDir, SEP_CHAR, log, seg) #define PrevBufIdx(curridx) \ ((curridx == 0) ? XLogCtl->XLogCacheBlck : (curridx - 1)) @@ -718,7 +718,7 @@ XLogWrite(char *buffer) static int XLogFileInit(uint32 log, uint32 seg) { - char path[MAXPGPATH+1]; + char path[MAXPGPATH]; int fd; XLogFileName(path, log, seg); @@ -760,7 +760,7 @@ tryAgain: static int XLogFileOpen(uint32 log, uint32 seg, bool econt) { - char path[MAXPGPATH+1]; + char path[MAXPGPATH]; int fd; XLogFileName(path, log, seg); @@ -1067,7 +1067,7 @@ next_record_is_invalid:; readId++; } { - char path[MAXPGPATH+1]; + char path[MAXPGPATH]; XLogFileName(path, readId, readSeg); unlink(path); diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c index 8871e14835..35797e82d2 100644 --- a/src/backend/bootstrap/bootstrap.c +++ b/src/backend/bootstrap/bootstrap.c @@ -7,7 +7,7 @@ * Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.69 1999/10/06 21:58:02 vadim Exp $ + * $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.70 1999/10/25 03:07:43 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -333,8 +333,10 @@ BootstrapMain(int argc, char *argv[]) */ if (IsUnderPostmaster || xloginit) { - sprintf(XLogDir, "%s%cpg_xlog", DataDir, SEP_CHAR); - sprintf(ControlFilePath, "%s%cpg_control", DataDir, SEP_CHAR); + snprintf(XLogDir, MAXPGPATH, "%s%cpg_xlog", + DataDir, SEP_CHAR); + snprintf(ControlFilePath, MAXPGPATH, "%s%cpg_control", + DataDir, SEP_CHAR); } if (IsUnderPostmaster && xloginit) diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index b243dd173b..75a917092a 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.41 1999/09/24 00:24:17 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.42 1999/10/25 03:07:43 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -38,9 +38,9 @@ createdb(char *dbname, char *dbpath, int encoding, CommandDest dest) { Oid db_id; int4 user_id; - char buf[512]; + char buf[MAXPGPATH + 100]; char *lp, - loc[512]; + loc[MAXPGPATH]; /* * If this call returns, the database does not exist and we're allowed @@ -56,7 +56,7 @@ createdb(char *dbname, char *dbpath, int encoding, CommandDest dest) { if (*(dbpath + strlen(dbpath) - 1) == SEP_CHAR) *(dbpath + strlen(dbpath) - 1) = '\0'; - snprintf(loc, 512, "%s%c%s", dbpath, SEP_CHAR, dbname); + snprintf(loc, sizeof(loc), "%s%c%s", dbpath, SEP_CHAR, dbname); } else strcpy(loc, dbname); @@ -71,11 +71,11 @@ createdb(char *dbname, char *dbpath, int encoding, CommandDest dest) if (mkdir(lp, S_IRWXU) != 0) elog(ERROR, "Unable to create database directory '%s'", lp); - snprintf(buf, 512, "%s %s%cbase%ctemplate1%c* %s", + snprintf(buf, sizeof(buf), "%s %s%cbase%ctemplate1%c* '%s'", COPY_CMD, DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR, lp); system(buf); - snprintf(buf, 512, + snprintf(buf, sizeof(buf), "insert into pg_database (datname, datdba, encoding, datpath)" " values ('%s', '%d', '%d', '%s');", dbname, user_id, encoding, loc); @@ -89,8 +89,8 @@ destroydb(char *dbname, CommandDest dest) int4 user_id; Oid db_id; char *path, - dbpath[MAXPGPATH + 1], - buf[MAXPGPATH + 50]; + dbpath[MAXPGPATH], + buf[MAXPGPATH + 100]; Relation pgdbrel; HeapScanDesc pgdbscan; ScanKeyData key; @@ -233,7 +233,7 @@ check_permissions(char *command, bool use_super; char *userName; text *dbtext; - char path[MAXPGPATH + 1]; + char path[MAXPGPATH]; userName = GetPgUserName(); utup = SearchSysCacheTuple(USENAME, @@ -332,7 +332,7 @@ static void stop_vacuum(char *dbpath, char *dbname) { #ifdef NOT_USED - char filename[MAXPGPATH + 1]; + char filename[MAXPGPATH]; FILE *fp; int pid; diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c index 7fc86d4ede..44cdbd8984 100644 --- a/src/backend/libpq/pqcomm.c +++ b/src/backend/libpq/pqcomm.c @@ -28,7 +28,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pqcomm.c,v 1.85 1999/10/23 03:13:22 tgl Exp $ + * $Id: pqcomm.c,v 1.86 1999/10/25 03:07:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -155,7 +155,8 @@ pq_close(void) * Stream functions are used for vanilla TCP connection protocol. */ -static char sock_path[MAXPGPATH + 1] = ""; +static char sock_path[MAXPGPATH]; + /* StreamDoUnlink() * Shutdown routine for backend connection diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 4ac877bbad..611e443048 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.126 1999/10/08 05:36:58 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.127 1999/10/25 03:07:45 tgl Exp $ * * NOTES * @@ -179,7 +179,7 @@ static time_t tnow; /* * Default Values */ -static char Execfile[MAXPATHLEN] = ""; +static char Execfile[MAXPGPATH]; static int ServerSock_INET = INVALID_SOCK; /* stream socket server */ @@ -195,7 +195,7 @@ static SSL_CTX *SSL_context = NULL; /* Global SSL context */ /* * Set by the -o option */ -static char ExtraOptions[MAXPATHLEN] = ""; +static char ExtraOptions[MAXPGPATH]; /* * These globals control the behavior of the postmaster in case some @@ -294,10 +294,10 @@ checkDataDir(const char *DataDir, bool *DataDirOK) } else { - char path[MAXPATHLEN]; + char path[MAXPGPATH]; FILE *fp; - sprintf(path, "%s%cbase%ctemplate1%cpg_class", + snprintf(path, sizeof(path), "%s%cbase%ctemplate1%cpg_class", DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR); #ifndef __CYGWIN32__ fp = AllocateFile(path, "r"); @@ -446,7 +446,7 @@ PostmasterMain(int argc, char *argv[]) case 'b': /* Set the backend executable file to use. */ if (!ValidateBinary(optarg)) - strcpy(Execfile, optarg); + StrNCpy(Execfile, optarg, MAXPGPATH); else { fprintf(stderr, "%s: invalid backend \"%s\"\n", @@ -1698,7 +1698,7 @@ DoBackend(Port *port) { char *av[ARGV_SIZE * 2]; int ac = 0; - char execbuf[MAXPATHLEN]; + char execbuf[MAXPGPATH]; char debugbuf[ARGV_SIZE]; char protobuf[ARGV_SIZE]; char dbbuf[ARGV_SIZE]; @@ -1749,7 +1749,7 @@ DoBackend(Port *port) * ---------------- */ - StrNCpy(execbuf, Execfile, MAXPATHLEN); + StrNCpy(execbuf, Execfile, MAXPGPATH); av[ac++] = execbuf; /* @@ -2013,7 +2013,7 @@ SSDataBase(bool startup) { char *av[ARGV_SIZE * 2]; int ac = 0; - char execbuf[MAXPATHLEN]; + char execbuf[MAXPGPATH]; char nbbuf[ARGV_SIZE]; char dbbuf[ARGV_SIZE]; @@ -2024,7 +2024,7 @@ SSDataBase(bool startup) StreamClose(ServerSock_UNIX); #endif - StrNCpy(execbuf, Execfile, MAXPATHLEN); + StrNCpy(execbuf, Execfile, MAXPGPATH); av[ac++] = execbuf; av[ac++] = "-d"; diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c index 6d8425c4bd..c76f533511 100644 --- a/src/backend/storage/smgr/md.c +++ b/src/backend/storage/smgr/md.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.56 1999/10/06 06:38:04 inoue Exp $ + * $Header: /cvsroot/pgsql/src/backend/storage/smgr/md.c,v 1.57 1999/10/25 03:07:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -591,7 +591,7 @@ mdblindwrt(char *dbstr, else /* this is work arround only !!! */ { - char dbpath[MAXPGPATH + 1]; + char dbpath[MAXPGPATH]; Oid id; char *tmpPath; @@ -628,7 +628,7 @@ mdblindwrt(char *dbstr, else /* this is work arround only !!! */ { - char dbpath[MAXPGPATH + 1]; + char dbpath[MAXPGPATH]; Oid id; char *tmpPath; diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index ddf6c280e4..a73344e932 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.135 1999/10/23 03:13:22 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.136 1999/10/25 03:07:48 tgl Exp $ * * NOTES * this is the "main" module of the postgres backend and @@ -92,7 +92,6 @@ * ---------------- */ -/*static bool EnableRewrite = true; , never changes why have it*/ CommandDest whereToSendOutput = Debug; /* Define status buffer needed by PS_SET_STATUS */ @@ -114,8 +113,6 @@ int dontExecute = 0; static int ShowStats; static bool IsEmptyQuery = false; -char relname[80]; /* current relation name */ - /* note: these declarations had better match tcopprot.h */ DLLIMPORT sigjmp_buf Warn_restart; @@ -126,7 +123,7 @@ extern int NBuffers; static bool EchoQuery = false; /* default don't echo */ time_t tim; -char pg_pathname[256]; +char pg_pathname[MAXPGPATH]; FILE *StatFp; /* ---------------- @@ -1359,8 +1356,10 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[]) proc_exit(1); } BaseInit(); - sprintf(XLogDir, "%s%cpg_xlog", DataDir, SEP_CHAR); - sprintf(ControlFilePath, "%s%cpg_control", DataDir, SEP_CHAR); + snprintf(XLogDir, MAXPGPATH, "%s%cpg_xlog", + DataDir, SEP_CHAR); + snprintf(ControlFilePath, MAXPGPATH, "%s%cpg_control", + DataDir, SEP_CHAR); StartupXLOG(); } @@ -1372,6 +1371,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[]) SetCharSet(); #endif + /* On some systems our dynloader code needs the executable's pathname */ if (FindExec(pg_pathname, argv[0], "postgres") < 0) elog(FATAL, "%s: could not locate executable, bailing out...", argv[0]); @@ -1494,7 +1494,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[]) if (!IsUnderPostmaster) { puts("\nPOSTGRES backend interactive interface "); - puts("$Revision: 1.135 $ $Date: 1999/10/23 03:13:22 $\n"); + puts("$Revision: 1.136 $ $Date: 1999/10/25 03:07:48 $\n"); } /* diff --git a/src/backend/utils/adt/filename.c b/src/backend/utils/adt/filename.c index eb705b9f6b..0a3871280d 100644 --- a/src/backend/utils/adt/filename.c +++ b/src/backend/utils/adt/filename.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/filename.c,v 1.22 1999/07/17 20:17:55 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/filename.c,v 1.23 1999/10/25 03:07:49 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -31,7 +31,7 @@ filename_in(char *file) * (shexpand) */ - str = (char *) palloc(MAXPATHLEN * sizeof(*str)); + str = (char *) palloc(MAXPGPATH); str[0] = '\0'; if (file[0] == '~') { diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index 342a1029d1..247cf1c724 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.49 1999/10/06 21:58:09 vadim Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.50 1999/10/25 03:07:50 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -415,7 +415,6 @@ DebugFileOpen(void) if (OutputFileName[0]) { - OutputFileName[MAXPGPATH - 1] = '\0'; if ((fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY, 0666)) < 0) elog(FATAL, "DebugFileOpen: open of %s: %m", @@ -448,7 +447,8 @@ DebugFileOpen(void) fd = fileno(stderr); if (fcntl(fd, F_GETFD, 0) < 0) { - sprintf(OutputFileName, "%s/pg.errors.%d", DataDir, (int) MyProcPid); + snprintf(OutputFileName, MAXPGPATH, "%s%cpg.errors.%d", + DataDir, SEP_CHAR, (int) MyProcPid); fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY, 0666); } if (fd < 0) diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 20db5c34bb..72c87f1619 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.51 1999/10/06 21:58:10 vadim Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.52 1999/10/25 03:07:51 tgl Exp $ * * NOTES * InitPostgres() is the function called from PostgresMain @@ -100,7 +100,7 @@ static void InitMyDatabaseInfo(char *name) { char *path, - myPath[MAXPGPATH + 1]; + myPath[MAXPGPATH]; SetDatabaseName(name); GetRawDatabaseInfo(name, &MyDatabaseId, myPath); @@ -143,10 +143,9 @@ static void VerifySystemDatabase() { char *reason; - /* Failure reason returned by some function. NULL if no failure */ int fd; - char errormsg[1000]; + char errormsg[MAXPGPATH+100]; errormsg[0] = '\0'; @@ -155,20 +154,21 @@ VerifySystemDatabase() #else if ((fd = open(DataDir, O_RDONLY | O_DIROPEN, 0)) == -1) #endif - sprintf(errormsg, "Database system does not exist. " - "PGDATA directory '%s' not found.\n\tNormally, you " - "create a database system by running initdb.", - DataDir); + snprintf(errormsg, sizeof(errormsg), + "Database system does not exist. " + "PGDATA directory '%s' not found.\n\tNormally, you " + "create a database system by running initdb.", + DataDir); else { close(fd); ValidatePgVersion(DataDir, &reason); if (reason != NULL) - sprintf(errormsg, - "InitPostgres could not validate that the database" - " system version is compatible with this level of" - " Postgres.\n\tYou may need to run initdb to create" - " a new database system.\n\t%s", reason); + snprintf(errormsg, sizeof(errormsg), + "InitPostgres could not validate that the database" + " system version is compatible with this level of" + " Postgres.\n\tYou may need to run initdb to create" + " a new database system.\n\t%s", reason); } if (errormsg[0] != '\0') elog(FATAL, errormsg); @@ -185,7 +185,7 @@ VerifyMyDatabase() /* Failure reason returned by some function. NULL if no failure */ char *reason; int fd; - char errormsg[1000]; + char errormsg[MAXPGPATH+100]; name = DatabaseName; myPath = DatabasePath; @@ -195,26 +195,26 @@ VerifyMyDatabase() #else if ((fd = open(myPath, O_RDONLY | O_DIROPEN, 0)) == -1) #endif - sprintf(errormsg, - "Database '%s' does not exist." - "\n\tWe know this because the directory '%s' does not exist." - "\n\tYou can create a database with the SQL command" - " CREATE DATABASE.\n\tTo see what databases exist," - " look at the subdirectories of '%s/base/'.", - name, myPath, DataDir); + snprintf(errormsg, sizeof(errormsg), + "Database '%s' does not exist." + "\n\tWe know this because the directory '%s' does not exist." + "\n\tYou can create a database with the SQL command" + " CREATE DATABASE.\n\tTo see what databases exist," + " look at the subdirectories of '%s/base/'.", + name, myPath, DataDir); else { close(fd); ValidatePgVersion(myPath, &reason); if (reason != NULL) - sprintf(errormsg, - "InitPostgres could not validate that the database" - " version is compatible with this level of Postgres" - "\n\teven though the database system as a whole" - " appears to be at a compatible level." - "\n\tYou may need to recreate the database with SQL" - " commands DROP DATABASE and CREATE DATABASE." - "\n\t%s", reason); + snprintf(errormsg, sizeof(errormsg), + "InitPostgres could not validate that the database" + " version is compatible with this level of Postgres" + "\n\teven though the database system as a whole" + " appears to be at a compatible level." + "\n\tYou may need to recreate the database with SQL" + " commands DROP DATABASE and CREATE DATABASE." + "\n\t%s", reason); else { @@ -229,10 +229,10 @@ VerifyMyDatabase() rc = chdir(myPath); if (rc < 0) - sprintf(errormsg, - "InitPostgres unable to change " - "current directory to '%s', errno = %s (%d).", - myPath, strerror(errno), errno); + snprintf(errormsg, sizeof(errormsg), + "InitPostgres unable to change " + "current directory to '%s', errno = %s (%d).", + myPath, strerror(errno), errno); else errormsg[0] = '\0'; } diff --git a/src/backend/utils/misc/database.c b/src/backend/utils/misc/database.c index f5ff732b8f..cda5cfbdab 100644 --- a/src/backend/utils/misc/database.c +++ b/src/backend/utils/misc/database.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/misc/Attic/database.c,v 1.30 1999/09/24 00:25:04 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/misc/Attic/database.c,v 1.31 1999/10/25 03:07:52 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -30,7 +30,7 @@ GetDatabaseInfo(char *name, int4 *owner, char *path) { Oid dbowner, dbid; - char dbpath[MAXPGPATH + 1]; + char dbpath[MAXPGPATH]; text *dbtext; Relation dbrel; @@ -86,8 +86,7 @@ GetDatabaseInfo(char *name, int4 *owner, char *path) owner = palloc(sizeof(Oid)); *owner = dbowner; - path = palloc(strlen(dbpath) + 1); - strcpy(path, dbpath); + path = pstrdup(dbpath); /* doesn't do the right thing! */ return FALSE; } /* GetDatabaseInfo() */ @@ -97,46 +96,53 @@ GetDatabaseInfo(char *name, int4 *owner, char *path) char * ExpandDatabasePath(char *dbpath) { - char *path; + char buf[MAXPGPATH]; char *cp; - char buf[MAXPGPATH + 1]; + char *envvar; + int len; + + if (strlen(dbpath) >= MAXPGPATH) + return NULL; /* ain't gonna fit nohow */ /* leading path delimiter? then already absolute path */ if (*dbpath == SEP_CHAR) { #ifdef ALLOW_ABSOLUTE_DBPATHS cp = strrchr(dbpath, SEP_CHAR); - strncpy(buf, dbpath, (cp - dbpath)); - sprintf(&buf[cp - dbpath], "%cbase%c%s", SEP_CHAR, SEP_CHAR, (cp + 1)); + len = cp - dbpath; + strncpy(buf, dbpath, len); + snprintf(&buf[len], MAXPGPATH-len, "%cbase%c%s", + SEP_CHAR, SEP_CHAR, (cp + 1)); #else return NULL; #endif } /* path delimiter somewhere? then has leading environment variable */ - else if (strchr(dbpath, SEP_CHAR) != NULL) + else if ((cp = strchr(dbpath, SEP_CHAR)) != NULL) { - cp = strchr(dbpath, SEP_CHAR); - strncpy(buf, dbpath, (cp - dbpath)); - buf[cp - dbpath] = '\0'; - path = getenv(buf); + len = cp - dbpath; + strncpy(buf, dbpath, len); + buf[len] = '\0'; + envvar = getenv(buf); /* * problem getting environment variable? let calling routine * handle it */ - if (path == NULL) - return path; + if (envvar == NULL) + return envvar; - sprintf(buf, "%s%cbase%c%s", path, SEP_CHAR, SEP_CHAR, (cp + 1)); + snprintf(buf, sizeof(buf), "%s%cbase%c%s", + envvar, SEP_CHAR, SEP_CHAR, (cp + 1)); } - /* no path delimiter? then add the default path prefixes */ else - sprintf(buf, "%s%cbase%c%s", DataDir, SEP_CHAR, SEP_CHAR, dbpath); + { + /* no path delimiter? then add the default path prefix */ + snprintf(buf, sizeof(buf), "%s%cbase%c%s", + DataDir, SEP_CHAR, SEP_CHAR, dbpath); + } - path = palloc(strlen(buf) + 1); - strcpy(path, buf); - - return path; + return pstrdup(buf); } /* ExpandDatabasePath() */ diff --git a/src/bin/psql/psql.c b/src/bin/psql/psql.c index 89bd8e8e1f..e61ac49144 100644 --- a/src/bin/psql/psql.c +++ b/src/bin/psql/psql.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.193 1999/10/23 01:31:32 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.194 1999/10/25 03:07:54 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -19,7 +19,6 @@ #include #include #else -#include /* for MAXPATHLEN */ #include #include #endif @@ -76,7 +75,6 @@ #define open(x,y,z) _open(x,y,z) #define strcasecmp(x,y) stricmp(x,y) #define pqsignal(x,y) -#define MAXPATHLEN MAX_PATH #define R_OK 0 /* getopt is not in the standard includes on Win32 */ @@ -1495,7 +1493,7 @@ do_copy(const char *args, PsqlSettings *pset) bool from; /* The direction of the copy is from a file to a table. */ - char file[MAXPATHLEN + 1]; + char file[MAXPGPATH]; /* The pathname of the file from/to which we copy */ char table[NAMEDATALEN]; diff --git a/src/include/config.h.in b/src/include/config.h.in index e36220e940..698526e7da 100644 --- a/src/include/config.h.in +++ b/src/include/config.h.in @@ -203,6 +203,18 @@ /* #define NO_SECURITY */ /* #define OLD_REWRITE */ +/* + * MAXPGPATH: standard size of a pathname buffer in Postgres (hence, + * maximum usable pathname length is one less). + * + * We'd use a standard system header symbol for this, if there weren't + * so many to choose from: MAXPATHLEN, _POSIX_PATH_MAX, MAX_PATH, PATH_MAX + * are all defined by different "standards", and often have different + * values on the same platform! So we just punt and use a reasonably + * generous setting here. + */ +#define MAXPGPATH 1024 + /* *------------------------------------------------------------------------ * The following is set using configure. diff --git a/src/include/port/sco.h b/src/include/port/sco.h index 074c653397..daba7b5a70 100644 --- a/src/include/port/sco.h +++ b/src/include/port/sco.h @@ -1,9 +1,3 @@ -#include /* For _POSIX_PATH_MAX */ - -#ifndef MAXPATHLEN -#define MAXPATHLEN _POSIX_PATH_MAX -#endif - #ifndef NOFILE #define NOFILE NOFILES_MIN #endif diff --git a/src/include/port/win32.h b/src/include/port/win32.h index 7e21816b2b..59bd010afc 100644 --- a/src/include/port/win32.h +++ b/src/include/port/win32.h @@ -2,6 +2,3 @@ #define NEED_SIG_JMP #define USES_WINSOCK #define NOFILE 100 -#ifndef MAXPATHLEN -#define MAXPATHLEN 250 -#endif diff --git a/src/include/postgres.h b/src/include/postgres.h index 5835f44486..ddc8400733 100644 --- a/src/include/postgres.h +++ b/src/include/postgres.h @@ -6,7 +6,7 @@ * * Copyright (c) 1995, Regents of the University of California * - * $Id: postgres.h,v 1.27 1999/10/23 03:13:30 tgl Exp $ + * $Id: postgres.h,v 1.28 1999/10/25 03:07:55 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -28,7 +28,7 @@ * 2) varlena and array types * 3) TransactionId and CommandId * 4) genbki macros used by catalog/pg_xxx.h files - * 5) random CSIGNBIT, MAXPGPATH, STATUS macros + * 5) random stuff * * ---------------------------------------------------------------- */ @@ -158,9 +158,6 @@ typedef uint32 CommandId; /* msb for char */ #define CSIGNBIT (0x80) -/* this should probably be somewhere else */ -#define MAXPGPATH 128 - #define STATUS_OK (0) #define STATUS_ERROR (-1) #define STATUS_NOT_FOUND (-2) diff --git a/src/include/utils/dynamic_loader.h b/src/include/utils/dynamic_loader.h index 78d74966c7..fa2acd226a 100644 --- a/src/include/utils/dynamic_loader.h +++ b/src/include/utils/dynamic_loader.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: dynamic_loader.h,v 1.12 1999/07/15 23:04:21 momjian Exp $ + * $Id: dynamic_loader.h,v 1.13 1999/10/25 03:07:58 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -14,7 +14,6 @@ #define DYNAMIC_LOADER_H #include -#include /* For MAXPATHLEN */ /* we need this include because port files use them */ #include "postgres.h" @@ -30,7 +29,7 @@ typedef struct df_files { - char filename[MAXPATHLEN]; /* Full pathname of file */ + char filename[MAXPGPATH]; /* Full pathname of file */ dev_t device; /* Device file is on */ ino_t inode; /* Inode number of file */ void *handle; /* a handle for pg_dl* functions */ diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/preproc/pgc.l index ed406c9dd9..d84144262e 100644 --- a/src/interfaces/ecpg/preproc/pgc.l +++ b/src/interfaces/ecpg/preproc/pgc.l @@ -11,7 +11,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.45 1999/10/22 23:14:50 tgl Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.46 1999/10/25 03:07:59 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -22,11 +22,6 @@ #include "postgres.h" -#ifndef PATH_MAX -#include -#define PATH_MAX MAXPATHLEN -#endif - #include "miscadmin.h" #include "nodes/parsenodes.h" #include "nodes/pg_list.h" @@ -527,7 +522,7 @@ cppline {space}*#.*(\\{space}*\n)*\n* [^ \t\n]+ { /* got the include file name */ struct _yy_buffer *yb; struct _include_path *ip; - char inc_file[PATH_MAX]; + char inc_file[MAXPGPATH]; yb = mm_alloc(sizeof(struct _yy_buffer)); @@ -544,7 +539,7 @@ cppline {space}*#.*(\\{space}*\n)*\n* yyin = NULL; for (ip = include_paths; yyin == NULL && ip != NULL; ip = ip->next) { - if (strlen(ip->path) + strlen(yytext) + 3 > PATH_MAX) + if (strlen(ip->path) + strlen(yytext) + 3 > MAXPGPATH) { fprintf(stderr, "Error: Path %s/%s is too long in line %d, skipping.\n", ip->path, yytext, yylineno); continue; diff --git a/src/interfaces/libpq/fe-auth.c b/src/interfaces/libpq/fe-auth.c index 574d78c25d..fe92bd6964 100644 --- a/src/interfaces/libpq/fe-auth.c +++ b/src/interfaces/libpq/fe-auth.c @@ -9,7 +9,7 @@ * exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes). * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.33 1999/08/31 01:37:36 tgl Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.34 1999/10/25 03:08:00 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -127,7 +127,7 @@ pg_krb4_init() */ if (realm = getenv("PGREALM")) { - char tktbuf[MAXPATHLEN]; + char tktbuf[MAXPGPATH]; (void) sprintf(tktbuf, "%s@%s", tkt_string(), realm); krb_set_tkt_string(tktbuf); @@ -272,7 +272,7 @@ pg_krb5_init(void) krb5_error_code code; char *realm, *defname; - char tktbuf[MAXPATHLEN]; + char tktbuf[MAXPGPATH]; static krb5_ccache ccache = (krb5_ccache) NULL; if (ccache) diff --git a/src/interfaces/odbc/gpps.c b/src/interfaces/odbc/gpps.c index 8d2796673c..b067ca1c10 100644 --- a/src/interfaces/odbc/gpps.c +++ b/src/interfaces/odbc/gpps.c @@ -36,11 +36,6 @@ #define FALSE ((BOOL)0) #endif -#if HAVE_SYS_PARAM_H -#include -#else -#define MAXPATHLEN 255 -#endif DWORD GetPrivateProfileString(char *theSection, // section name @@ -50,7 +45,7 @@ GetPrivateProfileString(char *theSection, // section name size_t theReturnBufferLength, // byte length of return buffer char *theIniFileName) // pathname of ini file to search { - char buf[MAXPATHLEN+1]; + char buf[MAXPGPATH]; char* ptr = 0; FILE* aFile = 0; size_t aLength; @@ -70,8 +65,8 @@ GetPrivateProfileString(char *theSection, // section name if( ptr == NULL) { - if( MAXPATHLEN < j ) - theIniFileName[MAXPATHLEN] = '\0'; + if( MAXPGPATH-1 < j ) + theIniFileName[MAXPGPATH-1] = '\0'; sprintf(buf,"%s",theIniFileName); } @@ -84,12 +79,12 @@ GetPrivateProfileString(char *theSection, // section name * the file won't be found and thus the default value will be * returned. */ - if( MAXPATHLEN < strlen(ptr) + j ) + if( MAXPGPATH-1 < strlen(ptr) + j ) { - if( MAXPATHLEN < strlen(ptr) ) - ptr[MAXPATHLEN] = '\0'; + if( MAXPGPATH-1 < strlen(ptr) ) + ptr[MAXPGPATH-1] = '\0'; else - theIniFileName[MAXPATHLEN-strlen(ptr)] = '\0'; + theIniFileName[MAXPGPATH-1-strlen(ptr)] = '\0'; } sprintf( buf, "%s/%s",ptr,theIniFileName ); @@ -323,7 +318,7 @@ WritePrivateProfileString(char *theSection, // section name char *theBuffer, // input buffer char *theIniFileName) // pathname of ini file to write { - char buf[MAXPATHLEN+1]; + char buf[MAXPGPATH]; char* ptr = 0; FILE* aFile = 0; size_t aLength; @@ -349,8 +344,8 @@ WritePrivateProfileString(char *theSection, // section name if( ptr == NULL) { - if( MAXPATHLEN < j ) - theIniFileName[MAXPATHLEN] = '\0'; + if( MAXPGPATH-1 < j ) + theIniFileName[MAXPGPATH-1] = '\0'; sprintf(buf,"%s",theIniFileName); } @@ -363,12 +358,12 @@ WritePrivateProfileString(char *theSection, // section name // the file won't be found and thus the default value will be // returned. // - if( MAXPATHLEN < strlen(ptr) + j ) + if( MAXPGPATH-1 < strlen(ptr) + j ) { - if( MAXPATHLEN < strlen(ptr) ) - ptr[MAXPATHLEN] = '\0'; + if( MAXPGPATH-1 < strlen(ptr) ) + ptr[MAXPGPATH-1] = '\0'; else - theIniFileName[MAXPATHLEN-strlen(ptr)] = '\0'; + theIniFileName[MAXPGPATH-1-strlen(ptr)] = '\0'; } sprintf( buf, "%s/%s",ptr,theIniFileName ); diff --git a/src/interfaces/odbc/setup.c b/src/interfaces/odbc/setup.c index dae789003d..40816fd110 100644 --- a/src/interfaces/odbc/setup.c +++ b/src/interfaces/odbc/setup.c @@ -32,7 +32,6 @@ extern GLOBAL_VALUES globals; // Constants --------------------------------------------------------------- #define MIN(x,y) ((x) < (y) ? (x) : (y)) -#define MAXPATHLEN (255+1) // Max path length #define MAXKEYLEN (15+1) // Max keyword length #define MAXDESC (255+1) // Max description length #define MAXDSNAME (32+1) // Max data source name length @@ -323,7 +322,7 @@ LPCSTR lpsz; LPCSTR lpszStart; char aszKey[MAXKEYLEN]; int cbKey; -char value[MAXPATHLEN]; +char value[MAXPGPATH]; memset(&lpsetupdlg->ci, 0, sizeof(ConnInfo)); @@ -352,7 +351,7 @@ char value[MAXPATHLEN]; // lpsetupdlg->aAttr[iElement].fSupplied = TRUE; - _fmemcpy(value, lpszStart, MIN(lpsz-lpszStart+1, MAXPATHLEN)); + _fmemcpy(value, lpszStart, MIN(lpsz-lpszStart+1, MAXPGPATH)); mylog("aszKey='%s', value='%s'\n", aszKey, value); @@ -384,8 +383,8 @@ LPCSTR lpszDSN; // Pointer to da { if (hwndParent) { - char szBuf[MAXPATHLEN]; - char szMsg[MAXPATHLEN]; + char szBuf[MAXPGPATH]; + char szMsg[MAXPGPATH]; LoadString(s_hModule, IDS_BADDSN, szBuf, sizeof(szBuf)); wsprintf(szMsg, szBuf, lpszDSN); diff --git a/src/utils/version.c b/src/utils/version.c index 2284093eab..092f055d53 100644 --- a/src/utils/version.c +++ b/src/utils/version.c @@ -7,14 +7,10 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/utils/Attic/version.c,v 1.12 1999/07/17 20:18:55 momjian Exp $ + * $Header: /cvsroot/pgsql/src/utils/Attic/version.c,v 1.13 1999/10/25 03:08:03 tgl Exp $ * - * NOTES - * XXX eventually, should be able to handle version identifiers - * of length != 4. - * - * STANDALONE CODE - do not use error routines as this code is linked with - * stuff that does not cinterface.a + * STANDALONE CODE - do not use error routines as this code is not linked + * with any... *------------------------------------------------------------------------- */ #include @@ -41,7 +37,7 @@ PathSetVersionFilePath(const char *path, char *filepathbuf) Destructively change "filepathbuf" to contain the concatenation of "path" and the name of the version file name. ----------------------------------------------------------------------------*/ - if (strlen(path) > (MAXPGPATH - sizeof(PG_VERFILE) - 1)) + if ((strlen(path) + 1 + strlen(PG_VERFILE)) >= MAXPGPATH) *filepathbuf = '\0'; else sprintf(filepathbuf, "%s%c%s", path, SEP_CHAR, PG_VERFILE); @@ -61,44 +57,45 @@ ValidatePgVersion(const char *path, char **reason_p) we can't tell), and return a pointer to that space as <*reason_p>. -----------------------------------------------------------------------------*/ int fd; - char version[4]; - char full_path[MAXPGPATH + 1]; + int nread; + char myversion[32]; + char version[32]; + char full_path[MAXPGPATH]; PathSetVersionFilePath(path, full_path); + sprintf(myversion, "%s.%s\n", PG_RELEASE, PG_VERSION); + #ifndef __CYGWIN32__ if ((fd = open(full_path, O_RDONLY, 0)) == -1) #else if ((fd = open(full_path, O_RDONLY | O_BINARY, 0)) == -1) #endif { - *reason_p = malloc(200); + *reason_p = malloc(100 + strlen(full_path)); sprintf(*reason_p, "File '%s' does not exist or no read permission.", full_path); } else { - if (read(fd, version, 4) < 4 || - !isascii(version[0]) || !isdigit(version[0]) || - version[1] != '.' || - !isascii(version[2]) || !isdigit(version[2]) || - version[3] != '\n') + nread = read(fd, version, sizeof(version)-1); + if (nread < 4 || + !isdigit(version[0]) || + version[nread-1] != '\n') { - - *reason_p = malloc(200); + *reason_p = malloc(100 + strlen(full_path)); sprintf(*reason_p, "File '%s' does not have a valid format " "for a PG_VERSION file.", full_path); } else { - if (version[2] != PG_VERSION[0] || - version[0] != PG_RELEASE[0]) + version[nread] = '\0'; + if (strcmp(version, myversion) != 0) { - *reason_p = malloc(200); + *reason_p = malloc(200 + strlen(full_path)); sprintf(*reason_p, - "Version number in file '%s' should be %s.%s, " - "not %c.%c.", - full_path, - PG_RELEASE, PG_VERSION, version[0], version[2]); + "Version number in file '%s' should be %s, " + "not %s.", + full_path, myversion, version); } else *reason_p = NULL; @@ -120,11 +117,13 @@ SetPgVersion(const char *path, char **reason_p) return *reason_p = NULL. ---------------------------------------------------------------------------*/ int fd; - char version[4]; - char full_path[MAXPGPATH + 1]; + char version[32]; + char full_path[MAXPGPATH]; PathSetVersionFilePath(path, full_path); + sprintf(version, "%s.%s\n", PG_RELEASE, PG_VERSION); + #ifndef __CYGWIN32__ fd = open(full_path, O_WRONLY | O_CREAT | O_EXCL, 0666); #else @@ -141,12 +140,8 @@ SetPgVersion(const char *path, char **reason_p) { int rc; /* return code from some function we call */ - version[0] = PG_RELEASE[0]; - version[1] = '.'; - version[2] = PG_VERSION[0]; - version[3] = '\n'; - rc = write(fd, version, 4); - if (rc != 4) + rc = write(fd, version, strlen(version)); + if (rc != strlen(version)) { *reason_p = malloc(100 + strlen(full_path)); sprintf(*reason_p,