Bug 43490 - Redim function with option base set to 1 not working correctly
Summary: Redim function with option base set to 1 not working correctly
Status: RESOLVED NOTABUG
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
3.3.4 release
Hardware: All Linux (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-03 10:48 UTC by mieszcz
Modified: 2021-10-09 12:32 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description mieszcz 2011-12-03 10:48:19 UTC
If option base is set to 1, the redim function make wrong calculation of new table size, as in following example:

OPTION BASE 1

Sub Main
DIM table() AS VARIANT
DIM size AS INTEGER

size= ubound(table )
REDIM PRESERVE table( size + 1)


size= ubound(table )
REDIM PRESERVE table( size + 1)


size= ubound(table )
REDIM PRESERVE table( size + 1)


End Sub


During tracing of this program, to size of the table is changing:
1 -> 3 -> 5

And should be changing:

1 -> 2 -> 3
Comment 1 sasha.libreoffice 2012-04-27 07:38:23 UTC
Thanks for bugreport
reproduced in 3.3.4 and 3.5.2 on Fedora 64 bit
Variable "size" becomes -1 first time and increases by 2 in each redim statement
Comment 2 sasha.libreoffice 2012-04-27 07:39:56 UTC
@ Noel
What do You think about this bug?
Comment 3 Noel Power 2012-04-30 02:08:15 UTC
well I think mostly the problem here is the expectation that libreoffice basic behaves in exactly the same way as microsoft vba does. That isn't the case

ubound will return -1 for an unitialized array regardless of 'base' setting. This is different from microsoft vba which will return 0 with a 'base' setting of 1

array initialisation e.g. Array ( x ) initialises the array to contain x + 1 elements again regardless of the 'base' setting, again this is different to microsoft vba which will initialise an array to contain x elements in the case of 'base' 1. If you consider this behaviour when looking at the macro example given then the observed behaviour can be explained. 
Unfortunately even though this behaviour is not intuitive to those familiar with microsoft vba I would imagine this ( odd ) behaviour is already accepted for libreoffice/openoffice basic and I can't see it could be easily changed without some major fallout in existing legacy macros.

There is another ( little known and somewhat unofficial ) option that can help here ( 'option compatible' ) which can help


option compatible
option base 1
Sub Main
DIM table()

msgbox ubound(table)
table = Array()

DIM size AS INTEGER

size= ubound(table )
if size < 0 then size = 0
REDIM PRESERVE table( size + 1)


size= ubound(table )
REDIM PRESERVE table( size + 1)


size= ubound(table )
REDIM PRESERVE table( size + 1)


End Sub

note: the line

if size < 0 then size = 0

if there is any bug it would be imho that ubound should return 0 for the unitialised/empty array
Comment 4 sasha.libreoffice 2012-04-30 05:20:50 UTC
Thanks for explanations and for reminder about 'option compatible' 
It will seriously help using Basic
May be this option may set globally for all documents? Somewhere in Tools->Options.
Comment 5 jacky joy 2021-08-05 10:21:47 UTC
Yes. I think that is exactly what OP was asking for. Thanks for taking this on.