I am looking for a coding exercise that could be assigned to students and will give them practice with writing memory-safe code. In particular, I am looking for some exercise that students would have to implement in C, and where if they aren't cautious, it would be easy to introduce a memory-safety vulnerability -- e.g., an array out-of-bounds bug, or a buffer overrun vulnerability. Can anyone suggest a programming exercise that might be suitable?

I am hoping that having the experience of writing code that turns out to contain such a vulnerability might motivate interest in tools and techniques to assist with ensuring code is memory-safe.

To give an analogy: one classic exercise is to implement binary search in a sorted array. If you're not careful, it's easy to introduce an off-by-one bug into your code, so this is a good exercise to motivate loop invariants as a technique for writing correct code. Is there a coding exercise like this, aimed not at off-by-one bugs and loop variants, but at memory safety or buffer overruns or array bounds errors?

  • 1
    $\begingroup$ Just about any C program that requires heap memory seems like it'd fit the bill. I can't think of too many nontrivial C programs I've written that didn't involve running into memory safety problems at some point. Beyond that, does the project need to be greenfield? You could have them analyze and fix bugs in a small codebase with existing memory safety problems. $\endgroup$
    – ggorlen
    Nov 27, 2023 at 20:26
  • $\begingroup$ @ggorlen, Thanks for your comment, but that's not what I'm looking for, and does not match my experience. The chances of having a memory safety error are significantly higher for some coding tasks, and lower for others. I want a coding exercise where the chances are as high as possible. (For many small-sized coding tasks that are not chosen with this purpose in mind, if they program in a vaguely reasonable way, there is a significant chance they will have no memory safety problems.)Yes, I really do want a project where they write their own code, not analyze/fix bugs in an existing codebase. $\endgroup$
    – D.W.
    Nov 28, 2023 at 4:51
  • $\begingroup$ Sounds like you're a much better C programmer than I am, then. $\endgroup$
    – ggorlen
    Nov 28, 2023 at 4:54
  • $\begingroup$ @ggorlen, Well, no, that's not my basis for asking. I ask for faith that I have good reason for asking. I have experience with one attempt at such an exercise; the prevalence of memory safety errors in student solutions was not what I was hoping for. So I'm asking here hoping that others have a better solution. $\endgroup$
    – D.W.
    Nov 28, 2023 at 5:11

1 Answer 1


Exercises for array bounds

String manipulation code very often runs into array bounds.

For simple exercises, arrange two-part problems where the first part allocates memory for a string and the second part modifies that string in place. This forces students to distinguish between the data size and the allocated size. For example: read a line from a file by growing a buffer until you find a newline; then perform some transformation that might expand the line (e.g. a substring replacement or a format conversion).

For some more complex exercises, do some parsing of binary protocols with length-value encodings. This is a very common source of buffer overflows in real-world code. For example, one that allowed exploiting computers provided that they were powered off! (A bug in a Wake-on-LAN implementation in network card firmware.) This forces students to distinguish between the purported size (encoded in the string) and the actual size (bounded by the size of the complete input).

Exercises for memory management

In high-quality C code bases, buffer overflows tend to be rare, because it's not that hard to be disciplined about them. Memory management bugs are harder. Manual memory management is hard, and often the reason you need it is that you can't be very disciplined about it. So you should train your students to watch out for use-after-free and double-free, as well as memory leaks.

A very common case for a memory management bug is a program that needs to allocate two resources (such as two malloc'ed objects), and where a recoverable error can happen either after obtaining the first resource or after obtaining the second resource.

  • If the cleanup code tries to access the a resource whose allocation failed, you may have an access to uninitialized memory (possibly leading to dereferencing an arbitrary pointer object). In more disciplined code, where all pointers are initialized to NULL, you have a null pointer dereference.
  • If the cleanup code tries to access one of the resources after it has deallocated it, you may have a use-after-free.
  • If the cleanup code ends up freeing a resource twice (e.g. due to partial cleanup on a specific error path followed by a generic cleanup), you may have a double free.
  • If some cleanup code is missing, you may have a memory leak.

Here are a few examples of long-lived programs that allocate multiple resources for a single task:

  • A server allocates a send buffer and a receive buffer.
  • A data processing server, or a data processing interactive tool, that allocates multiple buffers to process each request.

Memory management errors also arise in code that manipulates complex data structures (trees, linked lists, hash tables, graphs, …). But that tends to make for more advanced exercises because the parts about the data structure itself are usually complex.


Please teach your students tooling that can help them. As a practitioner, I find ASan (Address Sanitizer) very useful. It will find most out-of-bounds accesses, use-after-free/double-free and memory leaks as long as there is a test case that reaches them.

  • $\begingroup$ Perhaps OP should use ASan or similar tools to check the memory-safety of their students' code?  (Memory errors, like concurrency errors, are notoriously difficult to trigger when testing — they tend to show themselves only ‘in the wild’ where they can do much more damage!) $\endgroup$
    – gidds
    Dec 24, 2023 at 22:24
  • $\begingroup$ @gidds Yes, but OP should also teach their students to do it. $\endgroup$ Dec 26, 2023 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.