fix: prevent segfault when package has subproc with param-initialized local var (#1005)#1123
fix: prevent segfault when package has subproc with param-initialized local var (#1005)#1123gaoxueyu merged 2 commits intoIvorySQL:masterfrom
Conversation
… local var Skip subproc datums in plisql_add_initdatums() to prevent parent block from initializing nested function/procedure variables. This fixes crash when subproc local var default references its own parameter. Fixes: IvorySQL#1005
WalkthroughPrevents parent-block initialization of datums that belong to nested subprocedures by adding a helper to detect subproc-owned datum indices and skipping those datums during init-datums collection. Adds SQL tests exercising nested subprocedures and parameter-initialized local variables. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
🔇 Additional comments (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/pl/plisql/src/sql/plisql_nested_subproc.sql (1)
3860-3888: Minor: Test 5 could be more comprehensive.The
func1nested function is defined with a param-initialized local variable (line 3876) but is never invoked or used in the return calculation. While this doesn't detract from validating the crash fix, including a call tofunc1or incorporating its return value into the aggregation (e.g.,v_sum := v_sum + func1(10)) would strengthen confidence in the fix across all nested subprogram types. The inline comment on line 3883 suggests this is a known limitation; if intentional, no action is needed.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (2)
src/pl/plisql/src/expected/plisql_nested_subproc.outis excluded by!**/*.outsrc/pl/plisql/src/expected/plisql_type_rowtype.outis excluded by!**/*.out
📒 Files selected for processing (2)
src/pl/plisql/src/pl_comp.c(3 hunks)src/pl/plisql/src/sql/plisql_nested_subproc.sql(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: rophy
Repo: IvorySQL/IvorySQL PR: 998
File: contrib/ivorysql_ora/src/builtin_packages/dbms_output/dbms_output.c:82-146
Timestamp: 2025-12-03T06:50:27.755Z
Learning: In contrib/ivorysql_ora/src/builtin_packages/dbms_output/dbms_output.c, the behavior where ENABLE() clears the existing buffer (rather than preserving it like Oracle does) is an intentional known difference tracked in issue #26 at https://github.com/rophy/IvorySQL/issues/26 (medium impact, open).
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: oracle_regression (ubuntu-latest)
- GitHub Check: build (ubuntu-latest)
- GitHub Check: contrib_regression
- GitHub Check: pg_regression (ubuntu-latest)
- GitHub Check: meson_build (ubuntu-latest)
- GitHub Check: oracle_pg_regression (ubuntu-latest)
🔇 Additional comments (3)
src/pl/plisql/src/pl_comp.c (2)
3285-3320: ✓ Correct helper function to identify subproc datumsThe new
plisql_datum_belongs_to_subproc()function correctly identifies whether a datum index belongs to any subprocedure by iterating through all subprocs and checking if the datum falls within each subproc's range[lastassignvardno, lastoutvardno). The guards onsubproc->function == NULL && subproc->lastoutvardno == 0properly exclude unfinalized or forward-declared subprocs.
3348-3359: ✓ Consistent application of subproc datum skip logicBoth the datum-counting loop and the varnos-building loop in
plisql_add_initdatums()consistently skip subproc datums. This ensures that subproc parameters and local variables are never added to the parent block's initialization list, preventing the segfault when the parent entry tries to initialize memory that belongs to the subproc's execution context.Also applies to: 3382-3384
src/pl/plisql/src/sql/plisql_nested_subproc.sql (1)
3773-3890: Excellent comprehensive test coverage for issue #1005.The five test cases effectively validate the fix by covering the key scenarios:
- Package with private procedure (param-initialized local; called and not called)
- Standalone function with nested procedure (called)
- Standalone function with nested function (never called)
- Multiple nested subprocedures and functions
The tests exercise both the crash-prevention path (uncalled subprocs) and the functional path (called subprocs) as intended.
Add 5 test cases covering the segfault in PL/iSQL packages with private procedures having local variables initialized from parameters: 1. Package with private proc that has param-initialized local var 2. Call the private proc to verify it works when invoked 3. Standalone function with nested procedure 4. Nested function with param-initialized var that is never called 5. Multiple nested subprocs with param-initialized local vars These tests verify the fix prevents the crash when: - A private procedure has a local var initialized from its parameter - The parent function is called without invoking the private proc
yasir-hussain-shah
left a comment
There was a problem hiding this comment.
@rophy
Fix is working good. Regression tests are well written which covers related cases.
Just a question out of my curiosity, the changes in the file
src/pl/plisql/src/expected/plisql_type_rowtype.out
seems unrelated to this fix. To verify, I reverted changes in this file and noted that: make oracle-check still passes, however, make oracle-check-world causes a regression in above file. That's strange.
Regards,
Yasir Hussain Shah
Data Bene
The change to Before the fix: When After the fix: Nested function (
This is more correct behavior - the error now points to the nested function This also matches Oracle's behavior where nested function variables are initialized at call time, not at parent function entry time. |
|
This fix looks very good and solid. Thanks for your contribution again! |
Summary
initialized from a parameter
Root Cause
When compiling nested subprocedures (private procedures/functions within a package or standalone function), the subproc's datums
(including parameters and local variables) were incorrectly included in the parent function's
initvarnosarray byplisql_add_initdatums().When the parent function was called, it attempted to initialize these subproc datums, but since the subproc hadn't been invoked yet, the
datum memory wasn't properly allocated - causing a segfault.
Fix
Added
plisql_datum_belongs_to_subproc()helper function to check if a datum index falls within any registered subproc's datum range.Modified
plisql_add_initdatums()to skip datums that belong to subprocs.This ensures subproc variables are only initialized when the subproc itself is called, not when the parent function is entered.
Test Plan
oracle-check-worldpassesplisql_nested_subproc.sql:Fixes #1005
Summary by CodeRabbit
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.