Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ See the [releases page](https://github.com/github/codeql-action/releases) for th

## [UNRELEASED]

No user facing changes.
- Fixed a bug where two diagnostics produced within the same millisecond could overwrite each other on disk, causing one of them to be lost. [#3852](https://github.com/github/codeql-action/pull/3852)

## 4.35.2 - 15 Apr 2026

Expand Down
9 changes: 7 additions & 2 deletions lib/analyze-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions lib/init-action-post.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions lib/init-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions lib/setup-codeql-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions lib/upload-lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions lib/upload-sarif-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ let unwrittenDiagnostics: UnwrittenDiagnostic[] = [];
*/
let unwrittenDefaultLanguageDiagnostics: DiagnosticMessage[] = [];

/**
* Counter used to generate a unique suffix for each diagnostic filename, so that
* two diagnostics produced within the same millisecond do not overwrite each
* other on disk.
*/
let diagnosticCounter = 0;

/**
* Constructs a new diagnostic message with the specified id and name, as well as optional additional data.
*
Expand Down Expand Up @@ -167,10 +174,18 @@ function writeDiagnostic(
// Create the directory if it doesn't exist yet.
mkdirSync(diagnosticsPath, { recursive: true });

// Include a monotonically increasing suffix to avoid filename collisions
// between diagnostics produced within the same millisecond.
const uniqueSuffix = (diagnosticCounter++).toString();
// We should only need to remove colons, but to be defensive, only allow a restricted set of
// characters.
const sanitizedTimestamp = diagnostic.timestamp.replace(
/[^a-zA-Z0-9.-]/g,
"",
);
const jsonPath = path.resolve(
diagnosticsPath,
// Remove colons from the timestamp as these are not allowed in Windows filenames.
`codeql-action-${diagnostic.timestamp.replaceAll(":", "")}.json`,
`codeql-action-${sanitizedTimestamp}-${uniqueSuffix}.json`,
);

writeFileSync(jsonPath, JSON.stringify(diagnostic));
Comment on lines 186 to 191
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with the random suffix, writeFileSync will still overwrite an existing diagnostic file if a (rare) collision occurs (same timestamp + same random suffix). To fully prevent losing diagnostics, consider writing with an exclusive flag (e.g. flag: "wx") and retrying with a new suffix on EEXIST (bounded retries) so we never overwrite a previous diagnostic.

See below for a potential fix:

    // We should only need to remove colons, but to be defensive, only allow a restricted set of
    // characters.
    const sanitizedTimestamp = diagnostic.timestamp.replace(
      /[^a-zA-Z0-9.-]/g,
      "",
    );

    const maxWriteAttempts = 10;
    let lastError: unknown;
    for (let attempt = 0; attempt < maxWriteAttempts; attempt++) {
      // Include a random suffix to avoid filename collisions between diagnostics
      // produced within the same millisecond. This doesn't need to be
      // cryptographically secure, so `Math.random` is fine.
      const uniqueSuffix = Math.floor(Math.random() * 0x100000000)
        .toString(16)
        .padStart(8, "0");
      const jsonPath = path.resolve(
        diagnosticsPath,
        `codeql-action-${sanitizedTimestamp}-${uniqueSuffix}.json`,
      );

      try {
        writeFileSync(jsonPath, JSON.stringify(diagnostic), { flag: "wx" });
        lastError = undefined;
        break;
      } catch (err) {
        if ((err as NodeJS.ErrnoException).code === "EEXIST") {
          lastError = err;
          continue;
        }
        throw err;
      }
    }

    if (lastError !== undefined) {
      throw lastError;
    }

Copilot uses AI. Check for mistakes.
Expand Down
Loading