Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter function result type and run query can cause server to crash (utf8 only) [CORE5849] #6110

Open
firebird-automations opened this issue Jun 20, 2018 · 11 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: @livius2

SET TERM ^ ;
CREATE FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(2)
AS
DECLARE VARIABLE VAR_RESULT VARCHAR(50);
BEGIN
VAR_RESULT = A || B || C;
IF (char_length(VAR_RESULT)=20) then
VAR_RESULT = '21312321';

RETURN VAR_RESULT;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE FUNCTION FUN1(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(26)
AS
BEGIN
RETURN FUN2(A, B, C) || A;
END^
SET TERM ; ^

------------------------------------------------------------------------------------------
run the query:
SELECT FUN1('1020', '2080', '000625180347'), FUN2('1020', '2080', '000625180347') FROM RDB$DATABASE
commit;

------------------------------------------------------------------------------------------
alter result type

SET TERM ^ ;
ALTER FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(50)
AS
DECLARE VARIABLE VAR_RESULT VARCHAR(50);
BEGIN

VAR_RESULT = '1020208000062518034721312321';

RETURN VAR_RESULT;
END^
SET TERM ; ^
commit;
------------------------------------------------------------------------------------------
run the query:
SELECT FUN1('1020', '2080', '000625180347') FROM RDB$DATABASE

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

Cannot reproduce the crash in Linux.

@firebird-automations
Copy link
Collaborator Author

Commented by: @livius2

Create database with UTF8 charset and 16K page

@firebird-automations
Copy link
Collaborator Author

Commented by: @livius2

If this matter i use flamerobin for this (maybe transaction setting matter)
tested on Windows with current snapshot

WI-V3.0.4.32989 Firebird 3.0

@firebird-automations
Copy link
Collaborator Author

Modified by: @livius2

summary: Alter function result type and run query can cause server to crash => Alter function result type and run query can cause server to crash (utf8 only)

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

Cannot reproduce with ISQL wit no-autocommit in latest v3 branch:

asfernandes@cheetah:~/fb/dev/firebird-3.0.git$ isql
Use CONNECT or CREATE DATABASE to specify a database
SQL> create database 'z.fdb' page_size 16384 default character set utf8;

asfernandes@cheetah:~/fb/dev/firebird-3.0.git$ isql -n -term ! z.fdb
Database: z.fdb, User: SYSDBA
SQL> CREATE FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(2)
CON> AS
CON> DECLARE VARIABLE VAR_RESULT VARCHAR(50);
CON> BEGIN
CON> VAR_RESULT = A || B || C;
CON> IF (char_length(VAR_RESULT)=20) then
CON> VAR_RESULT = '21312321';
CON>
CON> RETURN VAR_RESULT;
CON> END!
SQL> CREATE FUNCTION FUN1(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(26)
CON> AS
CON> BEGIN
CON> RETURN FUN2(A, B, C) || A || B || C;
CON> END!
SQL> SELECT FUN1('1020', '2080', '000625180347'), FUN2('1020', '2080', '000625180347') FROM RDB$DATABASE!

FUN1 FUN2
========================== ======
Statement failed, SQLSTATE = 22001
arithmetic exception, numeric overflow, or string truncation
-string right truncation
-expected length 2, actual 4
-At function 'FUN2' line: 5, col: 3
SQL> commit!
SQL> ALTER FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(50)
CON> AS
CON> DECLARE VARIABLE VAR_RESULT VARCHAR(50);
CON> BEGIN
CON> VAR_RESULT = A || B || C;
CON> IF (char_length(VAR_RESULT)=20) then
CON> VAR_RESULT = '21312321';
CON>
CON> RETURN VAR_RESULT;
CON> END!
SQL> SELECT FUN1('1020', '2080', '000625180347'), FUN2('1020', '2080', '000625180347') FROM RDB$DATABASE !

FUN1 FUN2
========================== ==================================================
Statement failed, SQLSTATE = 22001
arithmetic exception, numeric overflow, or string truncation
-string right truncation
-expected length 2, actual 4
-At function 'FUN2' line: 5, col: 3
SQL>

@firebird-automations
Copy link
Collaborator Author

Commented by: @livius2

Hi Adriano,

Did you manage to reproduce the problem?
Did you tried this with flamerobin on Windows?

@firebird-automations
Copy link
Collaborator Author

Commented by: @livius2

problem still exists in
WI-V3.0.5.33161 Firebird 3.0
used FlameRobin 0.9.0.1748

@firebird-automations
Copy link
Collaborator Author

Modified by: @livius2

Version: 3.0.5 [ 10885 ]

environment: WI-V3.0.4.32954 Firebird 3.0 => WI-V3.0.4.32954 Firebird 3.0
WI-V3.0.5.33161 Firebird 3.0

@firebird-automations
Copy link
Collaborator Author

Commented by: @livius2

shorter testcase tested under Firebird4 from isql under Windows 7

S:\GITHUB\firebirdLivius2\temp\x64\debug\firebird>isql
Use CONNECT or CREATE DATABASE to specify a database
SQL> connect "127.0.0.1:T:\FB4\FB4utf8.fdb"
CON> user 'SYSDBA' password 'xxxx';
Database: "127.0.0.1:T:\FB4\FB4utf8.fdb", User: SYSDBA
SQL> SET TERM ^ ;
SQL> CREATE FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(2)
CON> AS
CON> DECLARE VARIABLE VAR_RESULT VARCHAR(50);
CON> BEGIN
CON> VAR_RESULT = A || B || C;
CON> IF (char_length(VAR_RESULT)=20) then
CON> VAR_RESULT = '21312321';
CON>
CON> RETURN VAR_RESULT;
CON> END^
SQL> SET TERM ; ^
SQL>
SQL>
SQL> SET TERM ^ ;
SQL> CREATE FUNCTION FUN1(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(26)
CON> AS
CON> BEGIN
CON> RETURN FUN2(A, B, C) || A;
CON> END^
SQL> SET TERM ; ^
SQL> commit;
SQL> SELECT FUN1('1020', '2080', '000625180347'), FUN2('1020', '2080', '00062518
0347') FROM RDB$DATABASE;

FUN1 FUN2
========================== ======
Statement failed, SQLSTATE = 22001
arithmetic exception, numeric overflow, or string truncation
-string right truncation
-expected length 2, actual 4
-At function 'FUN2' line: 5, col: 3
SQL> commit;
SQL> SET TERM ^ ;
SQL> ALTER FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(50)
CON> AS
CON> DECLARE VARIABLE VAR_RESULT VARCHAR(50);
CON> BEGIN
CON>
CON> VAR_RESULT = '1020208000062518034721312321';
CON>
CON> RETURN VAR_RESULT;
CON> END^
SQL> SET TERM ; ^
SQL> commit;
SQL> SELECT FUN1('1020', '2080', '000625180347') FROM RDB$DATABASE;

FUN1

Statement failed, SQLSTATE = 08006
Error reading data from the connection.
SQL>

@firebird-automations
Copy link
Collaborator Author

Modified by: @livius2

description: SET TERM ^ ;
CREATE FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(2)
AS
DECLARE VARIABLE VAR_RESULT VARCHAR(50);
BEGIN
VAR_RESULT = A || B || C;
IF (char_length(VAR_RESULT)=20) then
VAR_RESULT = '21312321';

RETURN VAR_RESULT;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE FUNCTION FUN1(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(26)
AS
BEGIN
RETURN FUN2(A, B, C) || A || B || C;
END^
SET TERM ; ^

------------------------------------------------------------------------------------------
run the query:
SELECT FUN1('1020', '2080', '000625180347'), FUN2('1020', '2080', '000625180347') FROM RDB$DATABASE
commit;

------------------------------------------------------------------------------------------
alter result type

SET TERM ^ ;
ALTER FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(50)
AS
DECLARE VARIABLE VAR_RESULT VARCHAR(50);
BEGIN
VAR_RESULT = A || B || C;
IF (char_length(VAR_RESULT)=20) then
VAR_RESULT = '21312321';

RETURN VAR_RESULT;
END^
SET TERM ; ^

------------------------------------------------------------------------------------------
run the query:
SELECT FUN1('1020', '2080', '000625180347'), FUN2('1020', '2080', '000625180347') FROM RDB$DATABASE

------------------------------------------------------------------------------------------

error reading data from the connection...

FIREBIRDDEV Wed Jun 20 09:52:34 2018
Access violation.
The code attempted to access a virtual
address without privilege to do so.
This exception will cause the Firebird server
to terminate abnormally.

=>

SET TERM ^ ;
CREATE FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(2)
AS
DECLARE VARIABLE VAR_RESULT VARCHAR(50);
BEGIN
VAR_RESULT = A || B || C;
IF (char_length(VAR_RESULT)=20) then
VAR_RESULT = '21312321';

RETURN VAR_RESULT;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE FUNCTION FUN1(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(26)
AS
BEGIN
RETURN FUN2(A, B, C) || A;
END^
SET TERM ; ^

------------------------------------------------------------------------------------------
run the query:
SELECT FUN1('1020', '2080', '000625180347'), FUN2('1020', '2080', '000625180347') FROM RDB$DATABASE
commit;

------------------------------------------------------------------------------------------
alter result type

SET TERM ^ ;
ALTER FUNCTION FUN2(A CHAR(8), B CHAR(4), C CHAR(12)) RETURNS CHAR(50)
AS
DECLARE VARIABLE VAR_RESULT VARCHAR(50);
BEGIN

VAR_RESULT = '1020208000062518034721312321';

RETURN VAR_RESULT;
END^
SET TERM ; ^
commit;
------------------------------------------------------------------------------------------
run the query:
SELECT FUN1('1020', '2080', '000625180347') FROM RDB$DATABASE

@firebird-automations
Copy link
Collaborator Author

Commented by: @livius2

I found an error cause.
It is because "ALTER FUNCTION FUN2" remove ony FUN2 from the MET cache but dependent function FUN1 is not removed from the cache.

But i do not know how to get dependencies inside CreateAlterFunctionNode::execute?

When i just added a clean whole cache by MET_clear_cache(tdbb);
all is working without the crash. But to fix it i need to remove only dependent function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant