summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Eisentraut2007-02-08 11:10:27 +0000
committerPeter Eisentraut2007-02-08 11:10:27 +0000
commit67b259f59999035b0d3c8667f62feb7136fd9199 (patch)
tree01e961269ef6db33b67634efc3e432a23067b95e
parent2ffe59f719df665c787faf9adb21690d5673bc33 (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.
-rw-r--r--contrib/tsearch2/dict_syn.c2
-rw-r--r--contrib/tsearch2/stopword.c2
-rw-r--r--src/backend/access/transam/xlog.c4
-rw-r--r--src/bin/pg_dump/pg_backup_files.c11
-rw-r--r--src/bin/psql/common.c2
-rw-r--r--src/bin/psql/copy.c2
-rw-r--r--src/bin/psql/prompt.c27
-rw-r--r--src/interfaces/libpq/fe-connect.c6
-rw-r--r--src/interfaces/libpq/fe-secure.c3
-rw-r--r--src/tools/entab/entab.c2
10 files changed, 29 insertions, 32 deletions
diff --git a/contrib/tsearch2/dict_syn.c b/contrib/tsearch2/dict_syn.c
index 41166be815..c5b6b80ca1 100644
--- a/contrib/tsearch2/dict_syn.c
+++ b/contrib/tsearch2/dict_syn.c
@@ -101,7 +101,7 @@ syn_init(PG_FUNCTION_ARGS)
}
memset(d, 0, sizeof(DictSyn));
- while (fgets(buf, SYNBUFLEN, fin))
+ while (fgets(buf, sizeof(buf), fin))
{
slen = strlen(buf) - 1;
buf[slen] = '\0';
diff --git a/contrib/tsearch2/stopword.c b/contrib/tsearch2/stopword.c
index b9b7699594..d8bb54aca3 100644
--- a/contrib/tsearch2/stopword.c
+++ b/contrib/tsearch2/stopword.c
@@ -45,7 +45,7 @@ readstoplist(text *in, StopList * s)
errmsg("could not open file \"%s\": %m",
filename)));
- while (fgets(buf, STOPBUFLEN, hin))
+ while (fgets(buf, sizeof(buf), hin))
{
buf[strlen(buf) - 1] = '\0';
pg_verifymbstr(buf, strlen(buf), false);
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 8327c51570..641824ab1f 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -3374,7 +3374,7 @@ readTimeLineHistory(TimeLineID targetTLI)
/*
* Parse the file...
*/
- while (fgets(fline, MAXPGPATH, fd) != NULL)
+ while (fgets(fline, sizeof(fline), fd) != NULL)
{
/* skip leading whitespace and check for # comment */
char *ptr;
@@ -4248,7 +4248,7 @@ readRecoveryCommandFile(void)
/*
* Parse the file...
*/
- while (fgets(cmdline, MAXPGPATH, fd) != NULL)
+ while (fgets(cmdline, sizeof(cmdline), fd) != NULL)
{
/* skip leading whitespace and check for # comment */
char *ptr;
diff --git a/src/bin/pg_dump/pg_backup_files.c b/src/bin/pg_dump/pg_backup_files.c
index e68d20e1cf..877f5a875d 100644
--- a/src/bin/pg_dump/pg_backup_files.c
+++ b/src/bin/pg_dump/pg_backup_files.c
@@ -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 a503568361..4a49efeda7 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -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 cbbbdb4f11..771e8a8c76 100644
--- a/src/bin/psql/copy.c
+++ b/src/bin/psql/copy.c
@@ -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 97b289ce98..5a6502f789 100644
--- a/src/bin/psql/prompt.c
+++ b/src/bin/psql/prompt.c
@@ -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;
}
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index e48a10105b..82e1b4afbb 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -2845,11 +2845,11 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
return 1;
}
- while ((line = fgets(buf, MAXBUFSIZE - 1, f)) != NULL)
+ while ((line = fgets(buf, sizeof(buf), f)) != NULL)
{
linenr++;
- if (strlen(line) >= MAXBUFSIZE - 2)
+ if (strlen(line) >= sizeof(buf) - 1)
{
fclose(f);
printfPQExpBuffer(errorMessage,
@@ -3654,7 +3654,7 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
*ret;
int len;
- fgets(buf, LINELEN - 1, fp);
+ fgets(buf, sizeof(buf), fp);
len = strlen(buf);
if (len == 0)
diff --git a/src/interfaces/libpq/fe-secure.c b/src/interfaces/libpq/fe-secure.c
index d35bbcccae..f023a00bc5 100644
--- a/src/interfaces/libpq/fe-secure.c
+++ b/src/interfaces/libpq/fe-secure.c
@@ -1018,8 +1018,7 @@ SSLerrmessage(void)
errreason = ERR_reason_error_string(errcode);
if (errreason != NULL)
{
- strncpy(errbuf, errreason, SSL_ERR_LEN - 1);
- errbuf[SSL_ERR_LEN - 1] = '\0';
+ strlcpy(errbuf, errreason, SSL_ERR_LEN);
return errbuf;
}
snprintf(errbuf, SSL_ERR_LEN, libpq_gettext("SSL error code %lu"), errcode);
diff --git a/src/tools/entab/entab.c b/src/tools/entab/entab.c
index cd0a86a018..d377b3a919 100644
--- a/src/tools/entab/entab.c
+++ b/src/tools/entab/entab.c
@@ -108,7 +108,7 @@ main(int argc, char **argv)
escaped = FALSE;
- while (fgets(in_line, BUFSIZ, in_file) != NULL)
+ while (fgets(in_line, sizeof(in_line), in_file) != NULL)
{
col_in_tab = 0;
prv_spaces = 0;