Commit 086c1894 authored by Peter Eisentraut's avatar Peter Eisentraut

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.
parent b79575ce
/* $PostgreSQL: pgsql/contrib/tsearch2/dict_syn.c,v 1.11 2006/12/04 09:26:57 teodor Exp $ */ /* $PostgreSQL: pgsql/contrib/tsearch2/dict_syn.c,v 1.12 2007/02/08 11:10:26 petere Exp $ */
/* /*
* ISpell interface * ISpell interface
...@@ -101,7 +101,7 @@ syn_init(PG_FUNCTION_ARGS) ...@@ -101,7 +101,7 @@ syn_init(PG_FUNCTION_ARGS)
} }
memset(d, 0, sizeof(DictSyn)); memset(d, 0, sizeof(DictSyn));
while (fgets(buf, SYNBUFLEN, fin)) while (fgets(buf, sizeof(buf), fin))
{ {
slen = strlen(buf) - 1; slen = strlen(buf) - 1;
buf[slen] = '\0'; buf[slen] = '\0';
......
...@@ -45,7 +45,7 @@ readstoplist(text *in, StopList * s) ...@@ -45,7 +45,7 @@ readstoplist(text *in, StopList * s)
errmsg("could not open file \"%s\": %m", errmsg("could not open file \"%s\": %m",
filename))); filename)));
while (fgets(buf, STOPBUFLEN, hin)) while (fgets(buf, sizeof(buf), hin))
{ {
buf[strlen(buf) - 1] = '\0'; buf[strlen(buf) - 1] = '\0';
pg_verifymbstr(buf, strlen(buf), false); pg_verifymbstr(buf, strlen(buf), false);
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.262 2007/02/07 16:44:47 tgl Exp $ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.263 2007/02/08 11:10:27 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -3374,7 +3374,7 @@ readTimeLineHistory(TimeLineID targetTLI) ...@@ -3374,7 +3374,7 @@ readTimeLineHistory(TimeLineID targetTLI)
/* /*
* Parse the file... * Parse the file...
*/ */
while (fgets(fline, MAXPGPATH, fd) != NULL) while (fgets(fline, sizeof(fline), fd) != NULL)
{ {
/* skip leading whitespace and check for # comment */ /* skip leading whitespace and check for # comment */
char *ptr; char *ptr;
...@@ -4248,7 +4248,7 @@ readRecoveryCommandFile(void) ...@@ -4248,7 +4248,7 @@ readRecoveryCommandFile(void)
/* /*
* Parse the file... * Parse the file...
*/ */
while (fgets(cmdline, MAXPGPATH, fd) != NULL) while (fgets(cmdline, sizeof(cmdline), fd) != NULL)
{ {
/* skip leading whitespace and check for # comment */ /* skip leading whitespace and check for # comment */
char *ptr; char *ptr;
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* *
* *
* IDENTIFICATION * 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]) ...@@ -321,26 +321,25 @@ _getBlobTocEntry(ArchiveHandle *AH, Oid *oid, char fname[K_STD_BUF_SIZE])
{ {
lclContext *ctx = (lclContext *) AH->formatData; lclContext *ctx = (lclContext *) AH->formatData;
char blobTe[K_STD_BUF_SIZE]; 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); *oid = atooid(blobTe);
fpos = strcspn(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; eos = strlen(fname) - 1;
if (fname[eos] == '\n') if (fname[eos] == '\n')
fname[eos] = '\0'; fname[eos] = '\0';
} }
else else
{ {
*oid = 0; *oid = 0;
fname[0] = '\0'; fname[0] = '\0';
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2007, PostgreSQL Global Development Group * 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 "postgres_fe.h"
#include "common.h" #include "common.h"
...@@ -1497,7 +1497,7 @@ expand_tilde(char **filename) ...@@ -1497,7 +1497,7 @@ expand_tilde(char **filename)
if (*(fn + 1) == '\0') if (*(fn + 1) == '\0')
get_home_path(home); /* ~ or ~/ only */ get_home_path(home); /* ~ or ~/ only */
else if ((pw = getpwnam(fn + 1)) != NULL) else if ((pw = getpwnam(fn + 1)) != NULL)
StrNCpy(home, pw->pw_dir, MAXPGPATH); /* ~user */ strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
*p = oldp; *p = oldp;
if (strlen(home) != 0) if (strlen(home) != 0)
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2007, PostgreSQL Global Development Group * 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 "postgres_fe.h"
#include "copy.h" #include "copy.h"
...@@ -801,7 +801,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary) ...@@ -801,7 +801,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary)
/* enable longjmp while waiting for input */ /* enable longjmp while waiting for input */
sigint_interrupt_enabled = true; sigint_interrupt_enabled = true;
fgresult = fgets(buf, COPYBUFSIZ, copystream); fgresult = fgets(buf, sizeof(buf), copystream);
sigint_interrupt_enabled = false; sigint_interrupt_enabled = false;
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2007, PostgreSQL Global Development Group * 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" #include "postgres_fe.h"
...@@ -96,10 +96,10 @@ get_prompt(promptStatus_t status) ...@@ -96,10 +96,10 @@ get_prompt(promptStatus_t status)
destination[0] = '\0'; destination[0] = '\0';
for (p = prompt_string; for (p = prompt_string;
*p && strlen(destination) < MAX_PROMPT_SIZE; *p && strlen(destination) < sizeof(destination) - 1;
p++) p++)
{ {
memset(buf, 0, MAX_PROMPT_SIZE + 1); memset(buf, 0, sizeof(buf));
if (esc) if (esc)
{ {
switch (*p) switch (*p)
...@@ -107,7 +107,7 @@ get_prompt(promptStatus_t status) ...@@ -107,7 +107,7 @@ get_prompt(promptStatus_t status)
/* Current database */ /* Current database */
case '/': case '/':
if (pset.db) if (pset.db)
strncpy(buf, PQdb(pset.db), MAX_PROMPT_SIZE); strlcpy(buf, PQdb(pset.db), sizeof(buf));
break; break;
case '~': case '~':
if (pset.db) if (pset.db)
...@@ -116,9 +116,9 @@ get_prompt(promptStatus_t status) ...@@ -116,9 +116,9 @@ get_prompt(promptStatus_t status)
if (strcmp(PQdb(pset.db), PQuser(pset.db)) == 0 || if (strcmp(PQdb(pset.db), PQuser(pset.db)) == 0 ||
((var = getenv("PGDATABASE")) && strcmp(var, PQdb(pset.db)) == 0)) ((var = getenv("PGDATABASE")) && strcmp(var, PQdb(pset.db)) == 0))
strcpy(buf, "~"); strlcpy(buf, "~", sizeof(buf));
else else
strncpy(buf, PQdb(pset.db), MAX_PROMPT_SIZE); strlcpy(buf, PQdb(pset.db), sizeof(buf));
} }
break; break;
...@@ -132,7 +132,7 @@ get_prompt(promptStatus_t status) ...@@ -132,7 +132,7 @@ get_prompt(promptStatus_t status)
/* INET socket */ /* INET socket */
if (host && host[0] && !is_absolute_path(host)) if (host && host[0] && !is_absolute_path(host))
{ {
strncpy(buf, host, MAX_PROMPT_SIZE); strlcpy(buf, host, sizeof(buf));
if (*p == 'm') if (*p == 'm')
buf[strcspn(buf, ".")] = '\0'; buf[strcspn(buf, ".")] = '\0';
} }
...@@ -143,9 +143,9 @@ get_prompt(promptStatus_t status) ...@@ -143,9 +143,9 @@ get_prompt(promptStatus_t status)
if (!host if (!host
|| strcmp(host, DEFAULT_PGSOCKET_DIR) == 0 || strcmp(host, DEFAULT_PGSOCKET_DIR) == 0
|| *p == 'm') || *p == 'm')
strncpy(buf, "[local]", MAX_PROMPT_SIZE); strlcpy(buf, "[local]", sizeof(buf));
else else
snprintf(buf, MAX_PROMPT_SIZE, "[local:%s]", host); snprintf(buf, sizeof(buf), "[local:%s]", host);
} }
#endif #endif
} }
...@@ -153,12 +153,12 @@ get_prompt(promptStatus_t status) ...@@ -153,12 +153,12 @@ get_prompt(promptStatus_t status)
/* DB server port number */ /* DB server port number */
case '>': case '>':
if (pset.db && PQport(pset.db)) if (pset.db && PQport(pset.db))
strncpy(buf, PQport(pset.db), MAX_PROMPT_SIZE); strlcpy(buf, PQport(pset.db), sizeof(buf));
break; break;
/* DB server user name */ /* DB server user name */
case 'n': case 'n':
if (pset.db) if (pset.db)
strncpy(buf, session_username(), MAX_PROMPT_SIZE); strlcpy(buf, session_username(), sizeof(buf));
break; break;
case '0': case '0':
...@@ -252,7 +252,7 @@ get_prompt(promptStatus_t status) ...@@ -252,7 +252,7 @@ get_prompt(promptStatus_t status)
fd = popen(file, "r"); fd = popen(file, "r");
if (fd) if (fd)
{ {
fgets(buf, MAX_PROMPT_SIZE - 1, fd); fgets(buf, sizeof(buf), fd);
pclose(fd); pclose(fd);
} }
if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n') if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
...@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status) ...@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status)
name[nameend] = '\0'; name[nameend] = '\0';
val = GetVariable(pset.vars, name); val = GetVariable(pset.vars, name);
if (val) if (val)
strncpy(buf, val, MAX_PROMPT_SIZE); strlcpy(buf, val, sizeof(buf));
free(name); free(name);
p += nameend + 1; p += nameend + 1;
break; break;
...@@ -312,9 +312,8 @@ get_prompt(promptStatus_t status) ...@@ -312,9 +312,8 @@ get_prompt(promptStatus_t status)
} }
if (!esc) if (!esc)
strncat(destination, buf, MAX_PROMPT_SIZE - strlen(destination)); strlcat(destination, buf, sizeof(destination));
} }
destination[MAX_PROMPT_SIZE] = '\0';
return destination; return destination;
} }
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.341 2007/01/05 22:20:00 momjian Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.342 2007/02/08 11:10:27 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -2845,11 +2845,11 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage) ...@@ -2845,11 +2845,11 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
return 1; return 1;
} }
while ((line = fgets(buf, MAXBUFSIZE - 1, f)) != NULL) while ((line = fgets(buf, sizeof(buf), f)) != NULL)
{ {
linenr++; linenr++;
if (strlen(line) >= MAXBUFSIZE - 2) if (strlen(line) >= sizeof(buf) - 1)
{ {
fclose(f); fclose(f);
printfPQExpBuffer(errorMessage, printfPQExpBuffer(errorMessage,
...@@ -3654,7 +3654,7 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username) ...@@ -3654,7 +3654,7 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
*ret; *ret;
int len; int len;
fgets(buf, LINELEN - 1, fp); fgets(buf, sizeof(buf), fp);
len = strlen(buf); len = strlen(buf);
if (len == 0) if (len == 0)
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.91 2007/01/26 17:45:41 neilc Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.92 2007/02/08 11:10:27 petere Exp $
* *
* NOTES * NOTES
* [ Most of these notes are wrong/obsolete, but perhaps not all ] * [ Most of these notes are wrong/obsolete, but perhaps not all ]
...@@ -1018,8 +1018,7 @@ SSLerrmessage(void) ...@@ -1018,8 +1018,7 @@ SSLerrmessage(void)
errreason = ERR_reason_error_string(errcode); errreason = ERR_reason_error_string(errcode);
if (errreason != NULL) if (errreason != NULL)
{ {
strncpy(errbuf, errreason, SSL_ERR_LEN - 1); strlcpy(errbuf, errreason, SSL_ERR_LEN);
errbuf[SSL_ERR_LEN - 1] = '\0';
return errbuf; return errbuf;
} }
snprintf(errbuf, SSL_ERR_LEN, libpq_gettext("SSL error code %lu"), errcode); snprintf(errbuf, SSL_ERR_LEN, libpq_gettext("SSL error code %lu"), errcode);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
** entab.c - add tabs to a text file ** entab.c - add tabs to a text file
** by Bruce Momjian (root@candle.pha.pa.us) ** by Bruce Momjian (root@candle.pha.pa.us)
** **
** $PostgreSQL: pgsql/src/tools/entab/entab.c,v 1.17 2007/02/01 19:10:30 momjian Exp $ ** $PostgreSQL: pgsql/src/tools/entab/entab.c,v 1.18 2007/02/08 11:10:27 petere Exp $
** **
** version 1.3 ** version 1.3
** **
...@@ -108,7 +108,7 @@ main(int argc, char **argv) ...@@ -108,7 +108,7 @@ main(int argc, char **argv)
escaped = FALSE; escaped = FALSE;
while (fgets(in_line, BUFSIZ, in_file) != NULL) while (fgets(in_line, sizeof(in_line), in_file) != NULL)
{ {
col_in_tab = 0; col_in_tab = 0;
prv_spaces = 0; prv_spaces = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment