Re: bug in COPY - Mailing list pgsql-hackers

From Bruce Momjian
Subject Re: bug in COPY
Date
Msg-id 200208271846.g7RIkYZ28983@candle.pha.pa.us
Whole thread Raw
In response to Re: bug in COPY  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: bug in COPY
List pgsql-hackers
Tom Lane wrote:
> A subsidiary point here is that pg_atoi() explicitly takes a zero-length
> string as valid input of value 0.  I think this is quite bogus myself,
> but I don't know why that behavior was put in or whether we'd be breaking
> anything if we tightened it up.

I have attached a patch the throws an error if pg_atoi() is passed a
zero-length string, and have included regression diffs showing the
effects of the patch.

Seems the new code catches a few places that were bad, like defineing {}
for an array of 0,0.  The copy2.out change is because pg_atoi catches
the problem before COPY does.

The tightening up of pg_atoi seems safe and makes sense to me.

If no adverse comments, I will apply and fix up the regression results.

--
  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/utils/adt/numutils.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/utils/adt/numutils.c,v
retrieving revision 1.51
diff -c -c -r1.51 numutils.c
*** src/backend/utils/adt/numutils.c    16 Jul 2002 18:34:16 -0000    1.51
--- src/backend/utils/adt/numutils.c    27 Aug 2002 17:51:45 -0000
***************
*** 60,66 ****
      if (s == (char *) NULL)
          elog(ERROR, "pg_atoi: NULL pointer!");
      else if (*s == 0)
!         l = (long) 0;
      else
          l = strtol(s, &badp, 10);

--- 60,66 ----
      if (s == (char *) NULL)
          elog(ERROR, "pg_atoi: NULL pointer!");
      else if (*s == 0)
!         elog(ERROR, "pg_atoi: zero-length string!");
      else
          l = strtol(s, &badp, 10);

*** ./expected/arrays.out    Thu Nov 29 16:02:41 2001
--- ./results/arrays.out    Tue Aug 27 14:14:47 2002
***************
*** 16,21 ****
--- 16,22 ----
  --
  INSERT INTO arrtest (a[5], b[2][1][2], c, d, f, g)
     VALUES ('{1,2,3,4,5}', '{{{},{1,2}}}', '{}', '{}', '{}', '{}');
+ ERROR:  pg_atoi: zero-length string!
  UPDATE arrtest SET e[0] = '1.1';
  UPDATE arrtest SET e[1] = '2.2';
  INSERT INTO arrtest (f)
***************
*** 29,39 ****
     VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');
  SELECT * FROM arrtest;
        a      |        b        |     c     |       d       |     e     |        f        |      g
! -------------+-----------------+-----------+---------------+-----------+-----------------+-------------
!  {1,2,3,4,5} | {{{0,0},{1,2}}} | {}        | {}            |           | {}              | {}
   {11,12,23}  | {{3,4},{4,5}}   | {foobar}  | {{elt1,elt2}} | {3.4,6.7} | {"abc  ",abcde} | {abc,abcde}
   {}          | {3,4}           | {foo,bar} | {bar,foo}     |           |                 |
! (3 rows)

  SELECT arrtest.a[1],
            arrtest.b[1][1][1],
--- 30,39 ----
     VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');
  SELECT * FROM arrtest;
       a      |       b       |     c     |       d       |     e     |        f        |      g
! ------------+---------------+-----------+---------------+-----------+-----------------+-------------
   {11,12,23} | {{3,4},{4,5}} | {foobar}  | {{elt1,elt2}} | {3.4,6.7} | {"abc  ",abcde} | {abc,abcde}
   {}         | {3,4}         | {foo,bar} | {bar,foo}     |           |                 |
! (2 rows)

  SELECT arrtest.a[1],
            arrtest.b[1][1][1],
***************
*** 43,61 ****
     FROM arrtest;
   a  | b |   c    |  d   | e
  ----+---+--------+------+---
-   1 | 0 |        |      |
   11 |   | foobar | elt1 |
      |   | foo    |      |
! (3 rows)

  SELECT a[1], b[1][1][1], c[1], d[1][1], e[0]
     FROM arrtest;
   a  | b |   c    |  d   | e
  ----+---+--------+------+---
-   1 | 0 |        |      |
   11 |   | foobar | elt1 |
      |   | foo    |      |
! (3 rows)

  SELECT a[1:3],
            b[1:1][1:2][1:2],
--- 43,59 ----
     FROM arrtest;
   a  | b |   c    |  d   | e
  ----+---+--------+------+---
   11 |   | foobar | elt1 |
      |   | foo    |      |
! (2 rows)

  SELECT a[1], b[1][1][1], c[1], d[1][1], e[0]
     FROM arrtest;
   a  | b |   c    |  d   | e
  ----+---+--------+------+---
   11 |   | foobar | elt1 |
      |   | foo    |      |
! (2 rows)

  SELECT a[1:3],
            b[1:1][1:2][1:2],
***************
*** 63,82 ****
            d[1:1][1:2]
     FROM arrtest;
       a      |        b        |     c     |       d
! ------------+-----------------+-----------+---------------
!  {1,2,3}    | {{{0,0},{1,2}}} |           |
   {11,12,23} |                 | {foobar}  | {{elt1,elt2}}
              |                 | {foo,bar} |
! (3 rows)

  SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c
     FROM arrtest;
     a   |        b        |   c
! -------+-----------------+-------
!  [1:5] | [1:1][1:2][1:2] |
   [1:3] | [1:2][1:2]      | [1:1]
         | [1:2]           | [1:2]
! (3 rows)

  -- returns nothing
  SELECT *
--- 61,78 ----
            d[1:1][1:2]
     FROM arrtest;
       a      | b |     c     |       d
! ------------+---+-----------+---------------
   {11,12,23} |   | {foobar}  | {{elt1,elt2}}
              |   | {foo,bar} |
! (2 rows)

  SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c
     FROM arrtest;
     a   |     b      |   c
! -------+------------+-------
   [1:3] | [1:2][1:2] | [1:1]
         | [1:2]      | [1:2]
! (2 rows)

  -- returns nothing
  SELECT *
***************
*** 99,109 ****
    WHERE array_dims(c) is not null;
  SELECT a,b,c FROM arrtest;
         a       |           b           |         c
! ---------------+-----------------------+-------------------
!  {16,25,3,4,5} | {{{113,142},{1,147}}} | {}
   {}            | {3,4}                 | {foo,new_word}
   {16,25,23}    | {{3,4},{4,5}}         | {foobar,new_word}
! (3 rows)

  SELECT a[1:3],
            b[1:1][1:2][1:2],
--- 95,104 ----
    WHERE array_dims(c) is not null;
  SELECT a,b,c FROM arrtest;
       a      |       b       |         c
! ------------+---------------+-------------------
   {}         | {3,4}         | {foo,new_word}
   {16,25,23} | {{3,4},{4,5}} | {foobar,new_word}
! (2 rows)

  SELECT a[1:3],
            b[1:1][1:2][1:2],
***************
*** 111,119 ****
            d[1:1][2:2]
     FROM arrtest;
       a      |           b           |         c         |    d
! ------------+-----------------------+-------------------+----------
!  {16,25,3}  | {{{113,142},{1,147}}} |                   |
              |                       | {foo,new_word}    |
   {16,25,23} |                       | {foobar,new_word} | {{elt2}}
! (3 rows)

--- 106,113 ----
            d[1:1][2:2]
     FROM arrtest;
       a      | b |         c         |    d
! ------------+---+-------------------+----------
              |   | {foo,new_word}    |
   {16,25,23} |   | {foobar,new_word} | {{elt2}}
! (2 rows)


======================================================================

*** ./expected/copy2.out    Wed Aug 21 22:25:28 2002
--- ./results/copy2.out    Tue Aug 27 14:15:50 2002
***************
*** 34,40 ****
  ERROR:  Attribute "d" specified more than once
  -- missing data: should fail
  COPY x from stdin;
! ERROR:  copy: line 1, Missing data for column "b"
  lost synchronization with server, resetting connection
  COPY x from stdin;
  ERROR:  copy: line 1, Missing data for column "e"
--- 34,40 ----
  ERROR:  Attribute "d" specified more than once
  -- missing data: should fail
  COPY x from stdin;
! ERROR:  copy: line 1, pg_atoi: zero-length string!
  lost synchronization with server, resetting connection
  COPY x from stdin;
  ERROR:  copy: line 1, Missing data for column "e"

======================================================================


pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [BUGS] Bug #718: request for improvement of /? to show /d+ /l+
Next
From: Bruce Momjian
Date:
Subject: Re: Open 7.3 items