Skip to content

prevent stack overflows from user-defined constructor methods: strings#60876

Merged
adienes merged 1 commit intoJuliaLang:masterfrom
nsajko:faulty_constructor_method_should_not_cause_stack_overflow
Jan 30, 2026
Merged

prevent stack overflows from user-defined constructor methods: strings#60876
adienes merged 1 commit intoJuliaLang:masterfrom
nsajko:faulty_constructor_method_should_not_cause_stack_overflow

Conversation

@nsajko
Copy link
Copy Markdown
Member

@nsajko nsajko commented Jan 29, 2026

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:

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.

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.
@nsajko nsajko added strings "Strings!" bugfix This change fixes an existing bug labels Jan 29, 2026
@nsajko nsajko marked this pull request as ready for review January 29, 2026 18:43
@nsajko nsajko added the merge me PR is reviewed. Merge when all tests are passing label Jan 29, 2026
@oscardssmith oscardssmith added the error handling Handling of exceptions by Julia or the user label Jan 30, 2026
nsajko added a commit to nsajko/julia that referenced this pull request Jan 30, 2026
@nsajko

This comment was marked as resolved.

@adienes adienes merged commit 08dedb8 into JuliaLang:master Jan 30, 2026
14 checks passed
@adienes adienes removed the merge me PR is reviewed. Merge when all tests are passing label Jan 30, 2026
@nsajko nsajko deleted the faulty_constructor_method_should_not_cause_stack_overflow branch January 31, 2026 02:59
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 strings "Strings!"

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants