In-depth lesson · Memory corruption
Typical fixes
Every point the Memory corruption reports back under this heading, best-backed first, each with the reports it is seen in.
-
Validate the length of supplied data against the destination buffer size before copying.
Seen in 113 reports
-
Apple fixed most of these with improved bounds checking, improved input validation or improved memory management.
Seen in 82 reports
-
Validate that an object still exists before dereferencing or operating on it, and close race windows between freeing and reuse.
Seen in 76 reports
-
Add bounds and length checks before copying input into fixed-size buffers.
Seen in 68 reports
-
Add correct length and bounds checks before writing to heap or stack buffers.
Seen in 68 reports
-
Validate input length and size before any copy into a heap or stack buffer, and reject values that exceed the destination.
Seen in 67 reports
-
Validate the length of user-supplied data before copying it into a fixed stack buffer.
Seen in 65 reports
-
Validate declared lengths against the destination buffer size before reading or writing.
Seen in 57 reports
-
Validate lengths and sizes against the destination buffer before copying data into it.
Seen in 57 reports
-
Add bounds checks before reading or writing file-derived data.
Seen in 56 reports
-
Validate length, size and offset fields against the destination buffer before copying or reading.
Seen in 56 reports
-
Check that an object still exists before operating on it.
Seen in 52 reports
-
Use reference counting and safe teardown so objects cannot be freed while another thread still holds a reference, and guard against double free.
Seen in 50 reports
-
Validate the length of network and file input before copying it into fixed-size or heap buffers.
Seen in 45 reports
-
Check that an object still exists before operating on it, and clear pointers after freeing.
Seen in 40 reports
-
Check that an object still exists before using or freeing it, and clear pointers after freeing.
Seen in 40 reports
-
Check that an object still exists before operating on it, and clear references when freeing.
Seen in 37 reports
-
Validate the length of user-supplied data before copying it into fixed-size buffers.
Seen in 36 reports
-
Fix object lifetime handling so memory is not accessed after being freed, including under race conditions.
Seen in 33 reports
-
Fix object lifetime handling so memory is not freed twice or used after release, including under races.
Seen in 33 reports
-
Add bounds checks before reading or writing user-supplied data.
Seen in 32 reports
-
Use reference counting or deferred reclamation so objects cannot be freed while still reachable.
Seen in 32 reports
-
Fix object lifetime handling so memory is not freed while still referenced, and close the timing window that a race condition needs.
Seen in 31 reports
-
Use weak pointers or synchronisation barriers so asynchronous callbacks and cancellation paths cannot use an object after it is freed.
Seen in 31 reports
-
Check the size and structure of input data against the destination buffer before reading or writing.
Seen in 30 reports
-
Fix reference counting and lifetime handling so freed objects are not reused, and close race windows.
Seen in 30 reports
-
Validate every size, length and offset field against the destination buffer before copying or writing.
Seen in 30 reports
-
Validate that user-supplied values point to valid, expected memory before dereferencing them.
Seen in 25 reports
-
Fix object lifetime handling so freed memory is not referenced again and is not freed twice.
Seen in 24 reports
-
Manage object lifetime so freed memory is not referenced again, including under concurrency.
Seen in 23 reports
-
Validate all size, length and offset fields against the destination buffer before reading or writing.
Seen in 23 reports
-
Validate the size and structure of user-supplied data before reading or writing it.
Seen in 22 reports
-
Validate every size and length against the destination buffer before copying, and check read lengths against allocations.
Seen in 21 reports
-
Add bounds checks before reading from a buffer parsed from untrusted input.
Seen in 20 reports
-
Check that an object still exists before using or freeing it again.
Seen in 20 reports
-
Check user-supplied counts and sizes for overflow before using them in allocation size calculations.
Seen in 20 reports
-
Add bounds checks before writing document-controlled sizes into heap and stack buffers.
Seen in 19 reports
-
Check that an object still exists before using it, and clear or invalidate pointers when objects are freed.
Seen in 19 reports
-
Check the length of input before copying it into a fixed-size buffer.
Seen in 19 reports
-
Check that an object still exists before operating on it, and clear stale pointers after freeing.
Seen in 18 reports
-
Replace debug-only assertions that guard security-relevant bounds or type assumptions with checks that remain in release builds.
Seen in 18 reports
-
Validate every length, size, count and offset read from a file against the destination buffer before copying or indexing.
Seen in 18 reports
-
Add bounds checks before reading data so code cannot read past the end of a buffer.
Seen in 17 reports
-
Fix object lifetime handling so freed memory is not referenced, and make the relevant operations atomic to close race windows.
Seen in 17 reports
-
Check arithmetic on user-supplied sizes for overflow, underflow and truncation before allocating or copying.
Seen in 16 reports
-
Check input length against the destination buffer size before copying into heap or stack buffers.
Seen in 16 reports
-
Check the length of user-supplied data before copying it into fixed-size stack or heap buffers.
Seen in 16 reports
-
Validate every count, offset and size field against the record size before using it to index or copy.
Seen in 16 reports
-
Validate the length and bounds of user-supplied data before copying or indexing it.
Seen in 15 reports
-
Validate the length of user-supplied data against the destination buffer before every copy, and check size calculations for overflow.
Seen in 15 reports