https://bugs.documentfoundation.org/show_bug.cgi?id=163680
Bug ID: 163680
Summary: BASIC: 'Dim s As String * n' doesn't keep the string
length on assignments
Product: LibreOffice
Version: unspecified
Hardware: All
OS: All
Status: UNCONFIRMED
Severity: normal
Priority: medium
Component: BASIC
Assignee: [email protected]
Reporter: [email protected]
In VBA, there is a "fixed length" strings type [1], declared as
Dim s As String * 123
which takes length argument up to 2^16, and makes the string length fixed to
that value. Assigning a shorter string to it would pad spaces, and assigning a
longer string would truncate.
In LibreOffice Basic, there was always a code to support that feature, not even
dependent on the VBA support mode: the SbiSymDef::nLen member, the
SbiOpcode::PAD_ (used in SbiParser::Assign), the respective
SbiRuntime::StepPAD.
However, it has never worked properly. Before OOo 3.3, the declaration like
above would create a 0-length string variable (while it must be 123-character
from start, filled with spaces). Starting with OOo 3.3 (and LO), the
declaration creates the correct string, but following assignments break that
invariant:
s = "abc"
MsgBox Len(s) ' emits "3", instead if proper "123"
And using Let to assign the variable:
Let s = "def"
even used to crash the process before version 4.0 (likely, commit
6702bc37d4bc28ec45c6c25f6a953997f6999270 helped to fix the incorrect cast into
the variable internals, but at the same time, it broke the *idea* that the
StepPAD function is intended to modify the value on stack, that will be
assigned to the variable at the next StepPUT - the current code is basically a
no-op, modifying a local variable that is never used).
Additionally, even the original idea was broken, because it (hoped to) directly
modify the value, which might belong to another variable. *If* it worked, then
a code like this:
Dim s2 As String
s2 = "gfh"
MsgBox Len(s2) ' 3
Let s = s2
MsgBox Len(s2) ' would be 123 !!!
would suddenly make s2 equal to s, having the length of 123.
The code generator must emit the SbiOpcode::PAD_ also in SbiParser::Symbol
(which handles assignments without Let keyword), and the runtime must take care
to replace the variable on stack, when changing the length, instead of
modifying the existing variable's value there.
[1]
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/string-data-type
--
You are receiving this mail because:
You are the assignee for the bug.