← Back to Pull requests

Remove the broken `pointer(x::AbstractArray)` fallback #63436

Open
nhz2 opened this pull request with 10 lines diff across 4 files
View on GitHub
nhz2 nhz2 commented on Sep 27, 2026 21:42 UTC
Author

The generic pointer(x::AbstractArray) returned
unsafe_convert(Ptr{T}, cconvert(Ptr{T}, x)) since #51764. The object returned by cconvert is only guaranteed to be kept alive while it is rooted, as it is during a ccall, but callers of pointer only preserve x itself. When cconvert allocates a new object, the returned pointer can refer to memory that has already been freed.

Remove the broken pointer(x::AbstractArray) fallback, and give the array wrappers that previously relied on cconvert to reach their parent (PermutedDimsArray, CodeUnits, SharedArray, strided SubArray, ReshapedArray and ReinterpretArray) unsafe_convert methods that forward to unsafe_convert of the parent, so nested wrappers keep working.

Fixes #51962, #59685, and #60408

CC: @jakobnissen @chriselrod @eschnett

Assisted-by: Claude Code (Opus 5.5)

Diff: 10 lines (+7 / -3) across 4 files Reviewer: nhz2 Review duration: 70428m
All Vouch review confidence checks passed
Residual risk 0.08 = Change Risk (0.48) × (1 − Review Confidence (0.82)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 10 lines diff across 4 files
0.48
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.92 · 5 substantive comment(s)
0.92
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
82%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.143
Self Review self_review
0.140
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
16 items
nhz2 nhz2 commented on code on Sep 27, 2026 21:45 UTC
Code Review
base/abstractarray.jl:1
@@ -1276,7 +1276,7 @@ end
 # note: the following type definitions don't mean any AbstractArray is convertible to
 # a data Ref. they just map the array element type to the pointer type for
 # convenience in cases that work.
-pointer(x::AbstractArray{T}) where {T} = unsafe_convert(Ptr{T}, cconvert(Ptr{T}, x))
+pointer(x::AbstractArray{T}) where {T} = unsafe_convert(Ptr{T}, x)

Ideally this fallback doesn't exist, but that probably is too breaking of a change.

adienes adienes commented on Sep 27, 2026 21:49 UTC
Contributor

maybe a silly question --- but I mean it genuinely --- if it's this "easy" to fix, why hasn't it been done already? I feel like this pointer fallback bug has been noticed and discussed several times over

nhz2 nhz2 commented on Sep 27, 2026 22:52 UTC
Author

Most uses of pointer have been replaced with cconvert and unsafe_convert, but needing to specify the pointer type and use two functions is more cumbersome, so pointer continues to be used in some cases.

Also fixing the underlying issue requires updating packages (see #62895) because really unsafe_convert should only be called on the output of cconvert, as mentioned in #51962

As an example https://github.com/JuliaLinearAlgebra/BlockBandedMatrices.jl is currently broken on 1.14 (and this simple PR doesn't fix that pointer conversion problem)

nhz2 nhz2 commented on Sep 28, 2026 18:00 UTC
Author
nhz2 nhz2 commented on Sep 28, 2026 18:39 UTC
Author

Before this is merged all use of the pointer function in Base and the standard libraries should be replaced by calls to cconvert and unsafe_convert.

stevengj stevengj commented on Sep 30, 2026 04:23 UTC
Contributor

Could the fallback instead be unsafe_convert(Ptr{T}, x), i.e. it would succeed only in cases where cconvert is the identity and unsafe_convert is defined?

jakobnissen jakobnissen commented on Sep 30, 2026 10:43 UTC
Contributor

We could, but then what is the point of unsafe_convert? Won't it just be pointer under a different name?

nhz2 nhz2 commented on Sep 30, 2026 13:19 UTC
Author

The main difference is that with pointer the array chooses the pointer type, whereas with unsafe_convert the caller chooses the pointer type. The big question here is should pointer on an array wrapper call pointer of the parent, or is pointer never supposed to be overloaded, so unsafe_convert ends up being called on the parent.

stevengj stevengj commented on Sep 30, 2026 23:23 UTC
Contributor

The advantage of an unsafe_convert fallback is that a typical type needs to implement fewer methods, and the change is more likely to be backwards compatible (since types didn't need to implement both pointer and unsafe_convert in the past).

(A type that implements a nontrivial cconvert will still need a separate pointer method, though.)

stevengj stevengj commented on code on Oct 01, 2026 06:08 UTC
Code Review
base/strings/basic.jl:1
@@ -802,6 +802,7 @@ write(io::IO, s::CodeUnits) = write(io, s.s)
 
 cconvert(::Type{Ptr{T}},    s::CodeUnits{T}) where {T} = cconvert(Ptr{T}, s.s)
 cconvert(::Type{Ptr{Int8}}, s::CodeUnits{UInt8}) = cconvert(Ptr{Int8}, s.s)
+unsafe_convert(::Type{Ptr{T}}, s::CodeUnits{T}) where {T} = unsafe_convert(Ptr{T}, s.s)

Do we still need the cconvert methods too?

stevengj stevengj commented on code on Oct 01, 2026 06:09 UTC
Code Review
base/permuteddimsarray.jl:1
@@ -53,6 +53,7 @@ Base.similar(A::PermutedDimsArray, T::Type, dims::Base.Dims) = similar(parent(A)
 Base.dataids(A::PermutedDimsArray) = Base.dataids(parent(A))
 Base.unaliascopy(A::PermutedDimsArray) = typeof(A)(Base.unaliascopy(parent(A)))
 Base.cconvert(::Type{Ptr{T}}, A::PermutedDimsArray{T}) where {T} = Base.cconvert(Ptr{T}, parent(A))
+Base.unsafe_convert(::Type{Ptr{T}}, A::PermutedDimsArray{T}) where {T} = Base.unsafe_convert(Ptr{T}, parent(A))

Again, it seems like we can get rid of the cconvert method?

stevengj stevengj commented on Oct 01, 2026 06:16 UTC
Contributor

It seems like a lot of types are defining cconvert where they were supposed to define unsafe_convert, since in my understanding the latter is what is supposed to be used for pointer conversions: https://github.com/JuliaLang/julia/blob/b3032e9d11177204bea506abd3bf69e921c4851c/base/essentials.jl#L741

nhz2 nhz2 commented on Oct 01, 2026 18:15 UTC
Author

Some array types only define cconvert and not unsafe_convert. Removing the cconvert method overloads from, for example, PermutedDimsArray would cause issues if the parent is a cconvert only array.

In general only cconvert needs to be defined since it can return something that already has a unsafe_convert method.

stevengj stevengj commented on Oct 02, 2026 12:53 UTC
Contributor

I thought that the unsafe_convert + cconvert design is that the "unsafe" part — converting a rooted object to a raw pointer, was supposed to happen in unsafe_convert.

nhz2 nhz2 commented on Oct 02, 2026 14:00 UTC
Author

What is unsafe about cconvert(::Type{Ptr{T}}, s::CodeUnits{T}) where {T} = cconvert(Ptr{T}, s.s) ? And this may be required because s.s may have a cconvert method but not a unsafe_convert method. For example https://github.com/JuliaStrings/StaticStrings.jl/blob/3be63a7d1432680ec71b1e7f8404c842b15580c3/src/convert.jl#L72-L74

If this is true for CodeUnits, then the same logic applies to all of the array wrapping types here.

Reviewers
Assignees
nhz2 nhz2
Labels
risk: low confidence: 82.2% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 47.7% (MEDIUM) · Higher = larger blast radius
47.7%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 82.2% · Lower = rushed or shallow review
82.2%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 8.5% · Safe to merge
8.5%