Giter VIP home page Giter VIP logo

Comments (2)

straight-shoota avatar straight-shoota commented on September 24, 2024 1

Yes, problem lies in the assignment x = x.to_slice. StaticArray#to_slice takes a pointer of itself. So x.to_slice points to the location of x on the stack. By assigning the resulting slice to x again, you're overriding that memory with the instance of Slice which now points to itself.

Reduced example:

ary = StaticArray[1, 2, 3, 4]
slice = ary.to_slice

slice # => Slice[1, 2, 3, 4]

# now overwrite the memory location of `ary` with a new value
ary = StaticArray[5, 6, 7, 8]

slice # => Slice[5, 6, 7, 8]

This is obviously a foot gun.
StaticArray#to_slice is unsafe because it takes a stack pointer. It should be renamed to #to_unsafe_slice. This has been discussed previously in #9606 and there's already a patch in #11031. I'll extract the part for StaticArray#to_unsafe_slice from that PR which seems to be uncontested.

from crystal.

BlobCodes avatar BlobCodes commented on September 24, 2024

Another relevant example could be HTTP::WebSocket::Protocol#close, which does this with message:

def close(code : CloseCode? = nil, message = nil) : Nil
return if @io.closed?
if message
message = message.to_slice

The public method has no type restriction on message, so someone could footgun themselves:

def close(code : CloseCode | Int? = nil, message = nil) : Nil
return if closed?
@closed = true
@ws.close(code, message)
end

from crystal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.