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

Commit a694461

Browse files
committed
Fix copy to make it more robust against unexpected character
sequences. This is done by disabling multi-byte awareness when it's not necessary. This is kind of a workaround, not a perfect solution. However, there is no ideal way to parse broken multi-byte character sequences. So I guess this is the best way what we could do right now...
1 parent 4451ed3 commit a694461

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

src/backend/commands/copy.c

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.127 2001/01/03 20:04:10 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.128 2001/01/06 03:33:17 ishii Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -74,8 +74,8 @@ static bool fe_eof;
7474
static StringInfoData attribute_buf;
7575

7676
#ifdef MULTIBYTE
77-
static int encoding;
78-
77+
static int client_encoding;
78+
static int server_encoding;
7979
#endif
8080

8181

@@ -297,7 +297,8 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
297297
*/
298298
initStringInfo(&attribute_buf);
299299
#ifdef MULTIBYTE
300-
encoding = pg_get_client_encoding();
300+
client_encoding = pg_get_client_encoding();
301+
server_encoding = GetDatabaseEncoding();
301302
#endif
302303

303304
if (from)
@@ -1114,29 +1115,35 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_
11141115
}
11151116
appendStringInfoCharMacro(&attribute_buf, c);
11161117
#ifdef MULTIBYTE
1117-
/* get additional bytes of the char, if any */
1118-
s[0] = c;
1119-
mblen = pg_encoding_mblen(encoding, s);
1120-
for (j = 1; j < mblen; j++)
1118+
if (client_encoding != server_encoding)
11211119
{
1122-
c = CopyGetChar(fp);
1123-
if (c == EOF)
1124-
goto endOfFile;
1125-
appendStringInfoCharMacro(&attribute_buf, c);
1120+
/* get additional bytes of the char, if any */
1121+
s[0] = c;
1122+
mblen = pg_encoding_mblen(client_encoding, s);
1123+
for (j = 1; j < mblen; j++)
1124+
{
1125+
c = CopyGetChar(fp);
1126+
if (c == EOF)
1127+
goto endOfFile;
1128+
appendStringInfoCharMacro(&attribute_buf, c);
1129+
}
11261130
}
11271131
#endif
11281132
}
11291133

11301134
#ifdef MULTIBYTE
1131-
cvt = (char *) pg_client_to_server((unsigned char *) attribute_buf.data,
1132-
attribute_buf.len);
1133-
if (cvt != attribute_buf.data)
1135+
if (client_encoding != server_encoding)
11341136
{
1135-
/* transfer converted data back to attribute_buf */
1136-
attribute_buf.len = 0;
1137-
attribute_buf.data[0] = '\0';
1138-
appendBinaryStringInfo(&attribute_buf, cvt, strlen(cvt));
1139-
pfree(cvt);
1137+
cvt = (char *) pg_client_to_server((unsigned char *) attribute_buf.data,
1138+
attribute_buf.len);
1139+
if (cvt != attribute_buf.data)
1140+
{
1141+
/* transfer converted data back to attribute_buf */
1142+
attribute_buf.len = 0;
1143+
attribute_buf.data[0] = '\0';
1144+
appendBinaryStringInfo(&attribute_buf, cvt, strlen(cvt));
1145+
pfree(cvt);
1146+
}
11401147
}
11411148
#endif
11421149

@@ -1163,15 +1170,22 @@ CopyAttributeOut(FILE *fp, char *server_string, char *delim)
11631170
#endif
11641171

11651172
#ifdef MULTIBYTE
1166-
string = (char *) pg_server_to_client((unsigned char *) server_string,
1167-
strlen(server_string));
1168-
string_start = string;
1173+
if (client_encoding != server_encoding)
1174+
{
1175+
string = (char *) pg_server_to_client((unsigned char *) server_string,
1176+
strlen(server_string));
1177+
string_start = string;
1178+
}
1179+
else
1180+
{
1181+
string = server_string;
1182+
}
11691183
#else
11701184
string = server_string;
11711185
#endif
11721186

11731187
#ifdef MULTIBYTE
1174-
for (; (mblen = pg_encoding_mblen(encoding, string)) &&
1188+
for (; (mblen = (server_encoding == client_encoding? 1 : pg_encoding_mblen(client_encoding, string))) &&
11751189
((c = *string) != '\0'); string += mblen)
11761190
#else
11771191
for (; (c = *string) != '\0'; string++)
@@ -1188,7 +1202,7 @@ CopyAttributeOut(FILE *fp, char *server_string, char *delim)
11881202
}
11891203

11901204
#ifdef MULTIBYTE
1191-
if (string_start != server_string)
1205+
if (client_encoding != server_encoding)
11921206
pfree(string_start); /* pfree pg_server_to_client result */
11931207
#endif
11941208
}

0 commit comments

Comments
 (0)