Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9facc58

Browse files
committed
Fix use of 'char' to hold result of getc, per bug report forwarded by
Oliver Elphick. A few other minor cleanups while at it.
1 parent 8ff263f commit 9facc58

File tree

2 files changed

+111
-125
lines changed

2 files changed

+111
-125
lines changed

src/backend/libpq/hba.c

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.78 2001/11/12 04:29:23 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.79 2002/01/09 19:13:40 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -111,7 +111,7 @@ next_token(FILE *fp, char *buf, const int bufsz)
111111

112112

113113
static void
114-
read_to_eol(FILE *file)
114+
read_through_eol(FILE *file)
115115
{
116116
int c;
117117

@@ -162,7 +162,7 @@ tokenize_file(FILE *file)
162162
if (comment_ptr != NULL)
163163
{
164164
/* Found a comment, so skip the rest of the line */
165-
read_to_eol(file);
165+
read_through_eol(file);
166166
next_line = NIL;
167167
}
168168

@@ -1159,110 +1159,103 @@ GetCharSetByHost(char *TableName, int host, const char *DataDir)
11591159
OrigCharset[MAX_TOKEN],
11601160
DestCharset[MAX_TOKEN],
11611161
HostCharset[MAX_TOKEN],
1162-
c,
1163-
eof = false,
11641162
*map_file;
1165-
int key = 0,
1163+
int key,
11661164
ChIndex = 0,
1165+
c,
11671166
i,
11681167
bufsize;
1169-
11701168
struct CharsetItem *ChArray[MAX_CHARSETS];
11711169

11721170
*TableName = '\0';
11731171
bufsize = (strlen(DataDir) + strlen(CHARSET_FILE) + 2) * sizeof(char);
11741172
map_file = (char *) palloc(bufsize);
11751173
snprintf(map_file, bufsize, "%s/%s", DataDir, CHARSET_FILE);
11761174
file = AllocateFile(map_file, PG_BINARY_R);
1175+
pfree(map_file);
11771176
if (file == NULL)
11781177
{
11791178
/* XXX should we log a complaint? */
11801179
return;
11811180
}
1182-
while (!eof)
1181+
while ((c = getc(file)) != EOF)
11831182
{
1184-
c = getc(file);
1185-
ungetc(c, file);
1186-
if (c == EOF)
1187-
eof = true;
1183+
if (c == '#')
1184+
read_through_eol(file);
11881185
else
11891186
{
1190-
if (c == '#')
1191-
read_to_eol(file);
1192-
else
1187+
/* Read the key */
1188+
ungetc(c, file);
1189+
next_token(file, buf, sizeof(buf));
1190+
if (buf[0] != '\0')
11931191
{
1194-
/* Read the key */
1195-
next_token(file, buf, sizeof(buf));
1196-
if (buf[0] != '\0')
1192+
key = 0;
1193+
if (strcasecmp(buf, "HostCharset") == 0)
1194+
key = KEY_HOST;
1195+
if (strcasecmp(buf, "BaseCharset") == 0)
1196+
key = KEY_BASE;
1197+
if (strcasecmp(buf, "RecodeTable") == 0)
1198+
key = KEY_TABLE;
1199+
switch (key)
11971200
{
1198-
if (strcasecmp(buf, "HostCharset") == 0)
1199-
key = KEY_HOST;
1200-
if (strcasecmp(buf, "BaseCharset") == 0)
1201-
key = KEY_BASE;
1202-
if (strcasecmp(buf, "RecodeTable") == 0)
1203-
key = KEY_TABLE;
1204-
switch (key)
1205-
{
1206-
case KEY_HOST:
1207-
/* Read the host */
1208-
next_token(file, buf, sizeof(buf));
1209-
if (buf[0] != '\0')
1201+
case KEY_HOST:
1202+
/* Read the host */
1203+
next_token(file, buf, sizeof(buf));
1204+
if (buf[0] != '\0')
1205+
{
1206+
if (CharSetInRange(buf, host))
12101207
{
1211-
if (CharSetInRange(buf, host))
1212-
{
1213-
/* Read the charset */
1214-
next_token(file, buf, sizeof(buf));
1215-
if (buf[0] != '\0')
1216-
strcpy(HostCharset, buf);
1217-
}
1208+
/* Read the charset */
1209+
next_token(file, buf, sizeof(buf));
1210+
if (buf[0] != '\0')
1211+
strcpy(HostCharset, buf);
12181212
}
1219-
break;
1220-
case KEY_BASE:
1221-
/* Read the base charset */
1222-
next_token(file, buf, sizeof(buf));
1223-
if (buf[0] != '\0')
1224-
strcpy(BaseCharset, buf);
1225-
break;
1226-
case KEY_TABLE:
1227-
/* Read the original charset */
1213+
}
1214+
break;
1215+
case KEY_BASE:
1216+
/* Read the base charset */
1217+
next_token(file, buf, sizeof(buf));
1218+
if (buf[0] != '\0')
1219+
strcpy(BaseCharset, buf);
1220+
break;
1221+
case KEY_TABLE:
1222+
/* Read the original charset */
1223+
next_token(file, buf, sizeof(buf));
1224+
if (buf[0] != '\0')
1225+
{
1226+
strcpy(OrigCharset, buf);
1227+
/* Read the destination charset */
12281228
next_token(file, buf, sizeof(buf));
12291229
if (buf[0] != '\0')
12301230
{
1231-
strcpy(OrigCharset, buf);
1232-
/* Read the destination charset */
1231+
strcpy(DestCharset, buf);
1232+
/* Read the table filename */
12331233
next_token(file, buf, sizeof(buf));
12341234
if (buf[0] != '\0')
12351235
{
1236-
strcpy(DestCharset, buf);
1237-
/* Read the table filename */
1238-
next_token(file, buf, sizeof(buf));
1239-
if (buf[0] != '\0')
1240-
{
1241-
ChArray[ChIndex] =
1242-
(struct CharsetItem *) palloc(sizeof(struct CharsetItem));
1243-
strcpy(ChArray[ChIndex]->Orig, OrigCharset);
1244-
strcpy(ChArray[ChIndex]->Dest, DestCharset);
1245-
strcpy(ChArray[ChIndex]->Table, buf);
1246-
ChIndex++;
1247-
}
1236+
ChArray[ChIndex] =
1237+
(struct CharsetItem *) palloc(sizeof(struct CharsetItem));
1238+
strcpy(ChArray[ChIndex]->Orig, OrigCharset);
1239+
strcpy(ChArray[ChIndex]->Dest, DestCharset);
1240+
strcpy(ChArray[ChIndex]->Table, buf);
1241+
ChIndex++;
12481242
}
12491243
}
1250-
break;
1251-
}
1252-
read_to_eol(file);
1244+
}
1245+
break;
12531246
}
1247+
read_through_eol(file);
12541248
}
12551249
}
12561250
}
12571251
FreeFile(file);
1258-
pfree(map_file);
12591252

12601253
for (i = 0; i < ChIndex; i++)
12611254
{
1262-
if (!strcasecmp(BaseCharset, ChArray[i]->Orig) &&
1263-
!strcasecmp(HostCharset, ChArray[i]->Dest))
1255+
if (strcasecmp(BaseCharset, ChArray[i]->Orig) == 0 &&
1256+
strcasecmp(HostCharset, ChArray[i]->Dest) == 0)
12641257
strncpy(TableName, ChArray[i]->Table, 79);
1265-
pfree((struct CharsetItem *) ChArray[i]);
1258+
pfree(ChArray[i]);
12661259
}
12671260
}
12681261

src/backend/utils/init/miscinit.c

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.81 2001/10/25 05:49:51 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.82 2002/01/09 19:13:41 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -227,75 +227,80 @@ pg_convert2(PG_FUNCTION_ARGS)
227227

228228
#define MAX_TOKEN 80
229229

230-
/* Some standard C libraries, including GNU, have an isblank() function.
231-
Others, including Solaris, do not. So we have our own.
232-
*/
230+
/*
231+
* Some standard C libraries, including GNU, have an isblank() function.
232+
* Others, including Solaris, do not. So we have our own.
233+
*/
233234
static bool
234235
isblank(const char c)
235236
{
236-
return c == ' ' || c == 9 /* tab */ ;
237+
return c == ' ' || c == '\t';
237238
}
238239

240+
241+
/*
242+
* Grab one token out of fp. Tokens are strings of non-blank
243+
* characters bounded by blank characters, beginning of line, and end
244+
* of line. Blank means space or tab. Return the token as *buf.
245+
* Leave file positioned to character immediately after the token or
246+
* EOF, whichever comes first. If no more tokens on line, return null
247+
* string as *buf and position file to beginning of next line or EOF,
248+
* whichever comes first.
249+
*/
239250
static void
240251
next_token(FILE *fp, char *buf, const int bufsz)
241252
{
242-
/*--------------------------------------------------------------------------
243-
Grab one token out of fp. Tokens are strings of non-blank
244-
characters bounded by blank characters, beginning of line, and end
245-
of line. Blank means space or tab. Return the token as *buf.
246-
Leave file positioned to character immediately after the token or
247-
EOF, whichever comes first. If no more tokens on line, return null
248-
string as *buf and position file to beginning of next line or EOF,
249-
whichever comes first.
250-
--------------------------------------------------------------------------*/
251253
int c;
252254
char *eb = buf + (bufsz - 1);
253255

254-
/* Move over inital token-delimiting blanks */
255-
while (isblank(c = getc(fp)));
256+
/* Move over initial token-delimiting blanks */
257+
while ((c = getc(fp)) != EOF && isblank(c))
258+
;
256259

257-
if (c != '\n')
260+
if (c != EOF && c != '\n')
258261
{
259262
/*
260263
* build a token in buf of next characters up to EOF, eol, or
261-
* blank.
264+
* blank. If the token gets too long, we still parse it
265+
* correctly, but the excess characters are not stored into *buf.
262266
*/
263267
while (c != EOF && c != '\n' && !isblank(c))
264268
{
265269
if (buf < eb)
266270
*buf++ = c;
267271
c = getc(fp);
268-
269-
/*
270-
* Put back the char right after the token (putting back EOF
271-
* is ok)
272-
*/
273272
}
274-
ungetc(c, fp);
273+
274+
/*
275+
* Put back the char right after the token (critical in case it is
276+
* eol, since we need to detect end-of-line at next call).
277+
*/
278+
if (c != EOF)
279+
ungetc(c, fp);
275280
}
276281
*buf = '\0';
277282
}
278283

284+
279285
static void
280286
read_through_eol(FILE *file)
281287
{
282288
int c;
283289

284-
do
285-
c = getc(file);
286-
while (c != '\n' && c != EOF);
290+
while ((c = getc(file)) != EOF && c != '\n')
291+
;
287292
}
288293

294+
289295
void
290-
SetCharSet()
296+
SetCharSet(void)
291297
{
292298
FILE *file;
293-
char *p,
294-
c,
295-
eof = false;
299+
char *p;
296300
char *map_file;
297301
char buf[MAX_TOKEN];
298-
int i;
302+
int i,
303+
c;
299304
unsigned char FromChar,
300305
ToChar;
301306
char ChTable[80];
@@ -316,49 +321,37 @@ SetCharSet()
316321

317322
if (p && *p != '\0')
318323
{
319-
map_file = malloc(strlen(DataDir) + strlen(p) + 2);
320-
if (!map_file)
321-
elog(FATAL, "out of memory");
324+
map_file = palloc(strlen(DataDir) + strlen(p) + 2);
322325
sprintf(map_file, "%s/%s", DataDir, p);
323326
file = AllocateFile(map_file, PG_BINARY_R);
327+
pfree(map_file);
324328
if (file == NULL)
325-
{
326-
free(map_file);
327329
return;
328-
}
329-
eof = false;
330-
while (!eof)
330+
while ((c = getc(file)) != EOF)
331331
{
332-
c = getc(file);
333-
ungetc(c, file);
334-
if (c == EOF)
335-
eof = true;
332+
if (c == '#')
333+
read_through_eol(file);
336334
else
337335
{
338-
if (c == '#')
339-
read_through_eol(file);
340-
else
336+
/* Read the FromChar */
337+
ungetc(c, file);
338+
next_token(file, buf, sizeof(buf));
339+
if (buf[0] != '\0')
341340
{
342-
/* Read the FromChar */
341+
FromChar = strtoul(buf, 0, 0);
342+
/* Read the ToChar */
343343
next_token(file, buf, sizeof(buf));
344344
if (buf[0] != '\0')
345345
{
346-
FromChar = strtoul(buf, 0, 0);
347-
/* Read the ToChar */
348-
next_token(file, buf, sizeof(buf));
349-
if (buf[0] != '\0')
350-
{
351-
ToChar = strtoul(buf, 0, 0);
352-
RecodeForwTable[FromChar - 128] = ToChar;
353-
RecodeBackTable[ToChar - 128] = FromChar;
354-
}
346+
ToChar = strtoul(buf, 0, 0);
347+
RecodeForwTable[FromChar - 128] = ToChar;
348+
RecodeBackTable[ToChar - 128] = FromChar;
355349
read_through_eol(file);
356350
}
357351
}
358352
}
359353
}
360354
FreeFile(file);
361-
free(map_file);
362355
}
363356
}
364357

0 commit comments

Comments
 (0)