Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 086c189

Browse files
committed
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.
1 parent b79575c commit 086c189

File tree

10 files changed

+38
-41
lines changed

10 files changed

+38
-41
lines changed

contrib/tsearch2/dict_syn.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/contrib/tsearch2/dict_syn.c,v 1.11 2006/12/04 09:26:57 teodor Exp $ */
1+
/* $PostgreSQL: pgsql/contrib/tsearch2/dict_syn.c,v 1.12 2007/02/08 11:10:26 petere Exp $ */
22

33
/*
44
* ISpell interface
@@ -101,7 +101,7 @@ syn_init(PG_FUNCTION_ARGS)
101101
}
102102
memset(d, 0, sizeof(DictSyn));
103103

104-
while (fgets(buf, SYNBUFLEN, fin))
104+
while (fgets(buf, sizeof(buf), fin))
105105
{
106106
slen = strlen(buf) - 1;
107107
buf[slen] = '\0';

contrib/tsearch2/stopword.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ readstoplist(text *in, StopList * s)
4545
errmsg("could not open file \"%s\": %m",
4646
filename)));
4747

48-
while (fgets(buf, STOPBUFLEN, hin))
48+
while (fgets(buf, sizeof(buf), hin))
4949
{
5050
buf[strlen(buf) - 1] = '\0';
5151
pg_verifymbstr(buf, strlen(buf), false);

src/backend/access/transam/xlog.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.262 2007/02/07 16:44:47 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.263 2007/02/08 11:10:27 petere Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -3374,7 +3374,7 @@ readTimeLineHistory(TimeLineID targetTLI)
33743374
/*
33753375
* Parse the file...
33763376
*/
3377-
while (fgets(fline, MAXPGPATH, fd) != NULL)
3377+
while (fgets(fline, sizeof(fline), fd) != NULL)
33783378
{
33793379
/* skip leading whitespace and check for # comment */
33803380
char *ptr;
@@ -4248,7 +4248,7 @@ readRecoveryCommandFile(void)
42484248
/*
42494249
* Parse the file...
42504250
*/
4251-
while (fgets(cmdline, MAXPGPATH, fd) != NULL)
4251+
while (fgets(cmdline, sizeof(cmdline), fd) != NULL)
42524252
{
42534253
/* skip leading whitespace and check for # comment */
42544254
char *ptr;

src/bin/pg_dump/pg_backup_files.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
*
2222
* IDENTIFICATION
23-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.29 2006/07/14 14:52:26 momjian Exp $
23+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_files.c,v 1.30 2007/02/08 11:10:27 petere Exp $
2424
*
2525
*-------------------------------------------------------------------------
2626
*/
@@ -321,26 +321,25 @@ _getBlobTocEntry(ArchiveHandle *AH, Oid *oid, char fname[K_STD_BUF_SIZE])
321321
{
322322
lclContext *ctx = (lclContext *) AH->formatData;
323323
char blobTe[K_STD_BUF_SIZE];
324-
size_t fpos;
325-
size_t eos;
326324

327-
if (fgets(&blobTe[0], K_STD_BUF_SIZE - 1, ctx->blobToc) != NULL)
325+
if (fgets(blobTe, sizeof(blobTe), ctx->blobToc) != NULL)
328326
{
327+
size_t fpos;
328+
size_t eos;
329+
329330
*oid = atooid(blobTe);
330331

331332
fpos = strcspn(blobTe, " ");
332333

333-
strncpy(fname, &blobTe[fpos + 1], K_STD_BUF_SIZE - 1);
334+
strlcpy(fname, &blobTe[fpos + 1], K_STD_BUF_SIZE);
334335

335336
eos = strlen(fname) - 1;
336337

337338
if (fname[eos] == '\n')
338339
fname[eos] = '\0';
339-
340340
}
341341
else
342342
{
343-
344343
*oid = 0;
345344
fname[0] = '\0';
346345
}

src/bin/psql/common.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.132 2007/01/05 22:19:49 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.133 2007/02/08 11:10:27 petere Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -1497,7 +1497,7 @@ expand_tilde(char **filename)
14971497
if (*(fn + 1) == '\0')
14981498
get_home_path(home); /* ~ or ~/ only */
14991499
else if ((pw = getpwnam(fn + 1)) != NULL)
1500-
StrNCpy(home, pw->pw_dir, MAXPGPATH); /* ~user */
1500+
strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
15011501

15021502
*p = oldp;
15031503
if (strlen(home) != 0)

src/bin/psql/copy.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.73 2007/02/05 15:22:18 adunstan Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.74 2007/02/08 11:10:27 petere Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "copy.h"
@@ -801,7 +801,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary)
801801
/* enable longjmp while waiting for input */
802802
sigint_interrupt_enabled = true;
803803

804-
fgresult = fgets(buf, COPYBUFSIZ, copystream);
804+
fgresult = fgets(buf, sizeof(buf), copystream);
805805

806806
sigint_interrupt_enabled = false;
807807

src/bin/psql/prompt.c

+14-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.49 2007/01/05 22:19:49 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.50 2007/02/08 11:10:27 petere Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -96,18 +96,18 @@ get_prompt(promptStatus_t status)
9696
destination[0] = '\0';
9797

9898
for (p = prompt_string;
99-
*p && strlen(destination) < MAX_PROMPT_SIZE;
99+
*p && strlen(destination) < sizeof(destination) - 1;
100100
p++)
101101
{
102-
memset(buf, 0, MAX_PROMPT_SIZE + 1);
102+
memset(buf, 0, sizeof(buf));
103103
if (esc)
104104
{
105105
switch (*p)
106106
{
107107
/* Current database */
108108
case '/':
109109
if (pset.db)
110-
strncpy(buf, PQdb(pset.db), MAX_PROMPT_SIZE);
110+
strlcpy(buf, PQdb(pset.db), sizeof(buf));
111111
break;
112112
case '~':
113113
if (pset.db)
@@ -116,9 +116,9 @@ get_prompt(promptStatus_t status)
116116

117117
if (strcmp(PQdb(pset.db), PQuser(pset.db)) == 0 ||
118118
((var = getenv("PGDATABASE")) && strcmp(var, PQdb(pset.db)) == 0))
119-
strcpy(buf, "~");
119+
strlcpy(buf, "~", sizeof(buf));
120120
else
121-
strncpy(buf, PQdb(pset.db), MAX_PROMPT_SIZE);
121+
strlcpy(buf, PQdb(pset.db), sizeof(buf));
122122
}
123123
break;
124124

@@ -132,7 +132,7 @@ get_prompt(promptStatus_t status)
132132
/* INET socket */
133133
if (host && host[0] && !is_absolute_path(host))
134134
{
135-
strncpy(buf, host, MAX_PROMPT_SIZE);
135+
strlcpy(buf, host, sizeof(buf));
136136
if (*p == 'm')
137137
buf[strcspn(buf, ".")] = '\0';
138138
}
@@ -143,22 +143,22 @@ get_prompt(promptStatus_t status)
143143
if (!host
144144
|| strcmp(host, DEFAULT_PGSOCKET_DIR) == 0
145145
|| *p == 'm')
146-
strncpy(buf, "[local]", MAX_PROMPT_SIZE);
146+
strlcpy(buf, "[local]", sizeof(buf));
147147
else
148-
snprintf(buf, MAX_PROMPT_SIZE, "[local:%s]", host);
148+
snprintf(buf, sizeof(buf), "[local:%s]", host);
149149
}
150150
#endif
151151
}
152152
break;
153153
/* DB server port number */
154154
case '>':
155155
if (pset.db && PQport(pset.db))
156-
strncpy(buf, PQport(pset.db), MAX_PROMPT_SIZE);
156+
strlcpy(buf, PQport(pset.db), sizeof(buf));
157157
break;
158158
/* DB server user name */
159159
case 'n':
160160
if (pset.db)
161-
strncpy(buf, session_username(), MAX_PROMPT_SIZE);
161+
strlcpy(buf, session_username(), sizeof(buf));
162162
break;
163163

164164
case '0':
@@ -252,7 +252,7 @@ get_prompt(promptStatus_t status)
252252
fd = popen(file, "r");
253253
if (fd)
254254
{
255-
fgets(buf, MAX_PROMPT_SIZE - 1, fd);
255+
fgets(buf, sizeof(buf), fd);
256256
pclose(fd);
257257
}
258258
if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status)
274274
name[nameend] = '\0';
275275
val = GetVariable(pset.vars, name);
276276
if (val)
277-
strncpy(buf, val, MAX_PROMPT_SIZE);
277+
strlcpy(buf, val, sizeof(buf));
278278
free(name);
279279
p += nameend + 1;
280280
break;
@@ -312,9 +312,8 @@ get_prompt(promptStatus_t status)
312312
}
313313

314314
if (!esc)
315-
strncat(destination, buf, MAX_PROMPT_SIZE - strlen(destination));
315+
strlcat(destination, buf, sizeof(destination));
316316
}
317317

318-
destination[MAX_PROMPT_SIZE] = '\0';
319318
return destination;
320319
}

src/interfaces/libpq/fe-connect.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.341 2007/01/05 22:20:00 momjian Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.342 2007/02/08 11:10:27 petere Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2845,11 +2845,11 @@ parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage)
28452845
return 1;
28462846
}
28472847

2848-
while ((line = fgets(buf, MAXBUFSIZE - 1, f)) != NULL)
2848+
while ((line = fgets(buf, sizeof(buf), f)) != NULL)
28492849
{
28502850
linenr++;
28512851

2852-
if (strlen(line) >= MAXBUFSIZE - 2)
2852+
if (strlen(line) >= sizeof(buf) - 1)
28532853
{
28542854
fclose(f);
28552855
printfPQExpBuffer(errorMessage,
@@ -3654,7 +3654,7 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
36543654
*ret;
36553655
int len;
36563656

3657-
fgets(buf, LINELEN - 1, fp);
3657+
fgets(buf, sizeof(buf), fp);
36583658

36593659
len = strlen(buf);
36603660
if (len == 0)

src/interfaces/libpq/fe-secure.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.91 2007/01/26 17:45:41 neilc Exp $
14+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.92 2007/02/08 11:10:27 petere Exp $
1515
*
1616
* NOTES
1717
* [ Most of these notes are wrong/obsolete, but perhaps not all ]
@@ -1018,8 +1018,7 @@ SSLerrmessage(void)
10181018
errreason = ERR_reason_error_string(errcode);
10191019
if (errreason != NULL)
10201020
{
1021-
strncpy(errbuf, errreason, SSL_ERR_LEN - 1);
1022-
errbuf[SSL_ERR_LEN - 1] = '\0';
1021+
strlcpy(errbuf, errreason, SSL_ERR_LEN);
10231022
return errbuf;
10241023
}
10251024
snprintf(errbuf, SSL_ERR_LEN, libpq_gettext("SSL error code %lu"), errcode);

src/tools/entab/entab.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
** entab.c - add tabs to a text file
33
** by Bruce Momjian (root@candle.pha.pa.us)
44
**
5-
** $PostgreSQL: pgsql/src/tools/entab/entab.c,v 1.17 2007/02/01 19:10:30 momjian Exp $
5+
** $PostgreSQL: pgsql/src/tools/entab/entab.c,v 1.18 2007/02/08 11:10:27 petere Exp $
66
**
77
** version 1.3
88
**
@@ -108,7 +108,7 @@ main(int argc, char **argv)
108108

109109
escaped = FALSE;
110110

111-
while (fgets(in_line, BUFSIZ, in_file) != NULL)
111+
while (fgets(in_line, sizeof(in_line), in_file) != NULL)
112112
{
113113
col_in_tab = 0;
114114
prv_spaces = 0;

0 commit comments

Comments
 (0)