Ctrl+S said "Saved." The file was 0 bytes. This is a submis | Coderz Club

Ctrl+S said "Saved." The file was 0 bytes. This is a submission for DEV's Summer Bug Smash: Smash Stories powered by Sentry. Written with the help of AI (Claude). The bug, the fix, the validation set

Ctrl+S said "Saved." The file was 0 bytes. This is a submission for DEV's Summer Bug Smash: Smash Stories powered by Sentry. Written with the help of AI (Claude). The bug, the fix, the validation set

By Coderz Club · 2026-07-26 · Tags: ai, git, go

Ctrl+S said "Saved." The file was 0 bytes.

This is a submission for DEV's Summer Bug Smash: Smash Stories powered by Sentry. Written with the help of AI (Claude). The bug, the fix, the validation setup, and every claim below are mine, and were verified against the real codebase and a real full disk. The report Someone lost a Magic: The Gathering decklist. They were playing on Cockatrice — the open-source MTG client — with their decks on a drive that had quietly filled up while Oracle pushed an update in the background. They added a card, hit Ctrl+S, and Cockatrice said it saved. The debug log agreed: [2026-05-28 22:31:42.031 I] Saved deck to "G:/cockatrice300/data/decks/edh-b2-gitrog-reanimate.cod" with format 1 - true - true. Success. The file was 0 bytes. The deck was gone. That was issue #6952, filed by Mekkiss. The steps to reproduce are four lines long and completely damning: Have a full disk. Open a deck on the full disk Add one card to it Save the deck (ctrl+s) Observe that the deck is now a 0 byte file. Three ways to be wrong at once The save path lived in DeckLoader::saveToFile(). Stripped down, it looked like this: QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qCWarning(DeckLoaderLog) << "Could not create or open file:" << fileName; return std::nullopt; } bool success = false; switch (fmt) { /* ... saveToFile_Native / saveToFile_Plain ... */ } file.flush(); file.close(); qCInfo(DeckLoaderLog) << "Saved deck to " << fileName << "with format" << fmt << "-" << success; There are three independent failures stacked on top of each other here, and you need all three to lose data: 1. WriteOnly truncates on open. The instant open() succeeds, the existing deck is 0 bytes. Not after a successful write — at open time. The old deck is already destroyed before a single byte of the new one is written. On a full disk, open() still succeeds: truncating a file doesn't need free space. It frees space. 2. The serializers always returned true. saveToFile_Native() and saveToFile_Plain() write into the QTextStream / QIODevice and return true unconditionally. They never asked whether the bytes landed. 3. The return value of flush() was discarded. This is the last place the truncation could still have been caught, and the result went straight into the void. close() after it can't help either — its failure was also ignored. So: file truncated, writes silently fail because there's no room, nobody checks, and the log cheerfully prints - true. The user is told their deck is safe at the exact moment it stops existing. It was worse than the report While tracing the save path I checked the other places DeckLoader writes deck files. There were two more, and both used the same truncate-then-write-then-hope pattern. updateLastLoadedTimestamp() rewrites a deck to stamp it with a "last loaded" time — it runs on load, not save. Same QFile(fileName) opened WriteOnly, same always-true serializer. On a full disk, merely opening a deck truncated it to 0 bytes and reported success. You could lose a decklist without ever pressing Ctrl+S. convertToCockatriceFormat() was the ugly one. It opened the destination .cod file WriteOnly, wrote the deck, and then — if result was true, which it always was — deleted the original file: file.close(); if (result) { if (!QFile::remove(fileName)) { /* warn */ } A failed write there doesn't leave you with a 0-byte file and a backup. It leaves you with a 0-byte file and no original. And because the function opened the destination before checking whether the source format was even convertible, the early-return branches ran with the file already truncated. One user-visible bug on a full disk, and two more write paths queued up behind it waiting for the same conditions. The fix Qt has exactly the right tool for this and it has been sitting in QtCore since 5.1: QSaveFile. It writes to a temporary file next to the target and only replaces the target — atomically — when you call commit() and every byte has actually made it to disk. If anything fails, the original file is never touched. The change is mostly deletion: // Use QSaveFile so that a failed write (e.g. a full disk) leaves the existing deck untouched // instead of truncating it to a 0-byte file. The target is only replaced once every byte has // been flushed successfully in commit(). QSaveFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qCWarning(DeckLoaderLog) << "Could not create or open file:" << fileName; return std::nullopt; } bool success = false; switch (fmt) { /* ... */ } if (!success) { file.cancelWriting(); qCWarning(DeckLoaderLog) << "Failed to serialize deck for file:" << fileName; return std::nullopt; } if (!file.commit()) { qCWarning(DeckLoaderLog) << "Failed to save deck to " << fileName << ":" << file.errorString(); return std::nullopt; } qCInfo(DeckLoaderLog) << "Saved deck to " << fileName << "with format" << fmt; Note what moved: the success log now happens after commit() re

View this page on Coderz Club