Skip to content

BUG: amos/amos.h: fix scope of l in seri#92

Merged
fbourgey merged 9 commits into
scipy:mainfrom
JaRoSchm:amos_seri_fix
Jun 9, 2026
Merged

BUG: amos/amos.h: fix scope of l in seri#92
fbourgey merged 9 commits into
scipy:mainfrom
JaRoSchm:amos_seri_fix

Conversation

@JaRoSchm

@JaRoSchm JaRoSchm commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

Hi, while in the long term I am still interested of getting #50 into xsf and scipy, I am using my own Python wrapper around your C translation of AMOS in the meantime. I noticed that for some parameters the implementation of besh tries to write the result into cy[-1]. To find the origin of this I compared the xsf translation to the automatic AMOS translation from https://github.com/jpcima/zbessel. The wrapper I used for this can be found at https://github.com/JaRoSchm/pyamos. There I added a check which allows me to detect if AMOS tries to write out of the range of the result array, see https://github.com/JaRoSchm/pyamos/blob/1cd435a33db41d3bad2448991ca0e5027a87057e/pyamos.cpp#L44C1-L50C10. To see the problem, you can simply run pixi run python compare_xsf_zbessel.py there.

The bug I found using this is the scope of the variable l in the seri function (see the diff of this PR). After the goto L100 statement (line 4494), the value of l from the loop is needed and not the value l = 3 assigned beforehand. Without the change from this PR, we had l = 3 in line 4499, which is not the wanted value. The original Fortran code can be found at https://github.com/scipy/scipy/blob/4edfcaa3ce8a387450b6efce968572def71be089/scipy/special/amos/zseri.f#L147.

I thought about adding a test for this, but I am not sure, how to do this. In https://github.com/JaRoSchm/pyamos/blob/main/compare_xsf_zbessel.py I use n = 260 resulting in a array of this length, which would have to be added as a reference value. Additionally, I actually never used C++ (and its test libraries), so all of this is an extrapolation of my Python and C knowledge.

Fix the scope of l such that its value from the loop is conserved after
the goto statement in the loop's body.
@JaRoSchm JaRoSchm changed the title BUG: amos/amos.h: fix scope of l in seri BUG: amos/amos.h: fix scope of l in seri Feb 3, 2026
@fbourgey fbourgey added the Bug Something isn't working label Apr 15, 2026
JaRoSchm added a commit to JaRoSchm/xsf that referenced this pull request May 11, 2026
@JaRoSchm

Copy link
Copy Markdown
Contributor Author

I added a test to this PR which fails on main and passes here. In this case, I chose not to include 260 reference values but simply compare the "vectorized order" version to the "scalar" one. The "scalar" version is tested separately and should be fine as a reference. Additionally, I now use "test cases", which will allow me to simply add another test for #93, when this is merged.

@JaRoSchm

JaRoSchm commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@fbourgey Could you please have a look at this PR? I think this is similarly straightforward as my previous PRs to fix translation bugs in amos. After this is merged I can simply add a test case to #93 which otherwise is also ready for review.

Comment thread tests/xsf_tests/test_amos.cpp Outdated
}
}

TEST_CASE("amos besh vectorized", "[amos][xsf_tests]") {

@fbourgey fbourgey Jun 7, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems useful but should we not add a sentinel regression test as you did #158? It passes on main.

@JaRoSchm JaRoSchm Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking! Seems like I have mixed up what this PR actually fixes with some other PR. I have now added a test similar to #153 here.

Comment thread include/xsf/amos/amos.h
Comment on lines +4479 to +4480
int l;
for (l = 3; l < (nn + 1); l++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this seems to fix the translation error.

@JaRoSchm JaRoSchm closed this Jun 8, 2026
@JaRoSchm JaRoSchm reopened this Jun 8, 2026
}
}

TEST_CASE("amos seri buffer overflow gh-92", "[amos][xsf_tests]") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to run this test on upstream/main and it still passes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I tried again and for me the test fails if I revert the fix. I reverted the fix and pushed to see what CI says.

@JaRoSchm JaRoSchm Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, looks like you are using macOS? Then CI agrees with you. On Linux CI reproduces the failure I observe. So the effect of the bug might be ill-defined in C++ and gcc/clang give different results. Could this be the origin of the differences?

Would you be willing to merge if I revert the last commit? Looks like we agree that the fix is correct and we have a regression test at least for some of the CI jobs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those are memory bugs. I would expect CI to fail for all three Operating Systems.

Comment thread tests/xsf_tests/test_amos.cpp Outdated
JaRoSchm and others added 2 commits June 9, 2026 12:44
Co-authored-by: Florian Bourgey <bourgeyflorian@gmail.com>
@JaRoSchm

JaRoSchm commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

CI failed successfully, see https://github.com/scipy/xsf/pull/92/checks?sha=8a3cb53b1425a2de1d73dd67fe959bc6513ec017. I put the fix back in. I will update the test for #93 accordingly.

@fbourgey fbourgey merged commit 83c21ea into scipy:main Jun 9, 2026
6 of 8 checks passed
@fbourgey

fbourgey commented Jun 9, 2026

Copy link
Copy Markdown
Member

Thanks!

@JaRoSchm

Copy link
Copy Markdown
Contributor Author

Thank you for the review and for improving the test!

@JaRoSchm JaRoSchm deleted the amos_seri_fix branch June 10, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants