PostgreSQL Source Code  git master
plperl_helpers.h File Reference
#include "mb/pg_wchar.h"
#include "plperl.h"
Include dependency graph for plperl_helpers.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

static char * utf_u2e (char *utf8_str, size_t len)
 
static char * utf_e2u (const char *str)
 
static char * sv2cstr (SV *sv)
 
static SV * cstr2sv (const char *str)
 
static void croak_cstr (const char *str)
 

Function Documentation

◆ croak_cstr()

static void croak_cstr ( const char *  str)
inlinestatic

Definition at line 140 of file plperl_helpers.h.

References cstr2sv(), dTHX, get_sv, pfree(), and utf_e2u().

Referenced by plperl_return_next(), plperl_spi_commit(), plperl_spi_exec(), plperl_spi_exec_prepared(), plperl_spi_fetchrow(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), plperl_spi_rollback(), and plperl_util_elog().

141 {
142  dTHX;
143 
144 #ifdef croak_sv
145  /* Use sv_2mortal() to be sure the transient SV gets freed */
146  croak_sv(sv_2mortal(cstr2sv(str)));
147 #else
148 
149  /*
150  * The older way to do this is to assign a UTF8-marked value to ERRSV and
151  * then call croak(NULL). But if we leave it to croak() to append the
152  * error location, it does so too late (only after popping the stack) in
153  * some Perl versions. Hence, use mess() to create an SV with the error
154  * location info already appended.
155  */
156  SV *errsv = get_sv("@", GV_ADD);
157  char *utf8_str = utf_e2u(str);
158  SV *ssv;
159 
160  ssv = mess("%s", utf8_str);
161  SvUTF8_on(ssv);
162 
163  pfree(utf8_str);
164 
165  sv_setsv(errsv, ssv);
166 
167  croak(NULL);
168 #endif /* croak_sv */
169 }
#define dTHX
Definition: ppport.h:3208
void pfree(void *pointer)
Definition: mcxt.c:1031
static char * utf_e2u(const char *str)
static SV * cstr2sv(const char *str)
#define get_sv
Definition: ppport.h:3878

◆ cstr2sv()

static SV* cstr2sv ( const char *  str)
inlinestatic

Definition at line 112 of file plperl_helpers.h.

References dTHX, GetDatabaseEncoding(), pfree(), PG_SQL_ASCII, and utf_e2u().

Referenced by croak_cstr(), hstore_to_plperl(), JsonbValue_to_SV(), make_array_ref(), plperl_call_perl_func(), plperl_call_perl_trigger_func(), plperl_create_sub(), plperl_event_trigger_build_args(), plperl_hash_from_tuple(), plperl_spi_execute_fetch_result(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), and plperl_trigger_build_args().

113 {
114  dTHX;
115  SV *sv;
116  char *utf8_str;
117 
118  /* no conversion when SQL_ASCII */
120  return newSVpv(str, 0);
121 
122  utf8_str = utf_e2u(str);
123 
124  sv = newSVpv(utf8_str, 0);
125  SvUTF8_on(sv);
126  pfree(utf8_str);
127 
128  return sv;
129 }
#define dTHX
Definition: ppport.h:3208
void pfree(void *pointer)
Definition: mcxt.c:1031
static char * utf_e2u(const char *str)
int GetDatabaseEncoding(void)
Definition: mbutils.c:996

◆ sv2cstr()

static char* sv2cstr ( SV *  sv)
inlinestatic

Definition at line 54 of file plperl_helpers.h.

References dTHX, GetDatabaseEncoding(), isGV_with_GP, PG_SQL_ASCII, SvREFCNT_inc_simple_void, utf_u2e(), and val.

Referenced by hek2cstr(), plperl_call_perl_event_trigger_func(), plperl_call_perl_func(), plperl_call_perl_trigger_func(), plperl_create_sub(), plperl_init_interp(), plperl_spi_prepare(), plperl_sv_to_datum(), plperl_to_hstore(), plperl_trigger_handler(), plperl_trusted_init(), plperl_untrusted_init(), plperl_util_elog(), select_perl_context(), and SV_to_JsonbValue().

55 {
56  dTHX;
57  char *val,
58  *res;
59  STRLEN len;
60 
61  /*
62  * get a utf8 encoded char * out of perl. *note* it may not be valid utf8!
63  */
64 
65  /*
66  * SvPVutf8() croaks nastily on certain things, like typeglobs and
67  * readonly objects such as $^V. That's a perl bug - it's not supposed to
68  * happen. To avoid crashing the backend, we make a copy of the sv before
69  * passing it to SvPVutf8(). The copy is garbage collected when we're done
70  * with it.
71  */
72  if (SvREADONLY(sv) ||
73  isGV_with_GP(sv) ||
74  (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM))
75  sv = newSVsv(sv);
76  else
77  {
78  /*
79  * increase the reference count so we can just SvREFCNT_dec() it when
80  * we are done
81  */
83  }
84 
85  /*
86  * Request the string from Perl, in UTF-8 encoding; but if we're in a
87  * SQL_ASCII database, just request the byte soup without trying to make
88  * it UTF8, because that might fail.
89  */
91  val = SvPV(sv, len);
92  else
93  val = SvPVutf8(sv, len);
94 
95  /*
96  * Now convert to database encoding. We use perl's length in the event we
97  * had an embedded null byte to ensure we error out properly.
98  */
99  res = utf_u2e(val, len);
100 
101  /* safe now to garbage collect the new SV */
102  SvREFCNT_dec(sv);
103 
104  return res;
105 }
#define dTHX
Definition: ppport.h:3208
static char * utf_u2e(char *utf8_str, size_t len)
#define SvREFCNT_inc_simple_void(sv)
Definition: ppport.h:4725
int GetDatabaseEncoding(void)
Definition: mbutils.c:996
long val
Definition: informix.c:684
#define isGV_with_GP(gv)
Definition: ppport.h:5367

◆ utf_e2u()

static char* utf_e2u ( const char *  str)
inlinestatic

Definition at line 34 of file plperl_helpers.h.

References pg_server_to_any(), PG_UTF8, and pstrdup().

Referenced by croak_cstr(), and cstr2sv().

35 {
36  char *ret;
37 
38  ret = pg_server_to_any(str, strlen(str), PG_UTF8);
39 
40  /* ensure we have a copy even if no conversion happened */
41  if (ret == str)
42  ret = pstrdup(ret);
43 
44  return ret;
45 }
char * pstrdup(const char *in)
Definition: mcxt.c:1161
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:626

◆ utf_u2e()

static char* utf_u2e ( char *  utf8_str,
size_t  len 
)
inlinestatic

Definition at line 15 of file plperl_helpers.h.

References pg_any_to_server(), PG_UTF8, and pstrdup().

Referenced by sv2cstr().

16 {
17  char *ret;
18 
19  ret = pg_any_to_server(utf8_str, len, PG_UTF8);
20 
21  /* ensure we have a copy even if no conversion happened */
22  if (ret == utf8_str)
23  ret = pstrdup(ret);
24 
25  return ret;
26 }
char * pstrdup(const char *in)
Definition: mcxt.c:1161
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:553