From 53e6388f57cd7db69bdb8bc4e8505931ab41e71c Mon Sep 17 00:00:00 2001 From: Jerry Sievert Date: Thu, 7 Nov 2024 19:36:24 -0800 Subject: [PATCH] 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` --- src/cache.c | 6 ++---- src/pljs.c | 11 ++--------- src/types.c | 20 ++++++++------------ 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/cache.c b/src/cache.c index ac7d5c4..8d2b7c3 100644 --- a/src/cache.c +++ b/src/cache.c @@ -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); } /** diff --git a/src/pljs.c b/src/pljs.c index d4ac34f..71ba372 100644 --- a/src/pljs.c +++ b/src/pljs.c @@ -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, @@ -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; @@ -407,8 +402,6 @@ Datum pljs_call_handler(PG_FUNCTION_ARGS) { retval = pljs_call_function(fcinfo, &context, argv); } - SPI_finish(); - return retval; } @@ -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); diff --git a/src/types.c b/src/types.c index 46126ab..b1713a9 100644 --- a/src/types.c +++ b/src/types.c @@ -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; } @@ -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); @@ -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)); @@ -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); } } @@ -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 {