summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorPeter Eisentraut2007-02-08 11:10:27 +0000
committerPeter Eisentraut2007-02-08 11:10:27 +0000
commit086c189456655846707d9a260a3919f4404ecd9e (patch)
treec4c34d96579d19884bdc12cec5dec76f6a8f962c /src/bin
parentb79575ce4599db628a09773003b2e73d81d75f54 (diff)
Normalize fgets() calls to use sizeof() for calculating the buffer size
where possible, and fix some sites that apparently thought that fgets() will overwrite the buffer by one byte. Also add some strlcpy() to eliminate some weird memory handling.
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/pg_dump/pg_backup_files.c13
-rw-r--r--src/bin/psql/common.c4
-rw-r--r--src/bin/psql/copy.c4
-rw-r--r--src/bin/psql/prompt.c29
4 files changed, 24 insertions, 26 deletions
diff --git a/src/bin/pg_dump/pg_backup_files.c b/src/bin/pg_dump/pg_backup_files.c
index 8216d979d81..72eedac2e43 100644
--- a/src/bin/pg_dump/pg_backup_files.c
+++ b/src/bin/pg_dump/pg_backup_files.c
@@ -20,7 +20,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.29 2006/07/14 14:52:26 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.30 2007/02/08 11:10:27 petere Exp $
*
*-------------------------------------------------------------------------
*/
@@ -321,26 +321,25 @@ _getBlobTocEntry(ArchiveHandle *AH, Oid *oid, char fname[K_STD_BUF_SIZE])
{
lclContext *ctx = (lclContext *) AH->formatData;
char blobTe[K_STD_BUF_SIZE];
- size_t fpos;
- size_t eos;
- if (fgets(&blobTe[0], K_STD_BUF_SIZE - 1, ctx->blobToc) != NULL)
+ if (fgets(blobTe, sizeof(blobTe), ctx->blobToc) != NULL)
{
+ size_t fpos;
+ size_t eos;
+
*oid = atooid(blobTe);
fpos = strcspn(blobTe, " ");
- strncpy(fname, &blobTe[fpos + 1], K_STD_BUF_SIZE - 1);
+ strlcpy(fname, &blobTe[fpos + 1], K_STD_BUF_SIZE);
eos = strlen(fname) - 1;
if (fname[eos] == '\n')
fname[eos] = '\0';
-
}
else
{
-
*oid = 0;
fname[0] = '\0';
}
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 024cf1cd9cb..d05c241c698 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.132 2007/01/05 22:19:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.133 2007/02/08 11:10:27 petere Exp $
*/
#include "postgres_fe.h"
#include "common.h"
@@ -1497,7 +1497,7 @@ expand_tilde(char **filename)
if (*(fn + 1) == '\0')
get_home_path(home); /* ~ or ~/ only */
else if ((pw = getpwnam(fn + 1)) != NULL)
- StrNCpy(home, pw->pw_dir, MAXPGPATH); /* ~user */
+ strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
*p = oldp;
if (strlen(home) != 0)
diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c
index e655f46c349..eb205ecbc27 100644
--- a/src/bin/psql/copy.c
+++ b/src/bin/psql/copy.c
@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.73 2007/02/05 15:22:18 adunstan Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.74 2007/02/08 11:10:27 petere Exp $
*/
#include "postgres_fe.h"
#include "copy.h"
@@ -801,7 +801,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary)
/* enable longjmp while waiting for input */
sigint_interrupt_enabled = true;
- fgresult = fgets(buf, COPYBUFSIZ, copystream);
+ fgresult = fgets(buf, sizeof(buf), copystream);
sigint_interrupt_enabled = false;
diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c
index b7628e7a7f9..1df630ae6bb 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.49 2007/01/05 22:19:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.50 2007/02/08 11:10:27 petere Exp $
*/
#include "postgres_fe.h"
@@ -96,10 +96,10 @@ get_prompt(promptStatus_t status)
destination[0] = '\0';
for (p = prompt_string;
- *p && strlen(destination) < MAX_PROMPT_SIZE;
+ *p && strlen(destination) < sizeof(destination) - 1;
p++)
{
- memset(buf, 0, MAX_PROMPT_SIZE + 1);
+ memset(buf, 0, sizeof(buf));
if (esc)
{
switch (*p)
@@ -107,7 +107,7 @@ get_prompt(promptStatus_t status)
/* Current database */
case '/':
if (pset.db)
- strncpy(buf, PQdb(pset.db), MAX_PROMPT_SIZE);
+ strlcpy(buf, PQdb(pset.db), sizeof(buf));
break;
case '~':
if (pset.db)
@@ -116,9 +116,9 @@ get_prompt(promptStatus_t status)
if (strcmp(PQdb(pset.db), PQuser(pset.db)) == 0 ||
((var = getenv("PGDATABASE")) && strcmp(var, PQdb(pset.db)) == 0))
- strcpy(buf, "~");
+ strlcpy(buf, "~", sizeof(buf));
else
- strncpy(buf, PQdb(pset.db), MAX_PROMPT_SIZE);
+ strlcpy(buf, PQdb(pset.db), sizeof(buf));
}
break;
@@ -132,7 +132,7 @@ get_prompt(promptStatus_t status)
/* INET socket */
if (host && host[0] && !is_absolute_path(host))
{
- strncpy(buf, host, MAX_PROMPT_SIZE);
+ strlcpy(buf, host, sizeof(buf));
if (*p == 'm')
buf[strcspn(buf, ".")] = '\0';
}
@@ -143,9 +143,9 @@ get_prompt(promptStatus_t status)
if (!host
|| strcmp(host, DEFAULT_PGSOCKET_DIR) == 0
|| *p == 'm')
- strncpy(buf, "[local]", MAX_PROMPT_SIZE);
+ strlcpy(buf, "[local]", sizeof(buf));
else
- snprintf(buf, MAX_PROMPT_SIZE, "[local:%s]", host);
+ snprintf(buf, sizeof(buf), "[local:%s]", host);
}
#endif
}
@@ -153,12 +153,12 @@ get_prompt(promptStatus_t status)
/* DB server port number */
case '>':
if (pset.db && PQport(pset.db))
- strncpy(buf, PQport(pset.db), MAX_PROMPT_SIZE);
+ strlcpy(buf, PQport(pset.db), sizeof(buf));
break;
/* DB server user name */
case 'n':
if (pset.db)
- strncpy(buf, session_username(), MAX_PROMPT_SIZE);
+ strlcpy(buf, session_username(), sizeof(buf));
break;
case '0':
@@ -252,7 +252,7 @@ get_prompt(promptStatus_t status)
fd = popen(file, "r");
if (fd)
{
- fgets(buf, MAX_PROMPT_SIZE - 1, fd);
+ fgets(buf, sizeof(buf), fd);
pclose(fd);
}
if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status)
name[nameend] = '\0';
val = GetVariable(pset.vars, name);
if (val)
- strncpy(buf, val, MAX_PROMPT_SIZE);
+ strlcpy(buf, val, sizeof(buf));
free(name);
p += nameend + 1;
break;
@@ -312,9 +312,8 @@ get_prompt(promptStatus_t status)
}
if (!esc)
- strncat(destination, buf, MAX_PROMPT_SIZE - strlen(destination));
+ strlcat(destination, buf, sizeof(destination));
}
- destination[MAX_PROMPT_SIZE] = '\0';
return destination;
}