pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: move hex_decode() to /common so it can be called from frontend
7+ messages / 3 participants
[nested] [flat]

* pgsql: move hex_decode() to /common so it can be called from frontend
@ 2020-12-24 22:26 Bruce Momjian <bruce@momjian.us>
  2020-12-24 23:14 ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Bruce Momjian @ 2020-12-24 22:26 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

move hex_decode() to /common so it can be called from frontend

This allows removal of a copy of hex_decode() from ecpg, and will be
used by the soon-to-be added pg_alterckey command.

Backpatch-through: master

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/c3826f831e6e63e13a749fd3ab9fd7106707b549

Modified Files
--------------
src/backend/utils/adt/encode.c     |  64 +---------------------
src/backend/utils/adt/varlena.c    |   1 +
src/common/Makefile                |   1 +
src/common/hex_decode.c            | 106 +++++++++++++++++++++++++++++++++++++
src/include/common/hex_decode.h    |  16 ++++++
src/include/utils/builtins.h       |   1 -
src/interfaces/ecpg/ecpglib/data.c |  52 +-----------------
src/tools/msvc/Mkvcbuild.pm        |   2 +-
8 files changed, 127 insertions(+), 116 deletions(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: move hex_decode() to /common so it can be called from frontend
  2020-12-24 22:26 pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
@ 2020-12-24 23:14 ` Tom Lane <tgl@sss.pgh.pa.us>
  2020-12-24 23:16   ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2020-12-24 23:14 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; +Cc: pgsql-committers@lists.postgresql.org

Bruce Momjian <bruce@momjian.us> writes:
> move hex_decode() to /common so it can be called from frontend

The buildfarm seems pretty unimpressed with this.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: move hex_decode() to /common so it can be called from frontend
  2020-12-24 22:26 pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  2020-12-24 23:14 ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-12-24 23:16   ` Bruce Momjian <bruce@momjian.us>
  2020-12-25 00:04     ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 7+ messages in thread

From: Bruce Momjian @ 2020-12-24 23:16 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-committers@lists.postgresql.org

On Thu, Dec 24, 2020 at 06:14:49PM -0500, Tom Lane wrote:
> Bruce Momjian <bruce@momjian.us> writes:
> > move hex_decode() to /common so it can be called from frontend
> 
> The buildfarm seems pretty unimpressed with this.

Yes, we are working on reverting the ecpg part.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee






^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: move hex_decode() to /common so it can be called from frontend
  2020-12-24 22:26 pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  2020-12-24 23:14 ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Tom Lane <tgl@sss.pgh.pa.us>
  2020-12-24 23:16   ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
@ 2020-12-25 00:04     ` Michael Paquier <michael@paquier.xyz>
  2020-12-25 01:47       ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Paquier @ 2020-12-25 00:04 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-committers@lists.postgresql.org

On Thu, Dec 24, 2020 at 06:16:20PM -0500, Bruce Momjian wrote:
> On Thu, Dec 24, 2020 at 06:14:49PM -0500, Tom Lane wrote:
>> Bruce Momjian <bruce@momjian.us> writes:
>> > move hex_decode() to /common so it can be called from frontend
>> 
>> The buildfarm seems pretty unimpressed with this.
> 
> Yes, we are working on reverting the ecpg part.

Looks like the defense put in place by 6b1c5ca has allowed to catch up
a bug here.  When base64 has been copied from encode.c to src/common/
for SCRAM (newlines should not be handled by SCRAM, hence the copy),
we have done the same.  The copied code just returns -1 for error
paths.  For this case, I think that you should also prefix those
functions with "pg_", and also include the encode part for
completeness.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: move hex_decode() to /common so it can be called from frontend
  2020-12-24 22:26 pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  2020-12-24 23:14 ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Tom Lane <tgl@sss.pgh.pa.us>
  2020-12-24 23:16   ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  2020-12-25 00:04     ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Michael Paquier <michael@paquier.xyz>
@ 2020-12-25 01:47       ` Bruce Momjian <bruce@momjian.us>
  2020-12-25 02:20         ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Bruce Momjian @ 2020-12-25 01:47 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-committers@lists.postgresql.org

On Fri, Dec 25, 2020 at 09:04:41AM +0900, Michael Paquier wrote:
> Looks like the defense put in place by 6b1c5ca has allowed to catch up
> a bug here.  When base64 has been copied from encode.c to src/common/
> for SCRAM (newlines should not be handled by SCRAM, hence the copy),
> we have done the same.  The copied code just returns -1 for error
> paths.  For this case, I think that you should also prefix those
> functions with "pg_", and also include the encode part for
> completeness.

I now understand the wisdom of your suggestion.  Attached is a patch
that removes hex_decode from ecpg properly, and returns -1 from the
/common version.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee

Attachments:

hex.diff (text/x-diff, 5.1K) download diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c new file mode 100644 index a6c65b1..4604e9b *** a/src/backend/utils/adt/encode.c --- b/src/backend/utils/adt/encode.c *************** static const struct *** 504,510 **** { "hex", { ! hex_enc_len, hex_dec_len, hex_encode, hex_decode } }, { --- 504,510 ---- { "hex", { ! hex_enc_len, hex_dec_len, hex_encode, pg_hex_decode } }, { diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c new file mode 100644 index 9300d19..60b02aa *** a/src/backend/utils/adt/varlena.c --- b/src/backend/utils/adt/varlena.c *************** byteain(PG_FUNCTION_ARGS) *** 307,313 **** bc = (len - 2) / 2 + VARHDRSZ; /* maximum possible length */ result = palloc(bc); ! bc = hex_decode(inputText + 2, len - 2, VARDATA(result)); SET_VARSIZE(result, bc + VARHDRSZ); /* actual length */ PG_RETURN_BYTEA_P(result); --- 307,313 ---- bc = (len - 2) / 2 + VARHDRSZ; /* maximum possible length */ result = palloc(bc); ! bc = pg_hex_decode(inputText + 2, len - 2, VARDATA(result)); SET_VARSIZE(result, bc + VARHDRSZ); /* actual length */ PG_RETURN_BYTEA_P(result); diff --git a/src/common/hex_decode.c b/src/common/hex_decode.c new file mode 100644 index 3ecdc73..ce17812 *** a/src/common/hex_decode.c --- b/src/common/hex_decode.c *************** get_hex(const char *cp) *** 66,72 **** } uint64 ! hex_decode(const char *src, size_t len, char *dst) { const char *s, *srcend; --- 66,72 ---- } uint64 ! pg_hex_decode(const char *src, size_t len, char *dst) { const char *s, *srcend; *************** hex_decode(const char *src, size_t len, *** 89,96 **** if (s >= srcend) { #ifdef FRONTEND ! pg_log_fatal("invalid hexadecimal data: odd number of digits"); ! exit(EXIT_FAILURE); #else ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), --- 89,95 ---- if (s >= srcend) { #ifdef FRONTEND ! return -1; #else ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), diff --git a/src/include/common/hex_decode.h b/src/include/common/hex_decode.h new file mode 100644 index 1f99f06..ec210b6 *** a/src/include/common/hex_decode.h --- b/src/include/common/hex_decode.h *************** *** 10,16 **** #ifndef COMMON_HEX_DECODE_H #define COMMON_HEX_DECODE_H ! extern uint64 hex_decode(const char *src, size_t len, char *dst); #endif /* COMMON_HEX_DECODE_H */ --- 10,16 ---- #ifndef COMMON_HEX_DECODE_H #define COMMON_HEX_DECODE_H ! extern uint64 pg_hex_decode(const char *src, size_t len, char *dst); #endif /* COMMON_HEX_DECODE_H */ diff --git a/src/interfaces/ecpg/ecpglib/data.c b/src/interfaces/ecpg/ecpglib/data.c new file mode 100644 index 6bc91ef..0bb43f1 *** a/src/interfaces/ecpg/ecpglib/data.c --- b/src/interfaces/ecpg/ecpglib/data.c *************** *** 5,10 **** --- 5,11 ---- #include <math.h> + #include "common/hex_decode.h" #include "ecpgerrno.h" #include "ecpglib.h" #include "ecpglib_extern.h" *************** ecpg_hex_dec_len(unsigned srclen) *** 136,192 **** return srclen >> 1; } - static inline char - get_hex(char c) - { - static const int8 hexlookup[128] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, - -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - }; - int res = -1; - - if (c > 0 && c < 127) - res = hexlookup[(unsigned char) c]; - - return (char) res; - } - - static unsigned - hex_decode(const char *src, unsigned len, char *dst) - { - const char *s, - *srcend; - char v1, - v2, - *p; - - srcend = src + len; - s = src; - p = dst; - while (s < srcend) - { - if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r') - { - s++; - continue; - } - v1 = get_hex(*s++) << 4; - if (s >= srcend) - return -1; - - v2 = get_hex(*s++); - *p++ = v1 | v2; - } - - return p - dst; - } - unsigned ecpg_hex_encode(const char *src, unsigned len, char *dst) { --- 137,142 ---- *************** ecpg_get_data(const PGresult *results, i *** 532,538 **** dst_size = ecpg_hex_enc_len(varcharsize); src_size = size - 2; /* exclude backslash + 'x' */ dec_size = src_size < dst_size ? src_size : dst_size; ! variable->len = hex_decode(pval + 2, dec_size, variable->arr); if (dst_size < src_size) { --- 482,488 ---- dst_size = ecpg_hex_enc_len(varcharsize); src_size = size - 2; /* exclude backslash + 'x' */ dec_size = src_size < dst_size ? src_size : dst_size; ! variable->len = pg_hex_decode(pval + 2, dec_size, variable->arr); if (dst_size < src_size) {
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: pgsql: move hex_decode() to /common so it can be called from frontend
  2020-12-24 22:26 pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  2020-12-24 23:14 ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Tom Lane <tgl@sss.pgh.pa.us>
  2020-12-24 23:16   ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  2020-12-25 00:04     ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Michael Paquier <michael@paquier.xyz>
  2020-12-25 01:47       ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
@ 2020-12-25 02:20         ` Tom Lane <tgl@sss.pgh.pa.us>
  2020-12-25 02:22           ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2020-12-25 02:20 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; +Cc: Michael Paquier <michael@paquier.xyz>; pgsql-committers@lists.postgresql.org

Bruce Momjian <bruce@momjian.us> writes:
> I now understand the wisdom of your suggestion.  Attached is a patch
> that removes hex_decode from ecpg properly, and returns -1 from the
> /common version.

I'm fairly unimpressed with this.  I don't like having fundamentally
different (and 100% undocumented) behaviors between the frontend and
backend versions of the "same" function.

I think you should leave ecpglib alone.  Unifying that little bit
of code isn't worth having to contort the API of the common version.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: move hex_decode() to /common so it can be called from frontend
  2020-12-24 22:26 pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  2020-12-24 23:14 ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Tom Lane <tgl@sss.pgh.pa.us>
  2020-12-24 23:16   ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  2020-12-25 00:04     ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Michael Paquier <michael@paquier.xyz>
  2020-12-25 01:47       ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
  2020-12-25 02:20         ` Re: pgsql: move hex_decode() to /common so it can be called from frontend Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-12-25 02:22           ` Bruce Momjian <bruce@momjian.us>
  0 siblings, 0 replies; 7+ messages in thread

From: Bruce Momjian @ 2020-12-25 02:22 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael@paquier.xyz>; pgsql-committers@lists.postgresql.org

On Thu, Dec 24, 2020 at 09:20:27PM -0500, Tom Lane wrote:
> Bruce Momjian <bruce@momjian.us> writes:
> > I now understand the wisdom of your suggestion.  Attached is a patch
> > that removes hex_decode from ecpg properly, and returns -1 from the
> > /common version.
> 
> I'm fairly unimpressed with this.  I don't like having fundamentally
> different (and 100% undocumented) behaviors between the frontend and
> backend versions of the "same" function.
> 
> I think you should leave ecpglib alone.  Unifying that little bit
> of code isn't worth having to contort the API of the common version.

OK, sure, I can see that point too.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee






^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2020-12-25 02:22 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-12-24 22:26 pgsql: move hex_decode() to /common so it can be called from frontend Bruce Momjian <bruce@momjian.us>
2020-12-24 23:14 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-12-24 23:16   ` Bruce Momjian <bruce@momjian.us>
2020-12-25 00:04     ` Michael Paquier <michael@paquier.xyz>
2020-12-25 01:47       ` Bruce Momjian <bruce@momjian.us>
2020-12-25 02:20         ` Tom Lane <tgl@sss.pgh.pa.us>
2020-12-25 02:22           ` Bruce Momjian <bruce@momjian.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox