0002-Add-compatibilty-wrapper-functions-for-base64-encodi.patch 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. From b020863a3ea18488448bc09234a4e3b26b68058d Mon Sep 17 00:00:00 2001
  2. From: Sven Klemm <sven@timescale.com>
  3. Date: Sat, 19 Sep 2020 19:17:38 +0200
  4. Subject: [PATCH] Add compatibilty wrapper functions for base64
  5. encoding/decoding
  6. PG13 adds a destination length 4th argument to pg_b64_decode and
  7. pg_b64_encode functions so this patch adds a macro that translates
  8. to the 3 argument and 4 argument calls depending on postgres version.
  9. This patch also adds checking of return values for those functions.
  10. https://github.com/postgres/postgres/commit/cfc40d384a
  11. Signed-off-by: Maxim Kochetkov <fido_max@inbox.ru>
  12. Fetch from: https://github.com/timescale/timescaledb/commit/002510cb01e1d09767a526560f89c1857c1738a2.patch
  13. ---
  14. src/compat.h | 11 +++++++++++
  15. tsl/src/compression/compression.c | 12 ++++++++++--
  16. 2 files changed, 21 insertions(+), 2 deletions(-)
  17. diff --git a/src/compat.h b/src/compat.h
  18. index 267bb09a..d84f8754 100644
  19. --- a/src/compat.h
  20. +++ b/src/compat.h
  21. @@ -347,4 +347,15 @@ get_vacuum_options(const VacuumStmt *stmt)
  22. #endif
  23. }
  24. +/* PG13 added a dstlen parameter to pg_b64_decode and pg_b64_encode */
  25. +#if PG13_LT
  26. +#define pg_b64_encode_compat(src, srclen, dst, dstlen) pg_b64_encode((src), (srclen), (dst))
  27. +#define pg_b64_decode_compat(src, srclen, dst, dstlen) pg_b64_decode((src), (srclen), (dst))
  28. +#else
  29. +#define pg_b64_encode_compat(src, srclen, dst, dstlen) \
  30. + pg_b64_encode((src), (srclen), (dst), (dstlen))
  31. +#define pg_b64_decode_compat(src, srclen, dst, dstlen) \
  32. + pg_b64_decode((src), (srclen), (dst), (dstlen))
  33. +#endif
  34. +
  35. #endif /* TIMESCALEDB_COMPAT_H */
  36. diff --git a/tsl/src/compression/compression.c b/tsl/src/compression/compression.c
  37. index 470ec4b9..169f74e9 100644
  38. --- a/tsl/src/compression/compression.c
  39. +++ b/tsl/src/compression/compression.c
  40. @@ -1424,7 +1424,11 @@ tsl_compressed_data_in(PG_FUNCTION_ARGS)
  41. decoded_len = pg_b64_dec_len(input_len);
  42. decoded = palloc(decoded_len + 1);
  43. - decoded_len = pg_b64_decode(input, input_len, decoded);
  44. + decoded_len = pg_b64_decode_compat(input, input_len, decoded, decoded_len);
  45. +
  46. + if (decoded_len < 0)
  47. + elog(ERROR, "could not decode base64-encoded compressed data");
  48. +
  49. decoded[decoded_len] = '\0';
  50. data = (StringInfoData){
  51. .data = decoded,
  52. @@ -1446,7 +1450,11 @@ tsl_compressed_data_out(PG_FUNCTION_ARGS)
  53. const char *raw_data = VARDATA(bytes);
  54. int encoded_len = pg_b64_enc_len(raw_len);
  55. char *encoded = palloc(encoded_len + 1);
  56. - encoded_len = pg_b64_encode(raw_data, raw_len, encoded);
  57. + encoded_len = pg_b64_encode_compat(raw_data, raw_len, encoded, encoded_len);
  58. +
  59. + if (encoded_len < 0)
  60. + elog(ERROR, "could not base64-encode compressed data");
  61. +
  62. encoded[encoded_len] = '\0';
  63. PG_RETURN_CSTRING(encoded);
  64. --
  65. 2.29.2