Page 1 of 1

Firebird + empty strings, a problem

Posted: 16.07.2022, 19:51
by Milos
Hi all, first post so let me just start by saying thank you for your library!

I stumbled upon a problem when looking at a table with some string fields empty (not null, just empty)

Actual data in DB:

Code: Select all

ID Name1 Name2
1  'A'     '1'
2  'B'     ''
3  ''      '3'
4  'D'     '4'
mormot (or zeos) returns:
{"ID":1,"Name1":"A","Name2":"1"}
{"ID":2,"Name1":"B","Name2":""}
{"ID":3,"Name1":"A","Name2":"3"} --- Name1 should be blank!
{"ID":4,"Name1":"D","Name2":"4"}

I did lots of digging and it turned out that this function

function TZInterbase6XSQLDAResultSet.GetPAnsiChar(ColumnIndex: Integer; out Len: NativeUInt): PAnsiChar;

incorrectly returned 'A' as result for 3rd record's "Name1" field, while Len parameter was correctly returned as zero.

The other GetPAnsiChar function which doesn't take Len parameter correctly returned empty string.

The source of the problem may be in GetPCharFromTextVar function which is called by above.
Unless if there is a better way the problem can be fixed by changing the case for SQL_VARYING in GetPCharFromTextVar to this

Code: Select all

    SQL_VARYING:
      begin
        Len := PISC_VARYING(sqldata).strlen;
        if Len = 0
           then P := nil
           else P := @PISC_VARYING(sqldata).str[0];
      end;

Re: Firebird + empty strings, a problem

Posted: 18.07.2022, 10:53
by marsupilami
Hello Milos,

which Version of Zeos do you use? As far as I can see Zeos 7.2 as well as Zeos 8 use the same method for fetching values of type SQL_VARYING. I did your test on my computer and get the expected result:
ID NAME1 NAME2
1  "A"   "1"
2  "B"   ""
3  ""    "3"
4  "D"   "4"

Re: Firebird + empty strings, a problem

Posted: 18.07.2022, 11:40
by Milos
I appologize, this should be in 7.2 forum, I tried to find a way to contact an admin to tell them but I couldn't find a link

Re: Firebird + empty strings, a problem

Posted: 18.07.2022, 11:46
by Milos
Also I am using FB 2.5 in case it matters.

Re: Firebird + empty strings, a problem

Posted: 19.07.2022, 11:49
by Milos
Which function did you test?

GetPAnsiChar function that doesn't have Len parameter fixes the problem within itself by doing

Code: Select all

  ZSetString(P, Len, FRawTemp);
  Result := Pointer(FRawTemp);
but the one that does have Len param leaves the string content as-is.

Re: Firebird + empty strings, a problem

Posted: 05.08.2022, 11:18
by Milos
Can an admin move this to 7.2 please?

Also I can provide a sample project if needed, using Mormot, but not using zeos because I am not familiar enough with how it works sorry.

Re: Firebird + empty strings, a problem

Posted: 05.08.2022, 12:37
by marsupilami
Milos wrote: 05.08.2022, 11:18 Can an admin move this to 7.2 please?
Moved ;)
Milos wrote: 05.08.2022, 11:18Also I can provide a sample project if needed, using Mormot, but not using zeos because I am not familiar enough with how it works sorry.
That is not necessary at the moment. I just didn't have the possibility to look into this yet. :(

Re: Firebird + empty strings, a problem

Posted: 12.08.2022, 09:00
by Milos
No problem and thank you for your work.

Mormot souce code has since been modified to take this into consideration i.e. it checks the Len parameter in order to determine if string is actually empty so a Mormot example would no longer show a problem, but I think it's still worth making a change in Zeos.

Re: Firebird + empty strings, a problem

Posted: 15.08.2022, 08:55
by Fr0sT
This is the bug indeed, though calling functions should never use a value when Len is 0 - but we live in a non-ideal world so additional check wouldn't hurt.
Created PR with your fix

Re: Firebird + empty strings, a problem

Posted: 15.08.2022, 09:33
by marsupilami
I applied the patch to Zeos 8.0 and Zeos 7.2. Thank you :)