Skip to content

Conversation

@bbrtj
Copy link
Contributor

@bbrtj bbrtj commented Dec 14, 2025

This change addresses a long-standing but rather uncommon bug in checking for replacing scalars in a readonly array. In rare cases, modifying the last scalar in a readonly array will croak with Modification of a read-only value attempted. This happens because the comparison treats the last index of the array as if it was array size.

Here is a script which reproduces this bug:

use feature qw(refaliasing);
my @xs = qw(a b c);
Internals::SvREADONLY(@xs, 1);

\$xs[1] = \42;
\$xs[2] = \42;

Prints:

Aliasing via reference is experimental at test.pl line 5.
Aliasing via reference is experimental at test.pl line 6.
Modification of a read-only value attempted at test.pl line 6.

  • This set of changes does not require a perldelta entry.

@khwilliamson
Copy link
Contributor

Thank you for this.

I would propose though phrasing this in terms of av_count which we invented because of how easy AvFILL is to misuse, as this bug demonstrates. I probably should have tried expunging most occurrences of AvFILL at the time. That might have caught this one.

@bbrtj
Copy link
Contributor Author

bbrtj commented Dec 15, 2025

I think AvFILL actually makes sense in this place. x > AvFILL seems more natural here than x >= av_count, because we actually want to check against the last index and need to replace > with >= when checking against the array count. But if you still think it's better to have it use av_count instead, then I can of course do that.

@tonycoz
Copy link
Contributor

tonycoz commented Dec 16, 2025

Could you please add a regression test?

bbrtj added a commit to bbrtj/perl5 that referenced this pull request Dec 16, 2025
@bbrtj
Copy link
Contributor Author

bbrtj commented Dec 16, 2025

Added a test, I hope I've put it in the right place.

bbrtj added a commit to bbrtj/perl5 that referenced this pull request Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants