← All posts

Two digits, 147 days: the COBOL migration bug that passed every test

Posted on December 1st, 2026 by Andrei Văcaru
Last updated on December 1st, 2026
Posted in Verification

cobol twitter.png

The New York Times ran its home-delivery system on an IBM mainframe from 1979 until the day it was switched over to Java. The migration automatically translated more than two million lines of COBOL. On the first day in production, a subscriber called to complain about 147 days of missed deliveries. Working out the credit involved a date calculation, and the result was entered into a field that could hold at most 99 days. The Java system raised an exception and a critical batch job failed.

The team then fed the same input to the old mainframe. Nothing failed there, as the value was truncated. The calculation came out wrong, and the job finished without any errors.

The project leads describe the incident in their own paper. They also report that checking the new system against the old one took 70 to 80 percent of the project and was the main reason it shipped a year late. Even so, this bug made it into production.

Migrations like this one are about to get much more common. AI tools can now translate COBOL into modern languages far faster than the rule-based translator the Times used, and IBM and others already sell them for this purpose. Faster translation does not make the result any easier to check, though, and checking is where the Times project spent most of its time.

In this post, we reproduce that kind of bug in a 23-line COBOL program and port the program to Rust. We show why testing has such a hard time finding differences in the behavior of the two programs. Then, we do what testing cannot do: we prove, for every possible input, where the original and the port agree and where they do not. The tool behind the proof is a formal semantics of COBOL-85 that we wrote in the K Framework.

The mainframe was wrong too

The incident is usually told as the story of a Java port that crashed. However, the detail that's easy to miss is that the mainframe did not handle 147 days either. It dropped a digit and paid the credit on what was left. If the field held two digits, as the 99-day limit suggests, the subscriber was credited for 47 days. The paper does not give the number, only that the result was wrong.

Now consider what the migration had been asked to do. Its goal was functional equivalence: the same output as the old system for the same input. The translation vendor’s whitepaper promises “equivalent functionality (including transfer of existing bugs, if there are any in the old code)” (section 4.2). The seven-stage test process that took up most of the project had a single pass condition, which was agreement with the mainframe.

So everything in the project was set up to preserve this bug. A port that computed the correct credit would have failed the regression suite. The port that shipped matched the mainframe on every input anyone had tested, but diverged on the first input no one had.

This is better described as a missing specification than as a missing check. The credit calculation has a precondition: the outage is shorter than 100 days. Someone assumed it in 1979, and nobody ever stated it. It was recorded nowhere except in the width of a field, and COBOL does not complain when a value outgrows its field. For 35 years nothing drew attention to it, and the migration was forced to carry it over.

Reproducing it

We wrote a small program with the same flaw, using 1985 COBOL.

The paper does not show the original code, so this is a reproduction of the kind of bug, not of the Times program.

* CREDIT: subscriber credit for missed delivery days (1985 idiom).

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CREDIT.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-FIRST-DAY    PIC 9(3).
       01 WS-LAST-DAY     PIC 9(3).
       01 WS-DAYS-MISSED  PIC 99.
       01 WS-DAILY-RATE   PIC 9V99 VALUE 2.50.
       01 WS-CREDIT-DUE   PIC 9(5)V99.
       01 WS-CREDIT-OUT   PIC ZZ,ZZ9.99.
       PROCEDURE DIVISION.
           ACCEPT WS-FIRST-DAY
           ACCEPT WS-LAST-DAY
           COMPUTE WS-DAYS-MISSED = WS-LAST-DAY - WS-FIRST-DAY + 1
           COMPUTE WS-CREDIT-DUE = WS-DAYS-MISSED * WS-DAILY-RATE
           MOVE WS-CREDIT-DUE TO WS-CREDIT-OUT
           DISPLAY "DAYS MISSED: " WS-DAYS-MISSED
           DISPLAY "CREDIT DUE:  " WS-CREDIT-OUT
           STOP RUN.

The program reads the first and last missed day as day-of-year numbers. It computes how many days that covers, stores the count in WS-DAYS-MISSED, and multiplies it by a daily rate of 2.50.

The bug comes from two features of COBOL. First, a variable’s type is given by its picture clause, and PIC 99 means exactly two digits. Second, when a COMPUTE has no ON SIZE ERROR phrase, and the result does not fit, the program keeps the low-order digits and carries on without any signal. A span of 147 days is stored as 47, and the credit comes out as 117.50 when it should be 367.50.

Porting it the way a migration tool would

The whitepaper from the vendor behind the Times migration publishes the table it uses to translate types. PIC 9 through PIC 9(4) become a Java short. PIC 9(5) through PIC 9(9) become an int (section 2.4.1.4). Anything with a decimal point becomes the vendor’s own decimal class.

We wrote our port by hand, following that table, but in Rust. The short becomes an i16.

The credit is kept in integer cents, because Rust’s standard library has no decimal type and cents lose nothing here. We picked Rust because the last part of this post also needs a formal semantics for the target language, and Rust has one in K.

pub fn credit_naive(first: i16, last: i16) -> (i16, u32) {
    let days = last - first + 1; // COBOL: PIC 99, stored modulo 100
    (days, days as u32 * 250) // COBOL: PIC 9(5)V99, at 2.50 a day
}

Every statement of the COBOL program has a counterpart in the port. The picture clauses have none. A short is 16 bits wide, and the type table has nowhere to record that the original field held two digits.

One caveat we reached was that an i16 holds 147 without issue, so our port does not crash as the Times system did. The paper does not say which part of the Java system raised the exception. What we reproduce is the disagreement between the two programs, which is the part the COBOL standard and the compilers pin down.

Three implementations, one input

We ran the program three ways on the input 010 and 156, which is day 10 through day 156, a span of 147 days:

  • under GnuCOBOL, a production compiler;
  • under our K semantics of COBOL-85;
  • as the Rust port.
== K semantics (cobol85 run)
DAYS MISSED: 47
CREDIT DUE:     117.50

== GnuCOBOL (cobc -std=cobol85, pinned nixpkgs)
DAYS MISSED: 47
CREDIT DUE:     117.50

== Rust port (credit.rs)
DAYS MISSED: 147
CREDIT DUE:     367.50

The K semantics and GnuCOBOL print exactly the same two lines. The Rust port differs on both: it reports 147 days where the COBOL reports 47, and a credit of 367.50 where the COBOL pays 117.50.

On a control input of 21 days, all three print the same thing. That is what makes this kind of bug hard to find. The port is correct for every input below the limit and wrong for every input above it, and neither program mentions the limit anywhere in its code.

Why the tests could not see it

The obvious objection is that better testing would have caught this. So it is worth looking at what the best published testing of COBOL-to-Java translation does.

IBM Research validates its watsonx Code Assistant for Z in three steps. A symbolic executor analyzes the COBOL program and generates test inputs. Those inputs are run on a real mainframe and the outputs are recorded. The recordings become JUnit assertions against the translated Java. It is careful work, accurate down to the byte, and the authors say openly that it “cannot guarantee functional equivalence due to practical constraints like limited path and data space exploration”.

For our bug, the relevant limitation is how the inputs are chosen. Symbolic execution is used there to cover branches: for each path through the program, the solver produces one input that takes that path. Any other value in that input is whatever the solver happens to pick.

A store that truncates does not create a branch. Whether the span is 21 or 147, the same statements run in the same order, so both inputs are on the same path. That path gets one test input. It lands above 99 only by chance. And whatever the mainframe does with that input becomes the expected result of the test.

This is not a criticism of that work. Symbolic execution of COBOL has been in industrial use for over a decade: Fujitsu published an SMT-based test generator in 2012, and IBM Japan built a byte-level engine in 2015 for exactly this purpose, testing migrated code against the original. All of these tools use symbolic reasoning to decide which inputs to try. To determine the correct output, they run the program on a real system.

What an executable semantics changes

Our COBOL-85 semantics is a definition of the language written in K, following the behavior of IBM Enterprise COBOL. “Executable” means that K can run COBOL programs directly from the definition, without a compiler.

The semantics stores data as raw bytes, one buffer per record, because that is what COBOL programs rely on: REDEFINES gives the same bytes a second layout, and moving a group item copies its bytes. Numbers are decimals with an explicit scale, instead of floating point. Truncation occurs when a value is stored in a field.

To check the definition against the standard, we run the NIST COBOL-85 conformance suite. It currently passes 90 of the 95 programs in the suite’s Nucleus module, scored the same way GnuCOBOL scores its own run.

This gives us two things that testing against a mainframe does not.

The first is a second, independent reference for what a COBOL program should do. If the semantics and GnuCOBOL agree with each other and disagree with the port, the port has diverged from the original. If the semantics and GnuCOBOL disagree, one of them deviates from the standard, and that is worth knowing, too. With a single reference, you cannot tell these two situations apart.

The second is that we do not have to search for the limit. We can prove where it is. We gave the COMPUTE statement to the K prover and left the inputs symbolic: six unknown digits, three for each day number. Then we asked what goes into the two-digit field.

The answer came back in seconds. For every pair of day numbers where the first is no later than the last, the stored value is the span modulo 100. The port computes the span itself. So the two programs agree exactly when the span is below 100, and they disagree on every input from 100 up. We did not get this by sampling inputs. It is a theorem about all of them.

A second claim covers the same statement with an ON SIZE ERROR phrase added. It proves that the error condition is raised exactly when the span reaches 100. That is the check the original program never had. The proofs needed seven one-line lemmas about byte strings, of the kind any K semantics with byte-level storage ends up collecting. Running the three implementations on random inputs can find individual cases where they differ. The prover gives the complete set.

Two ports, one theorem

The last step is one the Times team had no tool for. We state the relationship between the original program and its replacement as a theorem that spans both languages, and have a machine check it. The port shown above is credit_naive.

It keeps the whole span, which is what a migration produces when the digit count is lost in translation. Next to it, we wrote credit_faithful, which manually restores the truncation: it takes the span modulo 100 before computing the credit, as the COBOL store does.

The tool for this is KEQ, our equivalence prover built on K, which we introduced in an earlier post, and it follows the cut-bisimulation method of Kasampalis et al. (ASPLOS 2021). In short, it runs each program symbolically under the semantics of its own language, here our COBOL semantics on one side and KMIR, the K semantics of Rust, on the other. Then it asks an SMT solver whether any input makes the two results differ.

We provide the relation that connects the two starting states. Ours says three things: the Rust arguments are the same numbers as the COBOL input digits, both are three-digit day numbers, and the first day is no later than the last. The three-digit bound is not optional: without it, KEQ rejects the naive port before comparing a single statement, because the port accepts day 1000 and the COBOL field cannot hold it.

Under that relation, the faithful port is equivalent to the COBOL program on every input. The check also shows that none of the Rust overflow checks can fail on those inputs.

The naive port is not equivalent, and the solver shows where:

❌ COBOL 7 ≢ RUST 15: counterexample (FIRST_DAY = ARG_INT1 = 0, LAST_DAY = ARG_INT2 = 100): […], returning 1, 250 vs 101, 25250
not equivalent

Days 0 through 100 span 101 days. The COBOL program pays for 1 day: 2.50. The port pays for all 101 days: 252.50.

If we add to the relation that the span is below 100, the same naive port becomes equivalent. So, below 100 days, the two programs cannot be distinguished, and from 100 days onward, they never agree. That statement has now been checked for every input, for which the Times had only a single phone call to go on.

The relation we ended up with is a result in its own right. Three-digit days, first no later than last, span below 100: that is the list of things the original program took for granted, written down for the first time.

Equivalent to what

The uncomfortable part of the Times story is that the new system failed on a case the old system had been getting wrong all along.

Neither way of porting such a program is obviously right. A perfectly faithful port keeps paying the wrong credit for every long outage, forever. A port that fixes the calculation changes a financial result nobody approved, and may break downstream logic that has adapted to the truncation over three and a half decades. Equivalence testing cannot even raise the question, because its pass condition treats the old system as the specification.

What a migration needs is the list of inputs on which the old and new systems disagree. Then a person can look at each entry and decide whether it is a preserved bug, a fixed bug, or a new regression. Producing that list means knowing what the source language does on every input, including the ones nobody thought to run. For COBOL, that knowledge has mostly lived within compilers and vendor frameworks, where no one outside can read or verify it.

There is also a lesson about the language itself. When a result does not fit, and there is no SIZE ERROR phrase, the 1985 standard leaves the stored value undefined. IBM Enterprise COBOL and GnuCOBOL both keep the low-order digits and continue. The only way to be told is to have written ON SIZE ERROR in advance, for a failure you do not expect to happen. COBOL keeps this class of bug quiet by design. A port to a language that complains about overflow did not introduce the bug. It only made it audible, and then the migration got the blame for the noise.

A formal semantics will not tell you whether 47 or 147 is the right answer. It will tell you, for every input, which of the two you are going to get. That is the question the Times spent most of a multi-year project trying to answer one test case at a time.

Porting legacy code is one of the tasks AI is speeding up the most: models can now translate large COBOL codebases far faster than any team could by hand. But translation was never the hard part. At the Times, most of the effort went into checking that the new system did what the old one did, and that check still missed this bug. As translation gets faster, checking becomes a larger share of the work, and getting it right matters more. To check a port properly, you need a precise definition of what the original program does on every input. A formal semantics of the source language gives you exactly that.

Our COBOL semantics is not public yet. We have been building formal semantics of programming languages in K for over a decade, and most of them, including KMIR for Rust and KEVM for the Ethereum Virtual Machine, are open source on GitHub. Everything shown above is real output from these tools.