Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e73b50

Browse files
committed
Workaround for perl problem where evaluating UTF8 regexes can cause
implicit loading of modules, thereby breaking Safe rules. We compile and call a tiny perl function on trusted interpreter init, after which the problem does not occur.
1 parent 895a94d commit 6e73b50

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/pl/plperl/GNUmakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Makefile for PL/Perl
2-
# $PostgreSQL: pgsql/src/pl/plperl/GNUmakefile,v 1.31 2007/07/25 10:17:46 mha Exp $
2+
# $PostgreSQL: pgsql/src/pl/plperl/GNUmakefile,v 1.32 2007/12/01 15:20:34 adunstan Exp $
33

44
subdir = src/pl/plperl
55
top_builddir = ../../..
@@ -27,6 +27,7 @@ override CFLAGS += -Wno-comment
2727
endif
2828

2929
override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) -I$(perl_archlibexp)/CORE
30+
override CFLAGS += -fPIC
3031

3132
rpathdir = $(perl_archlibexp)/CORE
3233

src/pl/plperl/plperl.c

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**********************************************************************
22
* plperl.c - perl as a procedural language for PostgreSQL
33
*
4-
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.132 2007/11/15 22:25:17 momjian Exp $
4+
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.133 2007/12/01 15:20:34 adunstan Exp $
55
*
66
**********************************************************************/
77

@@ -149,6 +149,8 @@ static HV *plperl_spi_execute_fetch_result(SPITupleTable *, int, int);
149149
static SV *newSVstring(const char *str);
150150
static SV **hv_store_string(HV *hv, const char *key, SV *val);
151151
static SV **hv_fetch_string(HV *hv, const char *key);
152+
static SV *plperl_create_sub(char *proname, char *s, bool trusted);
153+
static SV *plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo);
152154

153155
/*
154156
* This routine is a crock, and so is everyplace that calls it. The problem
@@ -504,6 +506,53 @@ plperl_safe_init(void)
504506
else
505507
{
506508
eval_pv(SAFE_OK, FALSE);
509+
if (GetDatabaseEncoding() == PG_UTF8)
510+
{
511+
512+
/*
513+
* Fill in just enough information to set up this perl
514+
* function in the safe container and call it.
515+
* For some reason not entirely clear, it prevents errors that
516+
* can arise from the regex code later trying to load
517+
* utf8 modules.
518+
*/
519+
520+
plperl_proc_desc desc;
521+
FunctionCallInfoData fcinfo;
522+
FmgrInfo outfunc;
523+
HeapTuple typeTup;
524+
Form_pg_type typeStruct;
525+
SV *ret;
526+
SV *func;
527+
528+
/* make sure we don't call ourselves recursively */
529+
plperl_safe_init_done = true;
530+
531+
/* compile the function */
532+
func = plperl_create_sub(
533+
"utf8fix",
534+
"return shift =~ /\\xa9/i ? 'true' : 'false' ;",
535+
true);
536+
537+
538+
/* set up to call the function with a single text argument 'a' */
539+
desc.reference = func;
540+
desc.nargs = 1;
541+
desc.arg_is_rowtype[0] = false;
542+
fcinfo.argnull[0] = false;
543+
fcinfo.arg[0] =
544+
DatumGetTextP(DirectFunctionCall1(textin,
545+
CStringGetDatum("a")));
546+
typeTup = SearchSysCache(TYPEOID,
547+
TEXTOID,
548+
0, 0, 0);
549+
typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
550+
fmgr_info(typeStruct->typoutput,&(desc.arg_out_func[0]));
551+
ReleaseSysCache(typeTup);
552+
553+
/* and make the call */
554+
ret = plperl_call_perl_func(&desc,&fcinfo);
555+
}
507556
}
508557

509558
plperl_safe_init_done = true;

0 commit comments

Comments
 (0)