Comments (2)
Specifically, the adversarial code will make the program crash right here:
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.
I just realized that I did not provide advice for calloc
. So for comparison, here's what my implementation of calloc looks like:
from c-how-to.
Related Issues (2)
- 能添加更多实例吗? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c-how-to.