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...
This commit is contained in:
parent
4451ed3dfe
commit
a6944611e2
@ -7,7 +7,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.127 2001/01/03 20:04:10 tgl Exp $
|
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.128 2001/01/06 03:33:17 ishii Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -74,8 +74,8 @@ static bool fe_eof;
|
|||||||
static StringInfoData attribute_buf;
|
static StringInfoData attribute_buf;
|
||||||
|
|
||||||
#ifdef MULTIBYTE
|
#ifdef MULTIBYTE
|
||||||
static int encoding;
|
static int client_encoding;
|
||||||
|
static int server_encoding;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@ -297,7 +297,8 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
|
|||||||
*/
|
*/
|
||||||
initStringInfo(&attribute_buf);
|
initStringInfo(&attribute_buf);
|
||||||
#ifdef MULTIBYTE
|
#ifdef MULTIBYTE
|
||||||
encoding = pg_get_client_encoding();
|
client_encoding = pg_get_client_encoding();
|
||||||
|
server_encoding = GetDatabaseEncoding();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (from)
|
if (from)
|
||||||
@ -1114,9 +1115,11 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_
|
|||||||
}
|
}
|
||||||
appendStringInfoCharMacro(&attribute_buf, c);
|
appendStringInfoCharMacro(&attribute_buf, c);
|
||||||
#ifdef MULTIBYTE
|
#ifdef MULTIBYTE
|
||||||
|
if (client_encoding != server_encoding)
|
||||||
|
{
|
||||||
/* get additional bytes of the char, if any */
|
/* get additional bytes of the char, if any */
|
||||||
s[0] = c;
|
s[0] = c;
|
||||||
mblen = pg_encoding_mblen(encoding, s);
|
mblen = pg_encoding_mblen(client_encoding, s);
|
||||||
for (j = 1; j < mblen; j++)
|
for (j = 1; j < mblen; j++)
|
||||||
{
|
{
|
||||||
c = CopyGetChar(fp);
|
c = CopyGetChar(fp);
|
||||||
@ -1124,10 +1127,13 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_
|
|||||||
goto endOfFile;
|
goto endOfFile;
|
||||||
appendStringInfoCharMacro(&attribute_buf, c);
|
appendStringInfoCharMacro(&attribute_buf, c);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MULTIBYTE
|
#ifdef MULTIBYTE
|
||||||
|
if (client_encoding != server_encoding)
|
||||||
|
{
|
||||||
cvt = (char *) pg_client_to_server((unsigned char *) attribute_buf.data,
|
cvt = (char *) pg_client_to_server((unsigned char *) attribute_buf.data,
|
||||||
attribute_buf.len);
|
attribute_buf.len);
|
||||||
if (cvt != attribute_buf.data)
|
if (cvt != attribute_buf.data)
|
||||||
@ -1138,6 +1144,7 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_
|
|||||||
appendBinaryStringInfo(&attribute_buf, cvt, strlen(cvt));
|
appendBinaryStringInfo(&attribute_buf, cvt, strlen(cvt));
|
||||||
pfree(cvt);
|
pfree(cvt);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (strcmp(attribute_buf.data, null_print) == 0)
|
if (strcmp(attribute_buf.data, null_print) == 0)
|
||||||
@ -1163,15 +1170,22 @@ CopyAttributeOut(FILE *fp, char *server_string, char *delim)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef MULTIBYTE
|
#ifdef MULTIBYTE
|
||||||
|
if (client_encoding != server_encoding)
|
||||||
|
{
|
||||||
string = (char *) pg_server_to_client((unsigned char *) server_string,
|
string = (char *) pg_server_to_client((unsigned char *) server_string,
|
||||||
strlen(server_string));
|
strlen(server_string));
|
||||||
string_start = string;
|
string_start = string;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string = server_string;
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
string = server_string;
|
string = server_string;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef MULTIBYTE
|
#ifdef MULTIBYTE
|
||||||
for (; (mblen = pg_encoding_mblen(encoding, string)) &&
|
for (; (mblen = (server_encoding == client_encoding? 1 : pg_encoding_mblen(client_encoding, string))) &&
|
||||||
((c = *string) != '\0'); string += mblen)
|
((c = *string) != '\0'); string += mblen)
|
||||||
#else
|
#else
|
||||||
for (; (c = *string) != '\0'; string++)
|
for (; (c = *string) != '\0'; string++)
|
||||||
@ -1188,7 +1202,7 @@ CopyAttributeOut(FILE *fp, char *server_string, char *delim)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MULTIBYTE
|
#ifdef MULTIBYTE
|
||||||
if (string_start != server_string)
|
if (client_encoding != server_encoding)
|
||||||
pfree(string_start); /* pfree pg_server_to_client result */
|
pfree(string_start); /* pfree pg_server_to_client result */
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user