Skip to content
All posts
ai debugging agentic c architecture

The 15 KB Trap: How a 1.6 GB File and an AI Agent Saved Us from a Costly Rewrite

How human instinct and an AI agent resolved a segfault in 15 minutes — and sidestepped a costly Java integration.

My colleague had been stuck for days, staring down a segmentation fault in an open-source C library. We were building an optical ground station simulator and needed to test CFDP (the CCSDS File Delivery Protocol). Since our client’s implementation was delayed, we grabbed an open-source alternative to keep moving.

The library mostly worked, but my colleague had already spent significant time patching it — like realizing the original author used strncpy instead of memcpy for PDU copying, which corrupted payloads at the first null byte. But this new issue was a wall: whenever we sent a larger file, the program crashed with a segfault.

He was exhausted and ready to throw in the towel, proposing we switch to a different, Java-based library. For our Go-based microservices architecture, integrating a Java component would have been a costly, complex nightmare that the client was already trying to push on us. I offered to help before we committed to that rewrite.

I didn’t plan on spending hours manually tracing pointers. Instead, I fired up Claude Code.

Claude mapped the codebase remarkably well. It documented the flow and confidently presented three plausible causes for the segfault:

  1. Mixing 32-bit and 64-bit types around file sizing and pointers.
  2. Using fseek for writing on files opened in append mode.
  3. Bizarre chunk sizing logic causing rapid file open/close cycles during checksum calculations.

They all sounded like classic C footguns, but my intuition said otherwise. Since our files were well under 4 GB, the 32/64-bit mismatch shouldn’t have triggered a segfault yet. My gut pointed to a buffer or stack overflow.

Instead of blind patching, I directed Claude to simply reproduce the crash. I asked it to generate a massive 1.6 GB binary file and run the existing test suite through lldb without changing a single line of the library’s code.

Claude generated the test, ran the debugger, and within seconds, we hit the segfault. The initial dump was noisy, but after two or three iterative runs, we narrowed it down.

The culprit? The library’s state machine was implemented using a tail-recursive function. With enough state transitions — meaning, a sufficiently large file — the recursion simply devoured the stack.

The trap was in the test data. The original author’s repository included a test file they considered “large.” It was 15 KB. My test file was 1.6 GB. The crash consistently triggered at around the 150 MB mark.

Once we found it, Claude didn’t just point out the problem; it rewrote the recursive function into an iterative loop. It worked perfectly.

Start to finish, the entire debugging session took about 15 minutes. Most of my manual labor consisted of forking the repo on GitHub, locating the test file, and reviewing the final patch. The agent did the heavy lifting of code analysis, test generation, and lldb execution. Ironically, we spent more time discussing the fix in our daily standup than it took to actually solve it.

The takeaway here isn’t just that “AI writes code.” It’s proof that AI agents combined with seasoned human instinct and the right test data can achieve incredible velocity. I didn’t need to be a human compiler, but I did need to know how to direct the agent — telling it not to fix things, but to reproduce the failure state first.

More importantly, that 15-minute session saved us weeks of architectural friction, successfully sidestepping a painful and costly Java integration.