firebird icon indicating copy to clipboard operation
firebird copied to clipboard

LPAD with length from DB tries to return 64k varchar and fetch via ISC API fails with 335544358 'message length error' error

Open ppazera opened this issue 8 months ago • 4 comments

Hello,

we are trying to migrate from firebird 2.5 to v5. On client side we are using Delphi with IBObjects. When using firebird v5 some 'select' statements, for example:

select lpad('6', RDB$RELATION_ID, '0') from rdb$database;

return 335544358 'message length error (encountered 2, expected 65538)' error

In isql that statement works fine. Comparing result in isql I see that in v2.5 result was 32k long, and in v5 it's 64k long

I guess the reason is that sqllen in XSQLVAR is defined as 'short' on your end (i.e. signed 16 bit integer), and the value returned by isc_dsql_prepare is -3 (the value would be 0xfffd or 65533 if it was 'unsigned short'). On our end it was defined as smallint which matched 'short', thus was treated as negative value in various places, for example when calculating and allocating buffer for isc_dsql_fetch. I reworked it to unsigned 16 bit integer and adjusted other types to calculate and allocate properly, but the error is still there. SQLDA is copied from isc_dsql_prepare to isc_dsql_fetch, on our end I now see sqllen 65533, but I guess isc_dsql_fetch on your end treats this as signed -3 thus fails

We can't fix this globally on our end (we are reworking such queries when we bump into this during execution), but we have lots of them and no guarantees we will find them all before pushing to production, thus it would be very helpful if firebird would consider reducing varchar size back to 32k (it's not possible to create 64k varchar domain, field, variable, parameter nor result, so not sure why 64k is returned and where it could be used)

we are using 'DataTypeCompatibility = 3.0' in firebird.conf for other purposes (mainly to avoid int128), but it doesn't help to shorten varchars

BTW, IBExpert fails with 'Out of memory' error, I guess due to the same reason

discussion about this in firebird support forum is here

Thanks, Paulius (GLDS)

ppazera avatar Apr 30 '25 12:04 ppazera

As someone else remarked on firebird-support, this seems only to happen if you connect with NONE, not if you specify a connection character set.

mrotteveel avatar Apr 30 '25 12:04 mrotteveel

This is duplicate of #7592. It was fixed, but then fix was reverted. I going to make almost the same fix, but then found that ticket. @asfernandes could you explain ?

hvlad avatar May 08 '25 05:05 hvlad

@hvlad look for email sent to us by Pavel Zotov: Problem "string right truncation / expected length 32765, actual 65533" after fix gh-7592

asfernandes avatar May 13 '25 00:05 asfernandes

Thanks, found it, will check

hvlad avatar May 13 '25 19:05 hvlad