Skip to content

Bugfix clip() can hang on OSC 8 hyperlinks - #252

Merged
jquast merged 3 commits into
masterfrom
jq/clip-hyperlink-hang
Sep 17, 2026
Merged

jquast merged 3 commits into
masterfrom
jq/clip-hyperlink-hang

Conversation

@jquast

@jquast jquast commented Sep 17, 2026 •

Copy link
Copy Markdown
Owner

wcwidth.clip() could loop forever, appending to its output and reaching OOM:

    clip('\x1b]8;;http://x\x07\x1b[5Ga\x1b]8;;\x07', 0, 4)

Strange enough, sequences are allowed within hyperlink text, and in this case cursor movement is used, even allowed. But it results in an otherwise empty hyperlink (move 5 columns, then letter 'a', but clip from 0-4). This put the code in a loop that this bugfix returns or breaks early for.

Also, module doctests are enabled and repaired

clip() could loop forever, appending to its output until the process was
killed by the OOM killer:

    clip('\x1b]8;;http://x\x07\x1b[5Ga\x1b]8;;\x07', 0, 4)

The painter reaches _process_hyperlink() with an OSC 8 unit whose inner
text is visible somewhere, so the unit is neither EMPTY nor OUTSIDE, but
whose visible text lands outside the requested columns once clipped.  The
clipped inner text is then the empty string of width zero, and
_write_cells() stored it as a cell.

That single zero-width cell caused two failures:

  - _reconstruct_painter() advances its walk by the cell's width, so it
    revisited the same column forever, appending the cell's text each
    time.  The walk now always advances at least one column.
  - Storing the cell also replaced whatever the column already held, so a
    hyperlink that clipped away silently erased a character underneath it.
    Painting nothing is now a no-op.

The input is unusual but well-formed.  The OSC 8 specification restricts
bytes to 32-126 only for the parameters and the URI; it places no such
restriction on the text between the opening and closing sequences, and
describes a hyperlink as "yet another attribute that character cells
have", where "all subsequent cells that are painted are hyperlinks to
this target".  Cursor movement between those cells is exactly how the
specification's own example of an anchor split across two editor panes
would be emitted.
The suite never executed doctests, so the examples in the public docstrings
had drifted from what the code returns.  Enabling --doctest-modules turned up
eight stale examples:

  - ljust(), rjust(), center() and clip() wrote their examples as
    'wcwidth.ljust(...)', but a doctest runs in its own module's namespace,
    where the package name is not bound.
  - iter_graphemes() spelled its expected output with the same escapes as its
    input, but repr() escapes only what is unprintable: the ZWJ stays escaped
    while the emoji and the combining acute do not.
  - HyperlinkParams.parse() doubled the backslash of its terminator inside a
    raw docstring.
  - TextSizingParams and TextSizing gained a repr that omits fields left at
    their defaults, which no example had been updated for.
  - TextSizing.from_match() imported TEXT_SIZING_PATTERN on a plain indented
    line rather than a '>>>' line, so the name was never bound and the
    example raised NameError.

testpaths is set so that a bare 'pytest' does not walk bin/, docs/ and
setup.py, which --doctest-modules would otherwise import.  The legacy
wcwidth/wcwidth.py re-export shim is skipped: it defines nothing of its own,
and importing it rebinds the package attribute 'wcwidth.wcwidth' from the
function to the module, which breaks every later 'from wcwidth import
wcwidth'.
@jquast jquast changed the title jq/clip hyperlink hang Bugfix clip() can hang on OSC 8 hyperlinks Sep 17, 2026
Also, add module doctests to pylint and make them match
@jquast
jquast force-pushed the jq/clip-hyperlink-hang branch from 6e60aae to 416d584 Compare September 17, 2026 00:14
@jquast
jquast marked this pull request as ready for review September 17, 2026 00:14
@codspeed

codspeed Bot commented Sep 17, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 116 untouched benchmarks


Comparing jq/clip-hyperlink-hang (6e60aae) with master (e0927eb)

Open in CodSpeed

@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e0927eb) to head (416d584).

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #252   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         1980      1982    +2     
  Branches       462       463    +1     
=========================================
+ Hits          1980      1982    +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jquast
jquast merged commit a29c0e1 into master Sep 17, 2026
34 of 35 checks passed
@jquast
jquast deleted the jq/clip-hyperlink-hang branch September 17, 2026 00:18
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

Successfully merging this pull request may close these issues.

1 participant