Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evRW0-00006F-93 for pgsql-bugs@arkaria.postgresql.org; Mon, 12 Mar 2018 17:45:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evRVz-00024p-2C for pgsql-bugs@arkaria.postgresql.org; Mon, 12 Mar 2018 17:45:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evRVy-00024g-Qy for pgsql-bugs@lists.postgresql.org; Mon, 12 Mar 2018 17:45:38 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evRVq-0004I7-L4 for pgsql-bugs@lists.postgresql.org; Mon, 12 Mar 2018 17:45:38 +0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 3EF47210D6; Mon, 12 Mar 2018 13:45:28 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute7.internal (MEProxy); Mon, 12 Mar 2018 13:45:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=KlGU0gAyAZpzam8GpsPl5I+ODwj2u A9ViAY//+ZhQyE=; b=jN7uFw9NYxK4rzxgyVaKC/8a4z+XcCoE9EgBfzU4dI32p 1HaTjC2lHzw0cJfZ7WkVg2S6nPd4l7imUNvd0KWHjUHSBfL6cAhBZS5KcA+/f4rP 6LjOAJFtey3MAsUbsOgl7DogZ2Sfw7XeWWK/t0Y21lRbEEeLghajl2Ty4U1002lu iJA6xFAxmUFc4FUWzjErJsmxqSxbuTElQA9w8SBSmI3B77oW9x5FhmeUH37GwXOH 4bNFfeDf72ApDPAMG8VNAWwJHVVnoBbV+3rgplZ6bYrbWJC3YnsBBEZI/sWN+oX5 XSiNJHnJ2fUJ2Szl+FQg57en5YTBOV7Jz097bRSrQ== X-ME-Sender: Received: from april.local (c-73-13-66-39.hsd1.pa.comcast.net [73.13.66.39]) by mail.messagingengine.com (Postfix) with ESMTPA id B984E7E0D6; Mon, 12 Mar 2018 13:45:27 -0400 (EDT) Subject: Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column To: Michael Paquier References: <152009601619.21242.262498029671910378@wrigleys.postgresql.org> <31194.1520098424@sss.pgh.pa.us> <87po4lkkaj.fsf@news-spur.riddles.org.uk> <20180304235654.GA32165@paquier.xyz> <87lgf7l6sj.fsf@news-spur.riddles.org.uk> <21016.1520228672@sss.pgh.pa.us> <20180305061325.GF2266@paquier.xyz> <26456.1520278713@sss.pgh.pa.us> <20180308080858.GE1788@paquier.xyz> Cc: Tom Lane , Andrew Gierth , evgeni-klimov@yandex.ru, pgsql-bugs@lists.postgresql.org From: Peter Eisentraut Organization: 2ndQuadrant Message-ID: Date: Mon, 12 Mar 2018 13:45:26 -0400 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <20180308080858.GE1788@paquier.xyz> Content-Type: multipart/mixed; boundary="------------BF42A5BFDAF2897E44C428C7" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------BF42A5BFDAF2897E44C428C7 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit On 3/8/18 03:08, Michael Paquier wrote: > Here are some comments for patch 2. > > + if (endptr != token + length || errno == ERANGE || > + /* check for overflow of int4 */ > + val != (long) ((int32) val)) > return T_Float; > It would be nice to have this check be consistent with the new > definition of ival and int32, One suggestion is to use directly int32 or > just have a static assertion that sizeof(int) == sizeof(int32)? Or > that's too much nannyism? Fixed in the attached next version. > By the way, why do you remove HAVE_LONG_INT_64? On platforms where long > is 4 bytes this would still be a no-op. Right, but the compiler can optimize it away then. No need to have #ifdefs. > The comment block on top of the definition of Value in value.h still > mentions "long", while it should mention "int" per your patch. done > If you care about those, you > could also remove the ones in interval.c and datetime.c... Actually, we could just use the strtoint() defined there and apply it everywhere, so avoid repeating these patterns. Done so in an additional patch. -- Peter Eisentraut http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --------------BF42A5BFDAF2897E44C428C7 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="v2-0001-Fix-CREATE-TABLE-LIKE-with-bigint-identity-column.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="v2-0001-Fix-CREATE-TABLE-LIKE-with-bigint-identity-column.pa"; filename*1="tch" =46rom 23a0bbc152529713ba45e44eaaa177b19b4ca814 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 7 Mar 2018 14:38:35 -0500 Subject: [PATCH v2 1/3] Fix CREATE TABLE / LIKE with bigint identity colu= mn CREATE TABLE / LIKE with a bigint identity column would fail on platforms where long is 32 bits. Copying the sequence values used makeInteger(), which would truncate the 64-bit sequence data to 32 bits. To fix, use makeFloat() instead, like the parser. (This does not actually make use of floats, but stores the values as strings.) Bug: #15096 --- src/backend/commands/sequence.c | 19 +++++++++++------ src/test/regress/expected/create_table_like.out | 28 ++++++++++++-------= ------ src/test/regress/sql/create_table_like.sql | 2 +- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/backend/commands/sequence.c b/src/backend/commands/seque= nce.c index ef3ca8c00b..dcc0aa536a 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -1752,12 +1752,19 @@ sequence_options(Oid relid) elog(ERROR, "cache lookup failed for sequence %u", relid); pgsform =3D (Form_pg_sequence) GETSTRUCT(pgstuple); =20 - options =3D lappend(options, makeDefElem("cache", (Node *) makeInteger(= pgsform->seqcache), -1)); - options =3D lappend(options, makeDefElem("cycle", (Node *) makeInteger(= pgsform->seqcycle), -1)); - options =3D lappend(options, makeDefElem("increment", (Node *) makeInte= ger(pgsform->seqincrement), -1)); - options =3D lappend(options, makeDefElem("maxvalue", (Node *) makeInteg= er(pgsform->seqmax), -1)); - options =3D lappend(options, makeDefElem("minvalue", (Node *) makeInteg= er(pgsform->seqmin), -1)); - options =3D lappend(options, makeDefElem("start", (Node *) makeInteger(= pgsform->seqstart), -1)); + /* Use makeFloat() for 64-bit integers, like gram.y does. */ + options =3D lappend(options, + makeDefElem("cache", (Node *) makeFloat(psprintf(INT64_FORMAT, pg= sform->seqcache)), -1)); + options =3D lappend(options, + makeDefElem("cycle", (Node *) makeInteger(pgsform->seqcycle), -1)= ); + options =3D lappend(options, + makeDefElem("increment", (Node *) makeFloat(psprintf(INT64_FORMAT= , pgsform->seqincrement)), -1)); + options =3D lappend(options, + makeDefElem("maxvalue", (Node *) makeFloat(psprintf(INT64_FORMAT,= pgsform->seqmax)), -1)); + options =3D lappend(options, + makeDefElem("minvalue", (Node *) makeFloat(psprintf(INT64_FORMAT,= pgsform->seqmin)), -1)); + options =3D lappend(options, + makeDefElem("start", (Node *) makeFloat(psprintf(INT64_FORMAT, pg= sform->seqstart)), -1)); =20 ReleaseSysCache(pgstuple); =20 diff --git a/src/test/regress/expected/create_table_like.out b/src/test/r= egress/expected/create_table_like.out index 52ff18c8ca..8d4543bfe8 100644 --- a/src/test/regress/expected/create_table_like.out +++ b/src/test/regress/expected/create_table_like.out @@ -66,13 +66,13 @@ SELECT * FROM inhg; /* Two records with three columns= in order x=3Dx, xx=3Dtext, y=3Dy (2 rows) =20 DROP TABLE inhg; -CREATE TABLE test_like_id_1 (a int GENERATED ALWAYS AS IDENTITY, b text)= ; +CREATE TABLE test_like_id_1 (a bigint GENERATED ALWAYS AS IDENTITY, b te= xt); \d test_like_id_1 Table "public.test_like_id_1" - Column | Type | Collation | Nullable | Default = ---------+---------+-----------+----------+------------------------------= - a | integer | | not null | generated always as identity - b | text | | |=20 + Column | Type | Collation | Nullable | Default =20 +--------+--------+-----------+----------+------------------------------ + a | bigint | | not null | generated always as identity + b | text | | |=20 =20 INSERT INTO test_like_id_1 (b) VALUES ('b1'); SELECT * FROM test_like_id_1; @@ -83,11 +83,11 @@ SELECT * FROM test_like_id_1; =20 CREATE TABLE test_like_id_2 (LIKE test_like_id_1); \d test_like_id_2 - Table "public.test_like_id_2" - Column | Type | Collation | Nullable | Default=20 ---------+---------+-----------+----------+--------- - a | integer | | not null |=20 - b | text | | |=20 + Table "public.test_like_id_2" + Column | Type | Collation | Nullable | Default=20 +--------+--------+-----------+----------+--------- + a | bigint | | not null |=20 + b | text | | |=20 =20 INSERT INTO test_like_id_2 (b) VALUES ('b2'); ERROR: null value in column "a" violates not-null constraint @@ -100,10 +100,10 @@ SELECT * FROM test_like_id_2; -- identity was not = copied CREATE TABLE test_like_id_3 (LIKE test_like_id_1 INCLUDING IDENTITY); \d test_like_id_3 Table "public.test_like_id_3" - Column | Type | Collation | Nullable | Default = ---------+---------+-----------+----------+------------------------------= - a | integer | | not null | generated always as identity - b | text | | |=20 + Column | Type | Collation | Nullable | Default =20 +--------+--------+-----------+----------+------------------------------ + a | bigint | | not null | generated always as identity + b | text | | |=20 =20 INSERT INTO test_like_id_3 (b) VALUES ('b3'); SELECT * FROM test_like_id_3; -- identity was copied and applied diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regres= s/sql/create_table_like.sql index 32d861621b..42cad6826b 100644 --- a/src/test/regress/sql/create_table_like.sql +++ b/src/test/regress/sql/create_table_like.sql @@ -37,7 +37,7 @@ CREATE TABLE inhg (x text, LIKE inhx INCLUDING CONSTRAI= NTS, y text); /* Copies c SELECT * FROM inhg; /* Two records with three columns in order x=3Dx, xx= =3Dtext, y=3Dy */ DROP TABLE inhg; =20 -CREATE TABLE test_like_id_1 (a int GENERATED ALWAYS AS IDENTITY, b text)= ; +CREATE TABLE test_like_id_1 (a bigint GENERATED ALWAYS AS IDENTITY, b te= xt); \d test_like_id_1 INSERT INTO test_like_id_1 (b) VALUES ('b1'); SELECT * FROM test_like_id_1; base-commit: 4a4e2442a7f7c1434e86dd290cdb3704cfebb24c --=20 2.16.2 --------------BF42A5BFDAF2897E44C428C7 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="v2-0002-Change-internal-integer-representation-of-Value-n.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="v2-0002-Change-internal-integer-representation-of-Value-n.pa"; filename*1="tch" =46rom 7cd34d15240ca7581826b795b599bd507ca43c4b Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 12 Mar 2018 12:17:58 -0400 Subject: [PATCH v2 2/3] Change internal integer representation of Value n= ode A Value node would store an integer as a long. This causes needless portability risks, as long can be of varying sizes. Change it to use int instead. All code using this was already careful to only store 32-bit values anyway. --- src/backend/nodes/outfuncs.c | 2 +- src/backend/nodes/read.c | 14 +++++--------- src/backend/nodes/value.c | 2 +- src/backend/parser/scan.l | 9 +++------ src/backend/utils/misc/guc.c | 2 +- src/include/nodes/value.h | 6 +++--- src/interfaces/ecpg/preproc/pgc.l | 9 +++------ 7 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 1785ea3918..fd80891954 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -3235,7 +3235,7 @@ _outValue(StringInfo str, const Value *value) switch (value->type) { case T_Integer: - appendStringInfo(str, "%ld", value->val.ival); + appendStringInfo(str, "%d", value->val.ival); break; case T_Float: =20 diff --git a/src/backend/nodes/read.c b/src/backend/nodes/read.c index 76414029d8..6e9fa45e37 100644 --- a/src/backend/nodes/read.c +++ b/src/backend/nodes/read.c @@ -224,13 +224,9 @@ nodeTokenType(char *token, int length) =20 errno =3D 0; val =3D strtol(token, &endptr, 10); - (void) val; /* avoid compiler warning if unused */ - if (endptr !=3D token + length || errno =3D=3D ERANGE -#ifdef HAVE_LONG_INT_64 - /* if long > 32 bits, check for overflow of int4 */ - || val !=3D (long) ((int32) val) -#endif - ) + if (endptr !=3D token + length || errno =3D=3D ERANGE || + /* check for overflow of int */ + val !=3D (int) val) return T_Float; return T_Integer; } @@ -387,9 +383,9 @@ nodeRead(char *token, int tok_len) case T_Integer: =20 /* - * we know that the token terminates on a char atol will stop at + * we know that the token terminates on a char atoi will stop at */ - result =3D (Node *) makeInteger(atol(token)); + result =3D (Node *) makeInteger(atoi(token)); break; case T_Float: { diff --git a/src/backend/nodes/value.c b/src/backend/nodes/value.c index 8f0428fce1..2a30307baf 100644 --- a/src/backend/nodes/value.c +++ b/src/backend/nodes/value.c @@ -20,7 +20,7 @@ * makeInteger */ Value * -makeInteger(long i) +makeInteger(int i) { Value *v =3D makeNode(Value); =20 diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l index eedef7c005..97d4dee628 100644 --- a/src/backend/parser/scan.l +++ b/src/backend/parser/scan.l @@ -1216,12 +1216,9 @@ process_integer_literal(const char *token, YYSTYPE= *lval) =20 errno =3D 0; val =3D strtol(token, &endptr, 10); - if (*endptr !=3D '\0' || errno =3D=3D ERANGE -#ifdef HAVE_LONG_INT_64 - /* if long > 32 bits, check for overflow of int4 */ - || val !=3D (long) ((int32) val) -#endif - ) + if (*endptr !=3D '\0' || errno =3D=3D ERANGE || + /* check for overflow of int */ + val !=3D (int) val) { /* integer too large, treat it as a float */ lval->str =3D pstrdup(token); diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index a4f9b3668e..fc3e10c750 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -6913,7 +6913,7 @@ flatten_set_variable_args(const char *name, List *a= rgs) switch (nodeTag(&con->val)) { case T_Integer: - appendStringInfo(&buf, "%ld", intVal(&con->val)); + appendStringInfo(&buf, "%d", intVal(&con->val)); break; case T_Float: /* represented as a string, so just copy it */ diff --git a/src/include/nodes/value.h b/src/include/nodes/value.h index 94d09e176e..1665714515 100644 --- a/src/include/nodes/value.h +++ b/src/include/nodes/value.h @@ -34,7 +34,7 @@ * better to use the more general representation.) * * Note that an integer-looking string will get lexed as T_Float if - * the value is too large to fit in a 'long'. + * the value is too large to fit in an 'int'. * * Nulls, of course, don't need the value part at all. *---------------------- @@ -44,7 +44,7 @@ typedef struct Value NodeTag type; /* tag appropriately (eg. T_String) */ union ValUnion { - long ival; /* machine integer */ + int ival; /* machine integer */ char *str; /* string */ } val; } Value; @@ -53,7 +53,7 @@ typedef struct Value #define floatVal(v) atof(((Value *)(v))->val.str) #define strVal(v) (((Value *)(v))->val.str) =20 -extern Value *makeInteger(long i); +extern Value *makeInteger(int i); extern Value *makeFloat(char *numericStr); extern Value *makeString(char *str); extern Value *makeBitString(char *str); diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/prep= roc/pgc.l index e99b7ff586..ba1798c77e 100644 --- a/src/interfaces/ecpg/preproc/pgc.l +++ b/src/interfaces/ecpg/preproc/pgc.l @@ -732,12 +732,9 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifnd= ef}|{import})((\/\*[^*/]*\*+ =20 errno =3D 0; val =3D strtol((char *)yytext, &endptr,10); - if (*endptr !=3D '\0' || errno =3D=3D ERANGE -#ifdef HAVE_LONG_INT_64 - /* if long > 32 bits, check for overflow of int4 */ - || val !=3D (long) ((int32) val) -#endif - ) + if (*endptr !=3D '\0' || errno =3D=3D ERANGE || + /* check for overflow of int */ + val !=3D (int) val) { errno =3D 0; base_yylval.str =3D mm_strdup(yytext); --=20 2.16.2 --------------BF42A5BFDAF2897E44C428C7 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="v2-0003-Move-strtoint-to-common.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="v2-0003-Move-strtoint-to-common.patch" =46rom aeea50127725058daeef294bf5b10f304e1d14bb Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 12 Mar 2018 13:44:15 -0400 Subject: [PATCH v2 3/3] Move strtoint() to common Several places uses similar code to convert a string to an int, so take the function that we already had and make it globally available. --- src/backend/nodes/read.c | 11 +++++------ src/backend/parser/scan.l | 9 ++++----- src/backend/utils/adt/datetime.c | 18 +----------------- src/common/string.c | 15 +++++++++++++++ src/include/common/string.h | 1 + src/interfaces/ecpg/pgtypeslib/Makefile | 6 +++++- src/interfaces/ecpg/pgtypeslib/interval.c | 16 ++-------------- src/interfaces/ecpg/preproc/pgc.l | 10 +++++----- 8 files changed, 38 insertions(+), 48 deletions(-) diff --git a/src/backend/nodes/read.c b/src/backend/nodes/read.c index 6e9fa45e37..5437fa3d0a 100644 --- a/src/backend/nodes/read.c +++ b/src/backend/nodes/read.c @@ -21,6 +21,7 @@ =20 #include =20 +#include "common/string.h" #include "nodes/pg_list.h" #include "nodes/readfuncs.h" #include "nodes/value.h" @@ -216,17 +217,15 @@ nodeTokenType(char *token, int length) /* * Yes. Figure out whether it is integral or float; this requires * both a syntax check and a range check. strtol() can do both for us.= - * We know the token will end at a character that strtol will stop at,= + * We know the token will end at a character that strtoint will stop a= t, * so we do not need to modify the string. */ - long val; + int val; char *endptr; =20 errno =3D 0; - val =3D strtol(token, &endptr, 10); - if (endptr !=3D token + length || errno =3D=3D ERANGE || - /* check for overflow of int */ - val !=3D (int) val) + val =3D strtoint(token, &endptr, 10); + if (endptr !=3D token + length || errno =3D=3D ERANGE) return T_Float; return T_Integer; } diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l index 97d4dee628..0cd782827a 100644 --- a/src/backend/parser/scan.l +++ b/src/backend/parser/scan.l @@ -34,6 +34,7 @@ #include #include =20 +#include "common/string.h" #include "parser/gramparse.h" #include "parser/parser.h" /* only needed for GUC variables */ #include "parser/scansup.h" @@ -1211,14 +1212,12 @@ litbufdup(core_yyscan_t yyscanner) static int process_integer_literal(const char *token, YYSTYPE *lval) { - long val; + int val; char *endptr; =20 errno =3D 0; - val =3D strtol(token, &endptr, 10); - if (*endptr !=3D '\0' || errno =3D=3D ERANGE || - /* check for overflow of int */ - val !=3D (int) val) + val =3D strtoint(token, &endptr, 10); + if (*endptr !=3D '\0' || errno =3D=3D ERANGE) { /* integer too large, treat it as a float */ lval->str =3D pstrdup(token); diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/dat= etime.c index 8375b93c39..3f0f65c295 100644 --- a/src/backend/utils/adt/datetime.c +++ b/src/backend/utils/adt/datetime.c @@ -22,6 +22,7 @@ #include "access/htup_details.h" #include "access/xact.h" #include "catalog/pg_type.h" +#include "common/string.h" #include "funcapi.h" #include "miscadmin.h" #include "nodes/nodeFuncs.h" @@ -251,23 +252,6 @@ static const datetkn *deltacache[MAXDATEFIELDS] =3D = {NULL}; static const datetkn *abbrevcache[MAXDATEFIELDS] =3D {NULL}; =20 =20 -/* - * strtoint --- just like strtol, but returns int not long - */ -static int -strtoint(const char *nptr, char **endptr, int base) -{ - long val; - - val =3D strtol(nptr, endptr, base); -#ifdef HAVE_LONG_INT_64 - if (val !=3D (long) ((int32) val)) - errno =3D ERANGE; -#endif - return (int) val; -} - - /* * Calendar time to Julian date conversions. * Julian date is commonly used in astronomical applications, diff --git a/src/common/string.c b/src/common/string.c index 0e3557076a..a45e9b8858 100644 --- a/src/common/string.c +++ b/src/common/string.c @@ -41,3 +41,18 @@ pg_str_endswith(const char *str, const char *end) str +=3D slen - elen; return strcmp(str, end) =3D=3D 0; } + + +/* + * strtoint --- just like strtol, but returns int not long + */ +int +strtoint(const char *restrict str, char **restrict endptr, int base) +{ + long val; + + val =3D strtol(str, endptr, base); + if (val !=3D (int) val) + errno =3D ERANGE; + return (int) val; +} diff --git a/src/include/common/string.h b/src/include/common/string.h index 06934b9914..23e2e01f0e 100644 --- a/src/include/common/string.h +++ b/src/include/common/string.h @@ -11,5 +11,6 @@ #define COMMON_STRING_H =20 extern bool pg_str_endswith(const char *str, const char *end); +extern int strtoint(const char *restrict str, char **restrict endptr, in= t base); =20 #endif /* COMMON_STRING_H */ diff --git a/src/interfaces/ecpg/pgtypeslib/Makefile b/src/interfaces/ecp= g/pgtypeslib/Makefile index 01206568f1..0945f3d5bc 100644 --- a/src/interfaces/ecpg/pgtypeslib/Makefile +++ b/src/interfaces/ecpg/pgtypeslib/Makefile @@ -32,6 +32,7 @@ SHLIB_EXPORTS =3D exports.txt OBJS=3D numeric.o datetime.o common.o dt_common.o timestamp.o interval.o= \ pgstrcasecmp.o \ $(filter rint.o snprintf.o strnlen.o, $(LIBOBJS)) \ + string.o \ $(WIN32RES) =20 all: all-lib @@ -47,6 +48,9 @@ include $(top_srcdir)/src/Makefile.shlib pgstrcasecmp.c rint.c snprintf.c strnlen.c: % : $(top_srcdir)/src/port/%= rm -f $@ && $(LN_S) $< . =20 +string.c: % : $(top_srcdir)/src/common/% + rm -f $@ && $(LN_S) $< . + install: all installdirs install-lib =20 installdirs: installdirs-lib @@ -54,6 +58,6 @@ installdirs: installdirs-lib uninstall: uninstall-lib =20 clean distclean: clean-lib - rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strnlen.c + rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strnlen.c string.c =20 maintainer-clean: distclean maintainer-clean-lib diff --git a/src/interfaces/ecpg/pgtypeslib/interval.c b/src/interfaces/e= cpg/pgtypeslib/interval.c index 24a2f36d4d..6146a3c7b6 100644 --- a/src/interfaces/ecpg/pgtypeslib/interval.c +++ b/src/interfaces/ecpg/pgtypeslib/interval.c @@ -9,25 +9,13 @@ #error -ffast-math is known to break this code #endif =20 +#include "common/string.h" + #include "extern.h" #include "dt.h" #include "pgtypes_error.h" #include "pgtypes_interval.h" =20 -/* copy&pasted from .../src/backend/utils/adt/datetime.c */ -static int -strtoint(const char *nptr, char **endptr, int base) -{ - long val; - - val =3D strtol(nptr, endptr, base); -#ifdef HAVE_LONG_INT_64 - if (val !=3D (long) ((int32) val)) - errno =3D ERANGE; -#endif - return (int) val; -} - /* copy&pasted from .../src/backend/utils/adt/datetime.c * and changesd struct pg_tm to struct tm */ diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/prep= roc/pgc.l index ba1798c77e..405dee73b0 100644 --- a/src/interfaces/ecpg/preproc/pgc.l +++ b/src/interfaces/ecpg/preproc/pgc.l @@ -21,6 +21,8 @@ #include #include =20 +#include "common/string.h" + #include "extern.h" #include "preproc.h" } @@ -727,14 +729,12 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifn= def}|{import})((\/\*[^*/]*\*+ return PARAM; } {integer} { - long val; + int val; char* endptr; =20 errno =3D 0; - val =3D strtol((char *)yytext, &endptr,10); - if (*endptr !=3D '\0' || errno =3D=3D ERANGE || - /* check for overflow of int */ - val !=3D (int) val) + val =3D strtoint(yytext, &endptr, 10); + if (*endptr !=3D '\0' || errno =3D=3D ERANGE) { errno =3D 0; base_yylval.str =3D mm_strdup(yytext); --=20 2.16.2 --------------BF42A5BFDAF2897E44C428C7--