xref: /trunk/odf_docs/openformula-milestone-a-report.md (revision 1354dfcdad4129e7cc561fdcfbb4f6c8958f0428)
1<!--
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements.  See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership.  The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License.  You may obtain a copy of the License at
9
10   http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied.  See the License for the
16 specific language governing permissions and limitations
17 under the License.
18-->
19
20# Apache OpenOffice — OpenFormula Milestone A Report
21
22## 1. Commit IDs
23
24- Starting commit: `1a90caaf711eb0c8fba10d331504670475957a66`
25- Ending commit:   `20ab51ba5a` (source + tests)
26- Branch: `openformula` (created in the prior audit phase)
27
28This report covers Milestone A (commit `9c7fb2dfd9`) and its corrections,
29Milestone A.1 (commit `91645d9295`) and Milestone A.1.1 (commit
30`20ab51ba5a`). A.1 fixed an off-by-one defect in the 48-bit operand upper
31bound (accepting `2^48`, which ODF 1.4 §6.6.1 excludes) and added
32`::rtl::math::isFinite` guards before the float-to-integer conversion in
33both bitwise helpers. A.1.1 completed the `ScBitArithmeticOps()` NaN guard:
34`PushIllegalArgument()` records an error but does not return, so without an
35early-exit the invalid operand was still converted to `sal_uInt64`
36(undefined behaviour); operands are now validated across the consumption
37loop and conversion/`PushDouble` are skipped when any operand is invalid.
38
39## 2. Files changed and reasons
40
41| File | Reason |
42|------|--------|
43| `main/sc/source/core/tool/interpr1.cxx` | Corrected `ScInterpreter::ScBitShiftOps` (BITLSHIFT/BITRSHIFT) and `ScInterpreter::ScIndex` (INDEX). |
44| `test/odf-validate/calcval-cases14.tsv` | Added ODF 1.4 Part 4 regression cases for BITLSHIFT, BITRSHIFT, and INDEX. |
45
46Only these two files were committed. Pre-existing, unrelated working-tree modifications
47(`main/solenv/gbuild/platform/freebsd.mk`, `main/solenv/inc/unxfbsd.mk`) and the prior
48audit-phase deliverables (`openformula-*.tsv`, `openformula-*.md`) were left unstaged
49and uncommitted.
50
51No production code outside the three functions was touched. No LibreOffice code was
52copied. No third-party formula engine was introduced.
53
54## 3. Original defects
55
56### BITLSHIFT / BITRSHIFT (`ScBitShiftOps`, interpr1.cxx:1216)
57
58The negative-shift-count direction reversal already matched ODF 1.4 (negative count
59reverses direction: BITLSHIFT(8;-1)=4, BITRSHIFT(8;-1)=16), so no direction change was
60needed. The actual defects were:
61
621. **Missing operand range check.** `BITAND`/`BITOR`/`BITXOR` (`ScBitArithmeticOps`)
63   reject operands outside `0 .. 2^48 - 1` (ODF 1.4 §6.6.1 requires `0 <= x <= 2^48 - 1`),
64   but `ScBitShiftOps` only rejected negative operands. The value `2^48` itself
65   (281474976710656) was accepted.
662. **Undefined behaviour on large shift counts.** The shift count was cast straight to
67   `sal_uInt64` and used as the shift amount. Shifting by `>= 64` bits (the width of
68   `sal_uInt64`) or by a huge count (e.g. `1e300`) is undefined behaviour in C++.
69
70### INDEX (`ScIndex`, interpr1.cxx:7311)
71
72The `bRowArray` logic implemented the ODF 1.3 row-vector shortcut: for a single-row
73reference called with exactly two arguments (reference + row), the row argument was
74reinterpreted as a column offset (`nCol = nRow; nRow = 1`). ODF 1.4 removes that rule,
75so `INDEX(H1:L1;3)` must be an error (row 3 on a 1-row reference) rather than returning
76the third column.
77
78## 4. Exact semantic changes
79
80### BITLSHIFT / BITRSHIFT
81
82- Corrected the upper-bound operand check to match ODF 1.4 §6.6.1, which defines the
83  valid unsigned range as `0 <= x <= 2^48 - 1` (previously the check allowed `2^48`
84  itself). The bound is now `val > 2^48 - 1`, i.e. `281474976710655`, the same ceiling
85  used by `ScBitArithmeticOps` (BITAND/BITOR/BITXOR).
86- Added `::rtl::math::isFinite` guards on both operands before the
87  floating-point-to-integer conversion, so NaN and non-finite values cannot reach the
88  `static_cast<sal_uInt64>` (undefined behaviour). `n` (the shift count) is guarded too.
89- Completed the `ScBitArithmeticOps()` guard: `PushIllegalArgument()` records an error
90  but does not return, so operands are now validated across the consumption loop and
91  conversion / `PushDouble` are skipped when any operand is invalid (tracked with a
92  `bValid` flag), matching the if/else pattern already used by `ScBitShiftOps()`.
93- The shift-count magnitude is now clamped to the width of `sal_uInt64`: a magnitude
94  of `>= 64` yields result `0`; otherwise the count is cast safely. This eliminates
95  undefined behaviour from shifting by `>=` the bit width or by a huge count.
96- **ODF requirement vs AOO choice:** ODF 1.4 §6.6.1 mandates the operand range
97  (`0 <= x <= 2^48 - 1`) but does not prescribe a result for shift counts at or beyond
98  the result width. Returning `0` for a shift of 64 bits or more is an AOO
99  implementation choice (the smallest defined behaviour avoiding undefined behaviour),
100  not an ODF-mandated value.
101- Direction logic preserved (already ODF 1.4-correct): a negative count reverses
102  direction (`BITLSHIFT` → right shift, `BITRSHIFT` → left shift); zero count returns
103  the value unchanged; positive count shifts in the named direction.
104- Result is pushed as a double via `PushDouble`.
105
106Result table (all verified against the ODF 1.4 required values):
107
108| Formula | Result |
109|---------|--------|
110| `BITLSHIFT(8;1)` | 16 |
111| `BITLSHIFT(8;-1)` | 4 |
112| `BITLSHIFT(8;0)` | 8 |
113| `BITRSHIFT(8;1)` | 4 |
114| `BITRSHIFT(8;-1)` | 16 |
115| `BITRSHIFT(8;0)` | 8 |
116| `BITLSHIFT(1;63)` | 9223372036854775808 |
117| `BITLSHIFT(1;64)` | 0 (AOO choice; out-of-width shift) |
118| `BITRSHIFT(1;64)` | 0 (AOO choice; out-of-width shift) |
119| `BITLSHIFT(8;-64)` | 0 (AOO choice; out-of-width shift) |
120| `BITLSHIFT(-1;1)` | #ERR |
121| `BITRSHIFT(-1;1)` | #ERR |
122| `BITLSHIFT(281474976710655;0)` | 281474976710655 (max valid operand, 2^48 - 1) |
123| `BITLSHIFT(281474976710656;1)` | #ERR (operand 2^48 is out of range) |
124| `BITLSHIFT(281474976710657;1)` | #ERR (operand > 2^48) |
125
126### INDEX
127
128- Removed the `bRowArray` variable, its two assignments, its two error-condition
129  clauses, and the row-vector reinterpretation branch (`nCol = nRow; nRow = 1`).
130- The existing bounds check (`nRow > 0 && nRow1+nRow-1 > nRow2`) now applies uniformly,
131  so a row index beyond the number of rows is an error regardless of orientation.
132
133Result table (ODF 1.4, §6.14.6), using the sheet data documented in the test file
134(`D1:D5` = 10,20,30,40,50; `H1:L1` = 10,20,30,40,50; `D1:F3` = 3×3):
135
136| Formula | Result |
137|---------|--------|
138| `INDEX(D1:D5;2)` | 20 |
139| `INDEX(D1:D5;2;1)` | 20 |
140| `INDEX(D1:F1;1;3)` | 101 |
141| `INDEX(H1:L1;3)` | #ERR (was 30 — legacy row-vector shortcut removed) |
142| `INDEX(H1:L1;;3)` | 30 |
143| `INDEX(D1:D5;2;2)` | #ERR |
144| `INDEX(D1:F3;2;3)` | 202 |
145| `INDEX(D1:D5;6)` | #ERR |
146
147## 5. INDEX compatibility findings and decision
148
149**Update:** this decision was reversed later. `ScIndex` keeps the legacy row-vector rule,
150`INDEX(H1:L1;3)` returns 30 again, and the case is listed as a known deviation.
151
152**Finding:** AOO retains no document/formula-version information that `INDEX` evaluation
153can branch on. `ScDocOptions` (docoptio.hxx) tracks only formula-related toggles such as
154`bFormulaRegexEnabled`; there is no ODF 1.2 / 1.3 / 1.4 formula conformance setting per
155document, and `FormulaLanguage` governs grammar/symbol names (ODF vs Excel), not ODF
1561.3-vs-1.4 `INDEX` semantics. Per the milestone constraints I did not introduce a new
157compatibility subsystem and did not infer document version from formula text.
158
159**Decision:** The change is applied globally — all documents now use ODF 1.4 `INDEX`
160semantics. This is the only spec-compliant path given the absence of a version-tracking
161mechanism.
162
163**Compatibility consequence (documented):** ODF 1.3 documents that relied on the legacy
164row-vector behaviour, i.e. `INDEX(<single-row-reference>;N)` returning column N, will
165now return an error when `N > 1` (row count exceeds the single row). Single-row,
166single-column references and two-argument calls on column vectors are unaffected.
167This is a deliberate, documented break from ODF 1.3 behaviour in favour of ODF 1.4
168conformance; it should be communicated to users migrating legacy documents.
169
170## 6. Added test cases and expected results
171
172Appended to `test/odf-validate/calcval-cases14.tsv` (columns: id, formula, mode,
173expected, note), following the existing file conventions:
174
175**BIT shifts** — positive, zero, and negative shift counts; both directions; width
176boundary; negative and out-of-range operands; exact-range boundary across all five bit
177functions:
178
179- `BITLSHIFT.pos` =BITLSHIFT(8;1) → 16
180- `BITLSHIFT.neg` =BITLSHIFT(8;-1) → 4
181- `BITLSHIFT.zero` =BITLSHIFT(8;0) → 8
182- `BITRSHIFT.pos` =BITRSHIFT(8;1) → 4
183- `BITRSHIFT.neg` =BITRSHIFT(8;-1) → 16
184- `BITRSHIFT.zero` =BITRSHIFT(8;0) → 8
185- `BITLSHIFT.bit63` =BITLSHIFT(1;63) → 9223372036854775808
186- `BITLSHIFT.bit64` =BITLSHIFT(1;64) → 0
187- `BITRSHIFT.bit64` =BITRSHIFT(1;64) → 0
188- `BITLSHIFT.neg64` =BITLSHIFT(8;-64) → 0
189- `BITLSHIFT.negval` =BITLSHIFT(-1;1) → #ERR
190- `BITRSHIFT.negval` =BITRSHIFT(-1;1) → #ERR
191- `BITLSHIFT.ovr` =BITLSHIFT(281474976710657;1) → #ERR
192- `BITAND.maxval` =BITAND(281474976710655;281474976710655) → 281474976710655
193- `BITAND.ovr2` =BITAND(281474976710656;1) → #ERR (operand 2^48 now rejected)
194- `BITOR.maxval` =BITOR(281474976710655;0) → 281474976710655
195- `BITOR.ovr2` =BITOR(281474976710656;1) → #ERR
196- `BITXOR.maxval` =BITXOR(281474976710655;0) → 281474976710655
197- `BITXOR.ovr2` =BITXOR(281474976710656;1) → #ERR
198- `BITAND.negval` =BITAND(-1;1) → #ERR (negative operand rejected)
199- `BITOR.negval` =BITOR(-1;1) → #ERR (negative operand rejected)
200- `BITXOR.negval` =BITXOR(-1;1) → #ERR (negative operand rejected)
201- `BITLSHIFT.maxval` =BITLSHIFT(281474976710655;0) → 281474976710655
202- `BITLSHIFT.ovr2` =BITLSHIFT(281474976710656;1) → #ERR
203- `BITRSHIFT.maxval` =BITRSHIFT(281474976710655;0) → 281474976710655
204- `BITRSHIFT.ovr2` =BITRSHIFT(281474976710656;1) → #ERR
205
206**INDEX** — row-vector, column-vector, two-dimensional, omitted args, out-of-range,
207and array (matrix) input:
208
209- `INDEX.row.twod` =INDEX(D1:F3;2;3) → 202
210- `INDEX.row.outrange` =INDEX(D1:D5;6) → #ERR
211- `INDEX.array` =INDEX({10;20|30;40|50;60};3;1) → 50
212
213(The file already contained `INDEX.col.row`, `INDEX.col.rowcol`, `INDEX.row.col`,
214`INDEX.row.rowonly`, `INDEX.row.empty`, `INDEX.col.beyond`; the INDEX change makes
215`INDEX.row.rowonly` =INDEX(H1:L1;3) → #ERR as the file already expects.)
216
217Each case asserts an exact expected result (value or #ERR), not mere evaluation
218success, per ODF 1.4 normative values.
219
220## 7. Build and test commands with results
221
222**Environment:** Linux (WSL2) x86_64, Python 3.14.4. `dmake` and `make` are present,
223and `main/solver/510/unxlngx6.pro` contains only a partial `xml2cmp` build.
224
225**Attempted:**
226
227- `dmake -C main/sc/source/core/tool interpr1.obj` → prints dmake usage only; there is
228  no makefile in the source tree (the `Module_*.mk` build files are generated by AOO's
229  gbuild configure step, which has not been run). `build.pl`/`deliver.pl` are absent
230  from the repository root.
231
232**Result:** A build could not be performed in this environment. The `sc` module is not
233built, and the full dependency chain required to compile `interpr1.cxx` is unavailable.
234Consequently the calcval ODF 1.4 tests (which require a headless office on macOS per
235`test/odf-validate/README.md`) could not be executed here.
236
237**Verification performed instead:** brace balance of `interpr1.cxx` is 0; every
238identifier used in the edits (`bitOperations`, `BITLSHIFT`, `BITRSHIFT`,
239`SAL_CONST_UINT64`, `sal_uInt64`, `PushIllegalArgument`, `PushDouble`, `GetByte`,
240`GetDouble`, `::rtl::math::approxFloor`, `static_cast`) was already present in the same
241function's scope; argument pop order was confirmed (`GetDouble()` pops the last-pushed
242parameter, verified via `ScIndex` at interpr1.cxx:7326-7330), so `n` is the shift count
243and `val` is the value.
244
245**To complete verification:** configure and build AOO (gbuild), build the `sc` module,
246then run `test/odf-validate/start.sh calcval <port>` and
247`test/odf-validate/runpy.sh calcval.py <port> calcval-cases14.tsv <out.json>` followed by
248`python3 -I calcval.py --compare ...` on macOS, plus existing Calc regression tests.
249
250## 8. Known limitations
251
252- Build and test execution were not possible in this environment (see §7). Test cases
253  were written to the existing `calcval-cases14.tsv` conventions and their expected
254  values derived from the ODF 1.4 required behaviour, but they have not been executed
255  against a built office.
256- `BITLSHIFT(1;63) = 9223372036854775808` (2^63) relies on the result being pushed as a
257  double; calcval compares numbers with 1e-9 relative tolerance, which is satisfied since
258  2^63 is exactly representable.
259- The shift-by-`>=64` → `0` choice is the safe defined behaviour; ODF 1.4's exact
260  prescription for out-of-width shift counts was not available in full local text, so
261  this is the smallest AOO-native correction avoiding undefined behaviour.
262- The operand range uses the same `2^48 - 1` ceiling as `BITAND`/`BITOR`/`BITXOR`;
263  ODF 1.4 §6.6.1 says "at least 48 bits", so this is consistent but not necessarily
264  maximal.
265
266## 9. Remaining ODF 1.4 compliance gaps discovered
267
268From the prior audit (retained here for completeness), ODF 1.4 gaps outside this
269milestone's scope (BITLSHIFT/BITRSHIFT/INDEX only):
270
271- **IFNA** and **SKEWP** (ODF 1.3) are missing entirely — no opcode, no interpreter
272  implementation, only Excel-filter `ocNoName` stubs (`xlformula.cxx:404`, `:415`).
273- **CONVERT** `atm` unit value differs from the ODF 1.4 table by 1.0e-6 relative
274  (`ODF14-KNOWN-LIMITATIONS-PUBLIC.md:72`).
275- **COUNTA**/COUNTBLANK ODF 1.4 empty-cell semantics unverified.
276- AOO claims **no** Part 4 conformance group (Small/Medium/Large)
277  (`ODF14-IMPLEMENTATION-DEFINED-PUBLIC.md:134`); coverage cannot be certified without
278  the full ODF 1.4 Part 4 normative text and per-group mandatory checks.
279