python / python/cpython

array: unpickling an odd-length 'w' array across endianness raises ValueError (wrong UTF-16/UTF-32 item size)

Offen
#155,850 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

extension-modules type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

array.array pickles carry a machine-format code so the receiver can decode
data from a different platform. The decoder's format table
(mformat_descriptors in Modules/arraymodule.c) stores a wrong item size
for the Unicode formats: UTF-16 entries say 4 bytes per item (actual: 2) and
UTF-32 entries say 8 (actual: 4).

The only consumer of that field is the length check on the cross-endian slow
path, so unpickling an odd-length 'w' array from a machine of the opposite
endianness fails:

import array
recon = array._array_reconstructor  # what pickle calls
data = 'abc'.encode('utf-32-be')    # 3 code points, big-endian
recon(array.array, 'w', 21, data)   # 21 = UTF32_BE, on a little-endian machine
# ValueError: string length not a multiple of item size

Even lengths decode correctly, and same-endian pickles use the fast path that
skips the check, which is why this stayed hidden. On 3.13/3.14 the 'u'
typecode maps to the (equally wrong) UTF-16 entries on Windows.

Linked PRs
  • gh-155851

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in Modules/arraymodule.c, insbesondere bei der mformat_descriptors-Tabelle und dem Cross-Endian-Pfad von _array_reconstructor. Überprüfe, dass Daten mit ungerader Länge in UTF-16 und UTF-32 mit „w“ oder „u“ endianübergreifend ohne den ValueError für die Elementgröße entpickelt werden können; der verknüpfte PR gh-155851 zeigt, dass die Arbeit bereits im Gange ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, python
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.