Skip to content

prevent stack overflows from faulty user-defined constructor methods#59506

Merged
topolarity merged 4 commits intoJuliaLang:masterfrom
nsajko:faulty_constructor_method_should_not_cause_stack_overflows
Sep 17, 2025
Merged

prevent stack overflows from faulty user-defined constructor methods#59506
topolarity merged 4 commits intoJuliaLang:masterfrom
nsajko:faulty_constructor_method_should_not_cause_stack_overflows

Conversation

@nsajko
Copy link
Copy Markdown
Member

@nsajko nsajko commented Sep 7, 2025

Base code often assumes that a constructor will return a value of the stated type. This is false, cross-reference issue #42372.

This change prevents several stack overflows that arise when a generic method is called with a newly-defined type with such a weird constructor for some Base-defined type.

`Base` code often assumes that a constructor will return a value of the
stated type. This is false, cross-reference issue JuliaLang#42372.

This change prevents several stack overflows that arise when a generic
method is called with a newly-defined type with such a weird
constructor for some `Base`-defined type.
@nsajko nsajko added the bugfix This change fixes an existing bug label Sep 7, 2025
@nsajko
Copy link
Copy Markdown
Member Author

nsajko commented Sep 7, 2025

The trimming CI failure is issue #59507.

@nsajko nsajko marked this pull request as ready for review September 7, 2025 19:10
Copy link
Copy Markdown
Member

@topolarity topolarity left a comment

Choose a reason for hiding this comment

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

Good find. Indeed, these implementations assume that these will reach a base case.

@topolarity topolarity merged commit f0ece4a into JuliaLang:master Sep 17, 2025
7 checks passed
@nsajko nsajko deleted the faulty_constructor_method_should_not_cause_stack_overflows branch September 17, 2025 22:52
xal-0 pushed a commit to xal-0/julia that referenced this pull request Sep 30, 2025
…uliaLang#59506)

`Base` code often assumes that a constructor will return a value of the
stated type. This is false, cross-reference issue JuliaLang#42372.

This change prevents several stack overflows that arise when a generic
method is called with a newly-defined type with such a weird constructor
for some `Base`-defined type.
nsajko added a commit to nsajko/julia that referenced this pull request Jan 29, 2026
Due to issue JuliaLang#42372, it can not be assumed that a constructor call will
return a value of the requested type. Enforce that assumption in code
where it matters.

Example stack overflows that this change prevents:

```julia
struct FaultyInt <: Integer end        # new `Int`-like
function Base.Int(x::FaultyInt) x end  # invalid constructor

iterate("", FaultyInt())  # overflows the stack
isvalid("", FaultyInt())  # overflows the stack

struct MyString <: AbstractString  # valid new subtype of `AbstractString`
    str::String
end

isvalid(MyString(""), FaultyInt())   # overflows the stack
codeunit(MyString(""), FaultyInt())  # overflows the stack
```

Follow up on PR JuliaLang#59506.
adienes pushed a commit that referenced this pull request Jan 30, 2026
#60876)

Due to issue #42372, it can not be assumed that a constructor call will
return a value of the requested type. Enforce that assumption in code
where it matters.

Example stack overflows that this change prevents:

```julia
struct FaultyInt <: Integer end        # new `Int`-like
function Base.Int(x::FaultyInt) x end  # invalid constructor

iterate("", FaultyInt())  # overflows the stack
isvalid("", FaultyInt())  # overflows the stack

struct MyString <: AbstractString  # valid new subtype of `AbstractString`
    str::String
end

isvalid(MyString(""), FaultyInt())   # overflows the stack
codeunit(MyString(""), FaultyInt())  # overflows the stack
```

Follow up on PR #59506.
@nsajko nsajko added the error handling Handling of exceptions by Julia or the user label Jan 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix This change fixes an existing bug error handling Handling of exceptions by Julia or the user

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants