Coder Social home page Coder Social logo

Comments (2)

bradenbest avatar bradenbest commented on August 23, 2024

Specifically, the adversarial code will make the program crash right here:
image

as reading from (void *)1 is illegal

free is expected to crash in a situation like this regardless. My point is that the check is unnecessary because the behavior is undefined. This creates a contract between the function and the caller that the caller must avoid doing that sort of thing. In other words, it is 100% valid and expected for free to assume the pointer is valid. Keep it simple. You're not implementing asan or valgrind here. And those tools use much more advanced techniques to detect and report memory errors. Not only do they lay their memory out in an unusual and inefficient way including things like "shadow bytes" and little traps, but they also literally read the binary to get the symbol table so they can tell you where you fucked up. If you're trying to catch memory errors, it is a much better idea to use these mature and battle-tested tools designed for it than to write your own allocator that refuses to end the program even when it's in an invalid state.

It would be one thing to write your own custom free that checks for some obvious error and then kills the program with a helpful error message. But for libc free, that case is already covered by UB.

from c-how-to.

bradenbest avatar bradenbest commented on August 23, 2024

I just realized that I did not provide advice for calloc. So for comparison, here's what my implementation of calloc looks like:
image

from c-how-to.

Related Issues (2)

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.