Bug 146608 - Unpredictable CInt(), CLng() results when given erroneous date literals
Summary: Unpredictable CInt(), CLng() results when given erroneous date literals
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: Macro-StarBasic
  Show dependency treegraph
 
Reported: 2022-01-06 10:35 UTC by Alain Romedenne
Modified: 2025-07-10 20:22 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 Alain Romedenne 2022-01-06 10:35:44 UTC Comment hidden (obsolete, pls_delete)
Comment 1 Andreas Heinisch 2022-01-07 18:56:12 UTC
Hi Alain, I think you forgot to attach a sample document 😊
Comment 2 Alain Romedenne 2022-02-04 16:54:29 UTC
Ignore above comments and consider the following Basic routine:

Sub Date2Integer()
   Const DAY_ONE = #1899-12-30# ' = 1
   MAX_DATE = DateAdd("d", DAY_ONE, 2^15-2) ' #1989-09-16#
   Print CInt(#1989-09-16) , CInt(#09/16/1989) , MAX_DATE ' 1964 , 0 , #1989-09-16#
   Print CInt(#1989-09-16#), CInt(#09/16/1989#) ' 32767 , 32767
   Print CLng(#1989-09-16) , CLng(#09/16/1989) , MAX_DATE ' 1964 , 0 , #1989-09-16#
End Sub

- Erroneous date literals should raise Error #5 at runtime
OR BETTER
- Be trapped at compile time

When provided with valid date literals CInt() returns a valid 1-32667 number, otherwise raises an overflow when dates are above MAX_DATE. 

Note: DateAdd() intercepts such invalid date syntax with Error #5
Comment 3 Buovjaga 2022-12-08 12:21:44 UTC
I get a warning dialog with

 1964          0            15.09.1989

Setting to NEW.

Arch Linux 64-bit
Version: 7.5.0.0.alpha1+ (X86_64) / LibreOffice Community
Build ID: 705b2924a14841883b4a8cac549f7af326d7a185
CPU threads: 8; OS: Linux 6.0; UI render: default; VCL: kf5 (cairo+xcb)
Locale: fi-FI (fi_FI.UTF-8); UI: en-US
Calc: threaded Jumbo
Built on 8 December 2022
Comment 4 QA Administrators 2024-12-08 03:13:20 UTC Comment hidden (obsolete)
Comment 5 Alain Romedenne 2025-07-09 08:38:28 UTC
Problem is still valid

Version: 25.8.0.0.alpha0+ (X86_64) / LibreOffice Community
Build ID: fa357e4a0a44471637373302c4391e6b0c2f1a20
CPU threads: 4; OS: Linux 6.1; UI render: default; VCL: gtk3
Locale: fr-FR (fr_FR.UTF-8); UI: en-US
Calc: threaded
Comment 6 Mike Kaganski 2025-07-10 20:22:38 UTC
Code pointer:

SbiScanner::NextSym sets `bHash = true`, when it finds a starting hash, but not ending; it is expected that such a hash represents a file number (Open/Write/Seek and friends; it is checked in SbiParser::Channel); or as an odd "feature" - a comment (see the code below `PrevLineCommentLbl`, which assigns "REM" to aSym).

To fix this bug, we need to make sure in this method, that *either* the # is really treated as comment (and then it masks the rest of the line; the code like `CInt(#09/16/1989)` would become invalid in that case, and produce an error); or # appears in a context that allows / expects such a character.

My idea would be to introduce a field in parser, which would allow (or disallow by default) channel (and its #). It would be set in SbiParser::Write / SbiParser::Open / whatever; and would be checked in SbiScanner::NextSym. When channel was disallowed, and # appeared and wasn't handled as a different entity, it would set an error.

A small issue is that we will need additionally to handle statements that allow channel numbers, that we don't handle specially in parser: e.g., Seek. We would need to introduce respective methods, or otherwise make sure, that when these appear, we set the field / allow #.