https://bugs.documentfoundation.org/show_bug.cgi?id=146082

            Bug ID: 146082
           Summary: In Basic , Structures stored in an array
           Product: LibreOffice
           Version: 7.1.2.2 release
          Hardware: All
                OS: All
            Status: UNCONFIRMED
          Severity: normal
          Priority: medium
         Component: BASIC
          Assignee: [email protected]
          Reporter: [email protected]

I found a bug in LibreOffice 7.2.2.2 on Fedora Linux as supplied by Fedora and
Windows 7.1.2.2. Next I installed 7.2.4.1 for Windows and tested it again with
the same results.

I was asked by Wolfgang Jäger [email protected] about a bug he found in
LibreOffice related structures stored in Arrays in Basic. I have verified that
there is a bug, but I wanted feedback before I file the bug report and I have
NOT at this point looked at the code. 

I expect a Structure to copy by value, which it seems to do, but a structure in
an array does NOT act as expected. It is even worse for user defined data
types. The details are complicated, but, if an array of structures does this: 

a(1) = a(2)

It might act like the value copied and it might not (depending on the
circumstances and how the array was declared). 

This bug was originally noted with a user defined type so I wrote a test using
a built-in UNO Struct com.sun.star.awt.Point

Test using User type “PersonType”

Type PersonType
  FirstName As String
  LastName As String
End Type

Sub PrintPersons(s, x)
  Print "Expect (" & s & ") found (" & x.FirstName & " " & x.LastName & ")"
End Sub

Sub PrintPoint(s, x)
  Print "Expect " & s & " Found (" & x.X & ", " & x.Y & ")"
End Sub

This is the original disturbing code. Attempting to copy an element from the
array causes problems. The same when trying to assign back into to the array. 


Sub otherExampleCreateNewType
REM Producing very strange errors. 
REM Inspect the two array elements and h at the same time
REM down to the inside variables while executing lines 19 through 21 stepwise. 

Dim Person(1 To 2) As PersonType
Person(1).FirstName = "Andrew"
Person(1).LastName = "Pitonyak"
Person(2).FirstName = "Funny"
Person(2).LastName = "Lupp"

PrintPersons("Andrew", Person(1)) ' Andrew (correct)
PrintPersons("Funny", Person(2)) ' Funny (correct)

Dim h As PersonType
h = Person(1)
Person(1) = Person(2)
Person(2) = h
PrintPersons("Funny", Person(1)) ' Andrew (wrong)
PrintPersons("Andrew", Person(2)) ' Funny (wrong)

REM Now look at the prints, and frown.
Dim h1 As PersonType, h2 As PersonType
h1 = Person(1)
h2 = Person(2)
PrintPersons("Funny", h1) ' Funny (correct)
PrintPersons("Andrew", h2) ' Funny (wrong)

End Sub

Next I run this using a built-in UNO type and have exactly the same incorrect
behaviors
Sub otherExampleCreateAWTPoint

Dim p(1 To 2) As New com.sun.star.awt.Point
p(1).X = 1
p(1).Y = 2
p(2).X = 3
p(2).Y = 4

PrintPoint("(1, 2)", p(1)) ' Correct
PrintPoint("(3, 4)", p(2)) ' Correct

Dim h As New com.sun.star.awt.Point
h = p(1)
p(1) = p(2)
p(2) = h
PrintPoint("(3, 4)", p(1)) ' wrong
PrintPoint("(1, 2)", p(2)) ' wrong

REM Now look at the prints, and frown.
Dim h1 As New com.sun.star.awt.Point, h2 As New com.sun.star.awt.Point
h1 = p(1)
h2 = p(2)
PrintPoint("(3, 4)", h1)  ' Correct
PrintPoint("(1, 2)", h2)  ' Wrong

End Sub

I then changed how the array is declared as shown below and this fixed the
problematic

Dim p(1 To 2) As Object
p(1) = CreateUnoStruct( "com.sun.star.awt.Point" )
p(2) = CreateUnoStruct( "com.sun.star.awt.Point" )

Doing the same for the PersonType also causes it to work as expected:

Dim Person(1 To 2) As Object
Person(1) = CreateObject("PersonType")
Person(2) = CreateObject("PersonType")

It gets worse. If I modify the subroutine to always make a copy of the object
and then use the object that is passed to the subroutine, then suddenly the
first program works for points, but the same fix does not work when done for
PersonType.

Sub PrintPoint(s, y)
  Dim x
  x = y
  Print "Expect " & s & " Found (" & x.X & ", " & x.Y & ")"
End Sub

Clearly something is wrong / inconsistent.

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to