The following bug has been logged on the website:
Bug reference:      17706
Logged by:          Sergey Shinderuk
Email address:      s.shinderuk@postgrespro.ru
PostgreSQL version: 15.1
Operating system:   Ubuntu 22.04
Description:
With PL/pgSQL:
create type foo as (a int, b int);
create function bar() returns record as $$
declare
        r foo := row(123, 2^30);
begin
        alter type foo alter attribute b type text;
        return r;
end;
$$ language plpgsql;
postgres=# select bar();
server closed the connection unexpectedly
  This probably means the server terminated abnormally
  before or while processing the request.
LOG:  server process (PID 394076) was terminated by signal 11: Segmentation
fault
(Here 2^30 is interpreted as a string length.)
With a cursor:
postgres=# create type foo as (a int, b int);
CREATE TYPE
postgres=# begin;
BEGIN
postgres=*# declare c cursor for select (i, 2^30)::foo from
generate_series(1,10) i;
DECLARE CURSOR
postgres=*# fetch c;
      row       
----------------
 (1,1073741824)
(1 row)
postgres=*# fetch c;
      row       
----------------
 (2,1073741824)
(1 row)
postgres=*# alter type foo alter attribute b type text;
ALTER TYPE
postgres=*# fetch c;
server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.