Skip to content

Commit

Permalink
Fix some memory issues that were masked on macos
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
JerrySievert committed Nov 8, 2024
1 parent 238b9b0 commit 53e6388
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,8 @@ void pljs_function_cache_to_context(pljs_context *context,

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

context->function->prosrc =
(char *)palloc(strlen(function_entry->prosrc) + 1);
memcpy(context->function->prosrc, function_entry->prosrc,
strlen(function_entry->prosrc));
context->function->prosrc = (char *)palloc(NAMEDATALEN);
memcpy(context->function->prosrc, function_entry->prosrc, NAMEDATALEN);
}

/**
Expand Down
11 changes: 2 additions & 9 deletions src/pljs.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ static JSValueConst *convert_arguments_to_javascript(FunctionCallInfo fcinfo,
if (fcinfo && IsPolymorphicType(argtype)) {
argtype = get_fn_expr_argtype(fcinfo->flinfo, i);
}
if (fcinfo->args[i].isnull == 1) {
if (fcinfo->args[inargs].isnull == 1) {
argv[inargs] = JS_NULL;
} else {
argv[inargs] = pljs_datum_to_jsvalue(fcinfo->args[inargs].value, argtype,
Expand Down Expand Up @@ -386,11 +386,6 @@ Datum pljs_call_handler(PG_FUNCTION_ARGS) {

ReleaseSysCache(proctuple);

// Connect to the SPI manager for any calls.
if (SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0) != SPI_OK_CONNECT) {
elog(ERROR, "could not connect to spi manager");
}

if (is_trigger) {
// Call in the context of a trigger.
Form_pg_proc procStruct;
Expand All @@ -407,8 +402,6 @@ Datum pljs_call_handler(PG_FUNCTION_ARGS) {
retval = pljs_call_function(fcinfo, &context, argv);
}

SPI_finish();

return retval;
}

Expand Down Expand Up @@ -474,7 +467,7 @@ Datum pljs_call_validator(PG_FUNCTION_ARGS) {
JSContext *ctx;

if (fcinfo->flinfo->fn_extra) {
elog(NOTICE, "fn_extra on validate");
elog(DEBUG3, "fn_extra on validate");
}
proctuple = SearchSysCache(PROCOID, ObjectIdGetDatum(fn_oid), 0, 0, 0);

Expand Down
20 changes: 8 additions & 12 deletions src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,13 @@ JSValue pljs_datum_to_jsvalue(Datum arg, Oid argtype, JSContext *ctx) {
char *buf = palloc(VARSIZE_ANY_EXHDR(p) + 1);
memcpy(buf, VARDATA(p), VARSIZE_ANY_EXHDR(p));

// buf[VARSIZE_ANY_EXHDR(p)] = '\0';
return_result = JS_NewStringLen(ctx, buf, VARSIZE_ANY_EXHDR(p));
pfree(buf);
break;
}

default:
elog(NOTICE, "Unknown type: %d", argtype);
elog(DEBUG3, "Unknown type: %d", argtype);
return_result = JS_NULL;
}

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

text *t = (text *)palloc(plen + VARHDRSZ);
SET_VARSIZE(t, plen + VARHDRSZ);
memcpy(VARDATA(t), str, plen);
Datum ret = CStringGetTextDatum(str);
JS_FreeCString(ctx, str);

PG_RETURN_TEXT_P(t);
return ret;
break;
}

case JSONOID: {
JSValueConst *argv = &val;
JSValue js = JS_JSONStringify(ctx, argv[0], argv[1], argv[2]);
JSValue js = JS_JSONStringify(ctx, argv[0], JS_UNDEFINED, JS_UNDEFINED);
size_t plen;
const char *str = JS_ToCStringLen(ctx, &plen, js);

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

case JSONBOID: {
JSValueConst *argv = &val;
JSValue js = JS_JSONStringify(ctx, argv[0], argv[1], argv[2]);
JSValue js = JS_JSONStringify(ctx, argv[0], JS_UNDEFINED, JS_UNDEFINED);
size_t plen;
const char *str = JS_ToCStringLen(ctx, &plen, js);

// return it as a Datum, since there is no direct CStringGetJsonb exposed.
Datum ret = (Datum)DatumGetJsonbP(
DirectFunctionCall1(jsonb_in, (Datum)(char *)str));
Expand Down Expand Up @@ -653,11 +649,11 @@ Datum pljs_jsvalue_to_datum(JSValue val, Oid rettype, JSContext *ctx,

return PointerGetDatum(buffer);
} else {
elog(NOTICE, "Unknown array type, tag: %lld", val.tag);
elog(DEBUG3, "Unknown array type, tag: %lld", val.tag);
for (uint8_t i = 0; i < 255; i++) {
void *res = JS_GetOpaque(val, i);
if (res != NULL) {
elog(NOTICE, "class_id: %d", i);
elog(DEBUG3, "class_id: %d", i);
}
}

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

default:
elog(NOTICE, "Unknown type: %d", rettype);
elog(DEBUG3, "Unknown type: %d", rettype);
if (fcinfo) {
PG_RETURN_NULL();
} else {
Expand Down

0 comments on commit 53e6388

Please sign in to comment.