-- Regression test for the NULL-fcinfo crash in pljs_jsvalue_to_datum(). -- -- pljs.execute() binds its parameters by calling pljs_jsvalue_to_datum() with -- fcinfo == NULL (see pljs_execute_params()). Two bind paths -- the bytea -- "unrecognised value" fallback and the non-Date date/timestamp fallback -- -- originally used PG_RETURN_NULL(), which expands to -- `fcinfo->isnull = true; return (Datum) 0` and crashed the backend by -- dereferencing the NULL fcinfo. -- -- Those paths now reject an unconvertible value with a clean, catchable error -- (instead of silently binding SQL NULL, which hid real binding mistakes) and -- crucially never touch fcinfo -- so the backend must stay alive either way. -- -- This covers the *bind* form specifically. pg_composite_null_datum covers the -- composite/record form of the same conversion, and pg_conversion_footguns -- covers the data-loss semantics; the overlap is deliberate, because these are -- distinct call paths into the same function and each has crashed on its own. CREATE EXTENSION IF NOT EXISTS pljs; NOTICE: extension "pljs" already exists, skipping -- site 1: unrecognised "binary" JS value bound to bytea -> clean error, no crash. DO $$ function bind_bytea(v) { try { pljs.execute('SELECT $1::bytea AS b', [ v ]); return 'no error'; } catch (e) { return 'err'; } } pljs.elog(NOTICE, 'bytea reject: ' + bind_bytea({}) + ' ' + bind_bytea(new Float64Array([1, 2]))); $$ LANGUAGE pljs; NOTICE: bytea reject: err err -- site 2: non-Date JS value bound to date / timestamp -> clean error, no crash. DO $$ function bind(sql, v) { try { pljs.execute(sql, [ v ]); return 'no error'; } catch (e) { return 'err'; } } pljs.elog(NOTICE, 'date/timestamp reject: ' + bind('SELECT $1::date AS d', 12345) + ' ' + bind('SELECT $1::timestamp AS t', {})); $$ LANGUAGE pljs; NOTICE: date/timestamp reject: err err -- The recognised forms still bind, so the rejection is narrow. DO $js$ const b = pljs.execute('SELECT $1::bytea AS b', [ new Uint8Array([1, 2, 3]) ])[0].b; const d = pljs.execute("SELECT ($1::date = DATE '2020-01-02') AS ok", [ new Date(Date.UTC(2020, 0, 2)) ])[0].ok; pljs.elog(NOTICE, 'accepted forms: bytea len=' + b.length + ' date ok=' + d); $js$ LANGUAGE pljs; NOTICE: accepted forms: bytea len=3 date ok=true -- The backend survived all of it. SELECT 1 AS alive; alive ------- 1 (1 row)