Skip to content

Bug: JS engine catastrophic backtracking when parse Go code like//.. //.. #1026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 of 5 tasks
zzuu666 opened this issue May 27, 2025 · 0 comments
Open
3 of 5 tasks

Comments

@zzuu666
Copy link
Contributor

zzuu666 commented May 27, 2025

Validations

Describe the bug

🐞 Bug Report

Describe the bug

When using shiki's JavaScript regex engine to parse Go code containing struct definitions with comments, the parser experiences catastrophic backtracking and becomes unresponsive (hangs indefinitely). This issue specifically occurs with Go struct definitions that have inline comments using //.

Example code that triggers the bug:

type GandalfServer struct { //nolint:bgolint // abcdef

The parser hangs when processing this code and never completes the syntax highlighting operation.

Reproduction

Minimal reproduction case:

import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript'
import { createHighlighter } from 'shiki'

const jsEngine = createJavaScriptRegexEngine()
const highlighter = await createHighlighter({
  themes: ['github-light'],
  langs: ['go'],
  engine: jsEngine,
})

// This will hang indefinitely
const result = highlighter.codeToHtml(
  'type GandalfServer struct { //nolint:bgolint // abcdef',
  { lang: 'go', theme: 'github-light' }
)

Image

Environment:

  • Shiki version: 3.4.2
  • Engine: JavaScript regex engine (@shikijs/engine-javascript)
  • Language: Go
  • Platform: Any (reproduced on macOS)

Additional test cases that also hang:

type Server struct { // simple comment
type User struct { //multiple //comments

Root Cause Analysis

The issue is located in the Go language definition file (packages/langs/dist/go.mjs) within the struct_variables_types_fields patterns. Specifically, the regex pattern contains \\S+ which greedily matches all non-whitespace characters, including comment symbols //.

When encountering struct definitions with comments, the \\S+ pattern attempts to match the comment symbols, causing massive backtracking operations and resulting in catastrophic backtracking.

Problematic patterns:

  • repository.struct_variables_types_fields.patterns[3]
  • Contains \\S+ that matches comment symbols

Expected Behavior

The Go syntax highlighter should process struct definitions with comments quickly (within milliseconds) without hanging.

Actual Behavior

The parser hangs indefinitely (>10 seconds) when encountering Go struct definitions with inline comments.

Additional Information

I have already implemented and tested a fix for this issue. The fix involves modifying 2 regex patterns in the Go language definition file, replacing \\S+ with [^\\s/]+. After applying the fix:

  • Original problematic code: 59.00ms (vs. hanging before)
  • Simple comments: 11.05ms
  • Complex structs: <1ms

Reproduction

https://textmate-grammars-themes.netlify.app/?theme=vitesse-dark&grammar=go&code=type+GandalfServer+struct+%7B+%2F%2Fnolint%3Abgolint+%2F%2F+abcde

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant