Skip to content
Back

Simon Tatham shows how to simulate coroutines in standard C with switch/case and __LINE__

In a post, Simon Tatham, creator of PuTTY, describes a portable, macro-based technique for simulating coroutines in standard C without threads or assembly. He states that PuTTY still uses versions of this trick in its SSH protocol code.

By Wendelmaques Pereira · Updated today

Source: Truque de corrotinas de Simon Tatham em C padrão (x.com). Text prepared with AI from this source.

What happened and what to do

The post by Simon Tatham, creator of PuTTY, explains how to simulate coroutines in standard C by combining the switch/case technique behind Duff's Device with the __LINE__ macro. This allows functions to be paused and resumed without threads or assembly. According to the author, PuTTY still uses versions of this trick in its SSH protocol code.

For companies with legacy C codebases, in protocol parsers, drivers or firmware, the case suggests reviewing where hand-written state machines already act as coroutines. A practical step is to map these sections, assess whether the resume logic can be verified, and cover them with unit tests, fuzzing and static analysis, reducing the risk of inconsistent states on malformed input. When refactoring is justified, the state machine can be isolated behind a stable interface with regression tests and coverage metrics. This is software engineering and security work, and does not depend on AI.

How the consultancy can help

Diagnosis of legacy C code that uses state machines or simulated coroutines, mapping resume points and parsing risks. Then a scoped proposal for testing with fuzzing, static analysis and isolated refactoring, with support for operating the tests in the client's environment.

Next step

If your team maintains C code with protocol parsers, drivers or firmware, send a short description of the system, the language and your main concern. You will then receive a scoped diagnosis and implementation proposal.

Let's assess your project

Wendelmaques Pereira

I reply with scope and a proposal.

WhatsApp

Looking for something else?Frequently asked questionsGuidesContact