Skip to content

Commit 53e6388

Browse files
committed
Fix some memory issues that were masked on macos
* fixed proc source allocation, we should always use `NAMEDATALEN` * eliminate extra outer SPI_connect, it should not be there * change some notification levels to `DEBUG3` * clean up string copy for text based OIDs in `pljs_jsvalue_to_datum` * set undefined arguments specifically to `JS_UNDEFINED`
1 parent 238b9b0 commit 53e6388

File tree

3 files changed

+12
-25
lines changed

3 files changed

+12
-25
lines changed

src/cache.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,8 @@ void pljs_function_cache_to_context(pljs_context *context,
235235

236236
memcpy(context->function->proname, function_entry->proname, NAMEDATALEN);
237237

238-
context->function->prosrc =
239-
(char *)palloc(strlen(function_entry->prosrc) + 1);
240-
memcpy(context->function->prosrc, function_entry->prosrc,
241-
strlen(function_entry->prosrc));
238+
context->function->prosrc = (char *)palloc(NAMEDATALEN);
239+
memcpy(context->function->prosrc, function_entry->prosrc, NAMEDATALEN);
242240
}
243241

244242
/**

src/pljs.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ static JSValueConst *convert_arguments_to_javascript(FunctionCallInfo fcinfo,
302302
if (fcinfo && IsPolymorphicType(argtype)) {
303303
argtype = get_fn_expr_argtype(fcinfo->flinfo, i);
304304
}
305-
if (fcinfo->args[i].isnull == 1) {
305+
if (fcinfo->args[inargs].isnull == 1) {
306306
argv[inargs] = JS_NULL;
307307
} else {
308308
argv[inargs] = pljs_datum_to_jsvalue(fcinfo->args[inargs].value, argtype,
@@ -386,11 +386,6 @@ Datum pljs_call_handler(PG_FUNCTION_ARGS) {
386386

387387
ReleaseSysCache(proctuple);
388388

389-
// Connect to the SPI manager for any calls.
390-
if (SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0) != SPI_OK_CONNECT) {
391-
elog(ERROR, "could not connect to spi manager");
392-
}
393-
394389
if (is_trigger) {
395390
// Call in the context of a trigger.
396391
Form_pg_proc procStruct;
@@ -407,8 +402,6 @@ Datum pljs_call_handler(PG_FUNCTION_ARGS) {
407402
retval = pljs_call_function(fcinfo, &context, argv);
408403
}
409404

410-
SPI_finish();
411-
412405
return retval;
413406
}
414407

@@ -474,7 +467,7 @@ Datum pljs_call_validator(PG_FUNCTION_ARGS) {
474467
JSContext *ctx;
475468

476469
if (fcinfo->flinfo->fn_extra) {
477-
elog(NOTICE, "fn_extra on validate");
470+
elog(DEBUG3, "fn_extra on validate");
478471
}
479472
proctuple = SearchSysCache(PROCOID, ObjectIdGetDatum(fn_oid), 0, 0, 0);
480473

src/types.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,13 @@ JSValue pljs_datum_to_jsvalue(Datum arg, Oid argtype, JSContext *ctx) {
307307
char *buf = palloc(VARSIZE_ANY_EXHDR(p) + 1);
308308
memcpy(buf, VARDATA(p), VARSIZE_ANY_EXHDR(p));
309309

310-
// buf[VARSIZE_ANY_EXHDR(p)] = '\0';
311310
return_result = JS_NewStringLen(ctx, buf, VARSIZE_ANY_EXHDR(p));
312311
pfree(buf);
313312
break;
314313
}
315314

316315
default:
317-
elog(NOTICE, "Unknown type: %d", argtype);
316+
elog(DEBUG3, "Unknown type: %d", argtype);
318317
return_result = JS_NULL;
319318
}
320319

@@ -519,18 +518,16 @@ Datum pljs_jsvalue_to_datum(JSValue val, Oid rettype, JSContext *ctx,
519518
size_t plen;
520519
const char *str = JS_ToCStringLen(ctx, &plen, val);
521520

522-
text *t = (text *)palloc(plen + VARHDRSZ);
523-
SET_VARSIZE(t, plen + VARHDRSZ);
524-
memcpy(VARDATA(t), str, plen);
521+
Datum ret = CStringGetTextDatum(str);
525522
JS_FreeCString(ctx, str);
526523

527-
PG_RETURN_TEXT_P(t);
524+
return ret;
528525
break;
529526
}
530527

531528
case JSONOID: {
532529
JSValueConst *argv = &val;
533-
JSValue js = JS_JSONStringify(ctx, argv[0], argv[1], argv[2]);
530+
JSValue js = JS_JSONStringify(ctx, argv[0], JS_UNDEFINED, JS_UNDEFINED);
534531
size_t plen;
535532
const char *str = JS_ToCStringLen(ctx, &plen, js);
536533

@@ -545,10 +542,9 @@ Datum pljs_jsvalue_to_datum(JSValue val, Oid rettype, JSContext *ctx,
545542

546543
case JSONBOID: {
547544
JSValueConst *argv = &val;
548-
JSValue js = JS_JSONStringify(ctx, argv[0], argv[1], argv[2]);
545+
JSValue js = JS_JSONStringify(ctx, argv[0], JS_UNDEFINED, JS_UNDEFINED);
549546
size_t plen;
550547
const char *str = JS_ToCStringLen(ctx, &plen, js);
551-
552548
// return it as a Datum, since there is no direct CStringGetJsonb exposed.
553549
Datum ret = (Datum)DatumGetJsonbP(
554550
DirectFunctionCall1(jsonb_in, (Datum)(char *)str));
@@ -653,11 +649,11 @@ Datum pljs_jsvalue_to_datum(JSValue val, Oid rettype, JSContext *ctx,
653649

654650
return PointerGetDatum(buffer);
655651
} else {
656-
elog(NOTICE, "Unknown array type, tag: %lld", val.tag);
652+
elog(DEBUG3, "Unknown array type, tag: %lld", val.tag);
657653
for (uint8_t i = 0; i < 255; i++) {
658654
void *res = JS_GetOpaque(val, i);
659655
if (res != NULL) {
660-
elog(NOTICE, "class_id: %d", i);
656+
elog(DEBUG3, "class_id: %d", i);
661657
}
662658
}
663659

@@ -666,7 +662,7 @@ Datum pljs_jsvalue_to_datum(JSValue val, Oid rettype, JSContext *ctx,
666662
}
667663

668664
default:
669-
elog(NOTICE, "Unknown type: %d", rettype);
665+
elog(DEBUG3, "Unknown type: %d", rettype);
670666
if (fcinfo) {
671667
PG_RETURN_NULL();
672668
} else {

0 commit comments

Comments
 (0)