Bug 2407232

Summary: UI buffer overflow on any run of the command
Product: [Fedora] Fedora Reporter: Mae Miller <mmiller>
Component: beetsAssignee: Gerald Cox <gbcox>
Status: CLOSED ERRATA QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: urgent Docs Contact:
Priority: unspecified    
Version: 43CC: gbcox, mh, michele
Target Milestone: ---Keywords: Desktop
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: beets-2.5.1-2.fc44 beets-2.5.1-2.fc43 Doc Type: ---
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2025-11-01 01:30:39 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Mae Miller 2025-10-29 20:57:33 UTC
Any time the program is run, regardless of command options, I get:

Traceback (most recent call last):
  File "/usr/bin/beet", line 5, in <module>
    from beets.ui import main
  File "/usr/lib/python3.14/site-packages/beets/ui/__init__.py", line 858, in <module>
    indent_str, left, right, separator=" -> ", max_width=term_width()
                                                         ~~~~~~~~~~^^
  File "/usr/lib/python3.14/site-packages/beets/ui/__init__.py", line 715, in term_width
    buf = fcntl.ioctl(0, termios.TIOCGWINSZ, " " * 4)
SystemError: buffer overflow

Reproducible: Always

Steps to Reproduce:
1. Install beets
2. Run `beets` or any sub command

Actual Results:
Traceback (most recent call last):
  File "/usr/bin/beet", line 5, in <module>
    from beets.ui import main
  File "/usr/lib/python3.14/site-packages/beets/ui/__init__.py", line 858, in <module>
    indent_str, left, right, separator=" -> ", max_width=term_width()
                                                         ~~~~~~~~~~^^
  File "/usr/lib/python3.14/site-packages/beets/ui/__init__.py", line 715, in term_width
    buf = fcntl.ioctl(0, termios.TIOCGWINSZ, " " * 4)
SystemError: buffer overflow

Expected Results:
Program outputs help options or does an import

Comment 1 Gerald Cox 2025-10-29 23:59:45 UTC
Appears to be caused to the change to python 3.14; since it didn't occur in F42.  Researching.

Comment 2 Gerald Cox 2025-10-31 19:30:22 UTC
diff -Naur beets-2.5.1/beets/ui/__init__.py beets-2.5.1.patched/beets/ui/__init__.py
--- beets-2.5.1/beets/ui/__init__.py    2025-10-14 15:51:25.515184900 -0700
+++ beets-2.5.1.patched/beets/ui/__init__.py    2025-10-31 11:57:03.165497870 -0700
@@ -700,27 +700,31 @@
 
 
 def term_width():
-    """Get the width (columns) of the terminal."""
+    """Get the width (columns) of the terminal, robustly."""
     fallback = config["ui"]["terminal_width"].get(int)
 
-    # The fcntl and termios modules are not available on non-Unix
-    # platforms, so we fall back to a constant.
+    # First, try POSIX ioctl with a proper bytes buffer (Python 3.13+ requires it).
     try:
-        import fcntl
-        import termios
-    except ImportError:
-        return fallback
+        import fcntl, termios, struct
+        # Prepare an 8-byte buffer and unpack all four unsigned shorts
+        s = struct.pack("HHHH", 0, 0, 0, 0)
+        res = fcntl.ioctl(0, termios.TIOCGWINSZ, s)
+        rows, cols, _, _ = struct.unpack("HHHH", res)
+        if cols:
+            return int(cols)
+    except Exception:
+        # Includes ImportError (non-Unix), ENOTTY, SystemError on 3.13/3.14, etc.
+        pass
 
+    # Next, try shutil (respects $COLUMNS and works on non-TTYs)
     try:
-        buf = fcntl.ioctl(0, termios.TIOCGWINSZ, " " * 4)
-    except OSError:
-        return fallback
-    try:
-        height, width = struct.unpack("hh", buf)
-    except struct.error:
-        return fallback
-    return width
+        import shutil
+        return shutil.get_terminal_size((fallback or 80, 24)).columns
+    except Exception:
+        pass
 
+    # Final fallback to configured/static width
+    return fallback or 80
 
 def split_into_lines(string, width_tuple):
     """Splits string into a list of substrings at whitespace.

Comment 3 Fedora Update System 2025-10-31 20:01:16 UTC
FEDORA-2025-b66c97cdf6 (beets-2.5.1-2.fc44) has been submitted as an update to Fedora 44.
https://bodhi.fedoraproject.org/updates/FEDORA-2025-b66c97cdf6

Comment 4 Fedora Update System 2025-10-31 20:11:40 UTC
FEDORA-2025-b589181602 (beets-2.5.1-2.fc43) has been submitted as an update to Fedora 43.
https://bodhi.fedoraproject.org/updates/FEDORA-2025-b589181602

Comment 5 Fedora Update System 2025-11-01 01:30:39 UTC
FEDORA-2025-b66c97cdf6 (beets-2.5.1-2.fc44) has been pushed to the Fedora 44 stable repository.
If problem still persists, please make note of it in this bug report.

Comment 6 Fedora Update System 2025-11-02 01:21:59 UTC
FEDORA-2025-b589181602 has been pushed to the Fedora 43 testing repository.
Soon you'll be able to install the update with the following command:
`sudo dnf upgrade --enablerepo=updates-testing --refresh --advisory=FEDORA-2025-b589181602`
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2025-b589181602

See also https://fedoraproject.org/wiki/QA:Updates_Testing for more information on how to test updates.

Comment 7 Fedora Update System 2025-11-03 01:38:22 UTC
FEDORA-2025-b589181602 (beets-2.5.1-2.fc43) has been pushed to the Fedora 43 stable repository.
If problem still persists, please make note of it in this bug report.