Bug 140485 - Password does not get set to Protect Sheet when added through LibreOffice BASIC
Summary: Password does not get set to Protect Sheet when added through LibreOffice BASIC
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium enhancement
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-02-17 18:30 UTC by An-Kh
Modified: 2021-03-30 11:53 UTC (History)
4 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 An-Kh 2021-02-17 18:30:43 UTC
In Libreoffice BASIC, if we type

Sub LockThisSheet
    LockSheet("Sheet1")
End Sub

Function LockSheet(passedSheetname) 
    Dim oDoc        As Object : oDoc    = ThisComponent
    Dim oSheet      As Object : oSheet  = oDoc.Sheets.getByName(passedSheetname)
    Dim pPassword   As String
    If IsMissing(pPassword) Then pPassword = "SomePassword"
    oSheet.UnProtect(pPassword)
    oSheet.Protect(pPassword)
End Function

Sheet is protected without the password.


Version: 7.2.0.0.alpha0+ (x64) / LibreOffice Community
Build ID: 6b15a8658f369e4144251854bcdb736acb595f47
CPU threads: 4; OS: Windows 10.0 Build 19042; UI render: default; VCL: win
Locale: en-IN (en_IN); UI: en-US
Calc: threaded
Comment 1 [REDACTED] 2021-02-18 13:28:30 UTC
The problem is with

> If IsMissing(pPassword) Then pPassword = "SomePassword" 

which does not set the password pPassword to "SomePassword". Runtime function "IsMissing" tests if a function is called with an _optional_ parameter and function "LockSheet" has no optional parameter called pPassword. Therefore pPassword stays empty and protection is activated with an "empty" password. Change the code to something like:

>Sub LockThisSheet
>    LockSheet("Sheet1")
>End Sub
>
>Function LockSheet(passedSheetname, optional pPassword ) 
>
>    Dim oDoc        As Object : oDoc    = ThisComponent
>    Dim oSheet      As Object : oSheet  = oDoc.Sheets.getByName(passedSheetname)
>    
>    If IsMissing(pPassword) Then pPassword = "SomePassword" 
>    oSheet.UnProtect(pPassword)
>    oSheet.Protect(pPassword)
>
>End Function

From my perspective not a bug
Comment 2 An-Kh 2021-02-18 16:30:26 UTC
(In reply to Uwe Auer from comment #1)
> The problem is with
> 
> > If IsMissing(pPassword) Then pPassword = "SomePassword" 
> 
> which does not set the password pPassword to "SomePassword". Runtime
> function "IsMissing" tests if a function is called with an _optional_
> parameter and function "LockSheet" has no optional parameter. called
> pPassword. Therefore pPassword stays empty and protection is activated with
> an "empty" password. Change the code to something like:
> 
> >Sub LockThisSheet
> >    LockSheet("Sheet1")
> >End Sub
> >
> >Function LockSheet(passedSheetname, optional pPassword ) 
> >
> >    Dim oDoc        As Object : oDoc    = ThisComponent
> >    Dim oSheet      As Object : oSheet  = oDoc.Sheets.getByName(passedSheetname)
> >    
> >    If IsMissing(pPassword) Then pPassword = "SomePassword" 
I think that in this line it says that if the pPassword is missing set the pPassword to "SomePassword". Since here pPassword is missing, pPassword is set to "SomePassword".
> >    oSheet.UnProtect(pPassword)
> >    oSheet.Protect(pPassword)
Here it protects the sheet with pPassword
> >
> >End Function
> 
> From my perspective not a bug
Comment 3 [REDACTED] 2021-02-18 16:44:22 UTC
(In reply to An-Kh from comment #2)

I did understand your code, but probably you did not understand my answer. To be precise and clear: Your code is wrong and I explained why.

> If IsMissing(pPassword) Then pPassword = "SomePassword"
is not doing what you understand / assume it is doing. It doesn *not* test whether variable is set. Runtime function IsMissing() tests for  *optional* parameter in your function definition (i.e. it tests, whether *pPassword* has been passed to function LockSheet(passedSheetname)) - but your function doesn't have that optional parameter at all. 

Therefore provided a solution (which you obviously did not check) to change:

- Function LockSheet(passedSheetname) 
+ Function LockSheet(passedSheetname, optional pPassword ) 

In other words: The declaration of your function *LockSheet* is incorrect with respect of using runtime function *IsMissing()*

Explanation
- means "remove" from your code
+ means "add" to your code
Comment 4 [REDACTED] 2021-02-18 16:51:52 UTC
(In reply to An-Kh from comment #2)
> 
> Therefore provided a solution (which you obviously did not check) to change:
> 

Change to my previous comment:

 - Dim pPassword  As String
 - Function LockSheet(passedSheetname) 
 + Function LockSheet(passedSheetname, optional pPassword )
Comment 5 An-Kh 2021-02-18 17:24:04 UTC
Hi

I think that in this case, some kind of dialog box should pop up telling that 

Dim pPassword   As String
If IsMissing(pPassword)

is not allowed.

I saw this code in the Bug 133257
Comment 7 [REDACTED] 2021-02-18 17:58:14 UTC
(In reply to An-Kh from comment #5)

> I saw this code in the Bug 133257

You saw it in a bug, which indicates that it did not work
Comment 8 An-Kh 2021-02-18 20:22:16 UTC
(In reply to Uwe Auer from comment #7)
> (In reply to An-Kh from comment #5)
> 
> > I saw this code in the Bug 133257
> 
> You saw it in a bug, which indicates that it did not work

That bug was unrelated to this one. It was related to UI in Calc during protection
Comment 9 An-Kh 2021-02-18 20:27:25 UTC
(In reply to himajin100000 from comment #6)
> note:
> 
> https://opengrok.libreoffice.org/xref/core/basic/source/runtime/methods.
> cxx?r=3482f590#2500

Hii

I saw the code in the above link. It should give an error message if the parameters are entered wrongly in IfMissing(). 
But here it is neither giving the error message nor the protection is applied.
Comment 10 himajin100000 2021-02-18 20:33:29 UTC
IMHO, It looks at least to me that in order to give compile-time error "IsMissing" has to be a language construct rather than a function.
Comment 11 Andreas Heinisch 2021-02-18 21:01:18 UTC
Imho, the behaviour is correct, since, if IsMissing is used on a non optional parameter it always gives false.
Comment 12 Andreas Heinisch 2021-02-18 21:11:52 UTC
But we could throw a runtime error ...
Comment 13 An-Kh 2021-02-18 21:26:42 UTC
(In reply to Andreas Heinisch from comment #12)
> But we could throw a runtime error ...

Yess.. I think some kind of indication should be there
Comment 14 [REDACTED] 2021-02-18 22:59:20 UTC
(In reply to Andreas Heinisch from comment #12)
> But we could throw a runtime error ...

that's ok, but then it is not a bug but an enhancement request. I stick with the statements that the code is wrong and documentation is clear about correct usage of IsMissing - it tests whether a optional function parameter has been passed to the function or not and that. And the function as defined in the report has no optional parameter which could be tested.
Comment 15 Andreas Heinisch 2021-02-19 06:50:57 UTC
Could be a valid enhancement request, but it may break exisiting (but wrong) macros. However, the function does what the documentation states.
Comment 16 Xisco Faulí 2021-03-30 11:53:01 UTC
(In reply to Andreas Heinisch from comment #15)
> Could be a valid enhancement request, but it may break exisiting (but wrong)
> macros. However, the function does what the documentation states.

Moving to NEW then