Re: [BUGS] COPY allows parameters which corrupt output - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [BUGS] COPY allows parameters which corrupt output
Date
Msg-id 200404061321.i36DLxL14289@candle.pha.pa.us
Whole thread Raw
List pgsql-patches
Kris Jurka wrote:
>
> COPY allows DELIMITER and NULL to be specified which don't allow the data
> to be copied back in properly.  Using any delimiter that could be part of
> a backslash escape sequence (\n \0 \t) will break if any of the data has a
> character matching the delimiter because it will be escaped and then be
> read as a special character.
>
> It also allows DELIMITER and NULL to overlap.  No character in the NULL
> specification should be the DELIMITER.

The attached applied patch throws an error if the delimiter appears in
the COPY NULL string:

    test=> copy pg_language to '/tmp/x' with delimiter '|';
    COPY
    test=> copy pg_language to '/tmp/x' with delimiter '|' null '|x';
    ERROR:  COPY delimiter must not appear in the NULL specification
    test=> copy pg_language from '/tmp/x' with delimiter '|' null '|x';
    ERROR:  COPY delimiter must not appear in the NULL specification

It also throws an error if it conflicts with the default NULL string:

    test=> copy pg_language to '/tmp/x' with delimiter '\\';
    ERROR:  COPY delimiter must not appear in the NULL specification
    test=> copy pg_language to '/tmp/x' with delimiter '\\' NULL 'x';
    COPY

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/backend/commands/copy.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/commands/copy.c,v
retrieving revision 1.218
diff -c -c -r1.218 copy.c
*** src/backend/commands/copy.c    10 Feb 2004 01:55:24 -0000    1.218
--- src/backend/commands/copy.c    6 Apr 2004 13:16:35 -0000
***************
*** 780,785 ****
--- 780,793 ----
                   errmsg("COPY delimiter must be a single character")));

      /*
+      * Don't allow the delimiter to appear in the null string.
+      */
+     if (strchr(null_print, delim[0]) != NULL)
+         ereport(ERROR,
+                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                  errmsg("COPY delimiter must not appear in the NULL specification")));
+
+     /*
       * Don't allow COPY w/ OIDs to or from a table without them
       */
      if (oids && !rel->rd_rel->relhasoids)

pgsql-patches by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: [HACKERS] logging statement levels
Next
From: Tom Lane
Date:
Subject: Re: hint infrastructure setup (v3)