| Ticket UUID: | 05349414362d9ea895f78492940807a1b9dad9d3 | |||||||||||||||||||
| Title: | Tcllib - ::mime::parsetype (mime.tcl:1017) - state(content) is never set | |||||||||||||||||||
| Type: | Bug | Version: | tcllib 1.21 (mime 1.7.0) and tcllib 2.0 (mime 1.7.2). | |||||||||||||||||
| Submitter: | pfinnegan01 | Created on: | 2026-08-16 06:47:12 | |||||||||||||||||
| Subsystem: | (unused) | Assigned To: | ||||||||||||||||||
| Priority: | 5 Medium | Severity: | Minor | |||||||||||||||||
| Status: | Open | Last Modified: | 2026-11-16 00:06:14 | |||||||||||||||||
| Resolution: | None | Closed By: | nobody | |||||||||||||||||
| Closed on: | ||||||||||||||||||||
| Description: |
The actual bug
::mime::parsetype (mime.tcl:1017) does this:
proc ::mime::parsetype {token string} {
global errorCode errorInfo
...
set code [catch {mime::parsetypeaux $token $string} result]
set ecode $errorCode ;# ← reads ::errorCode unconditionally
set einfo $errorInfo ;# ← reads ::errorInfo unconditionally
...
return -code $code -errorinfo $einfo -errorcode $ecode $result
}
fresh interpreter → errorCode exists: 0 errorInfo exists: 0
after one error → errorCode exists: 1 errorInfo exists: 1
So on the first Content-Type parsed in any interpreter, set ecode $errorCode raises can't read "errorCode": no such variable — even when the Content-Type parsed perfectly. Then parsepart (mime.tcl:723) swallows it:
if {![catch {set x [parsetype $token $value]}]} {
set state(content) [lindex $x 0] ;# never reached
}
state(content) is never set, so the message defaults to text/plain, no parts are built, and getbody returns the entire raw body — base64 attachments and all.
The failure is self-healing: throwing that error creates ::errorCode and ::errorInfo, so every subsequent parse in that interpreter works.
Proof
┌───────────────────────────────────────┬───────────────────────────────────────────┐
│ Test │ Result │
├───────────────────────────────────────┼───────────────────────────────────────────┤
│ Each file alone, own interpreter │ both text/plain ❌ │
├───────────────────────────────────────┼───────────────────────────────────────────┤
│ Two files, one interpreter │ 1st text/plain ❌, 2nd multipart/mixed ✅ │
├───────────────────────────────────────┼───────────────────────────────────────────┤
│ Same, reversed order │ 1st text/plain ❌, 2nd multipart/mixed ✅ │
├───────────────────────────────────────┼───────────────────────────────────────────┤
│ One file, after catch {error priming} │ multipart/mixed, parts: 2 ✅ │
└───────────────────────────────────────┴───────────────────────────────────────────┘
Order is irrelevant — it's always the first. And priming ::errorInfo with a throwaway error fixes it completely.
┌────────┬────────────────────────────────────┬──────────────────────────────────────────────────────────┐
│ | Installed │ Available │
├────────┼────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│ Tcl │ 8.6.15 (system), 8.6.9 (ActiveTcl) │ tcl9 9.0.3 and tcl 8.6.18, both from devel:languages:tcl │
├────────┼────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│ tcllib │ 1.21 (mime 1.7.0) │ 2.0 (mime 1.7.2), same repo │
└────────┴────────────────────────────────────┴──────────────────────────────────────────────────────────┘
mime ships in tcllib, not Tcl core, so upgrading to Tcl 9 wouldn't change the package at all. The relevant upgrade is tcllib 1.21 → 2.0.
tcllib 2.0 does fix the symptom — but by accident
I downloaded tcllib 2.0 (without installing) and tested it directly.
The buggy code is byte-identical. diff of parsetype between 1.7.0 and 1.7.2 shows no change — same global errorCode errorInfo, same unconditional set ecode $errorCode at line 1018, same silent catch swallow in parsepart at line 723.
Yet 1.7.2 parses correctly on the first attempt. Why:
tcllib 1.21: after 'package require mime' → errorInfo exists=0 ❌
tcllib 2.0: after 'package require mime' → errorInfo exists=1 ✅
Something in tcllib 2.0's load path raises and catches an error during initialisation, which creates ::errorInfo and ::errorCode as a side effect. That incidentally primes the exact globals parsetype reads — so the latent bug never fires.
| |||||||||||||||||||
| User Comments: |
serhiy.storchaka added on 2026-11-16 00:06:14:
Proposed fix in branch mime-catch-options: all places that read errorCode/errorInfo after catch now use the catch options dict, so a stale or missing global value no longer makes the first multipart parse return text/plain. Tests mime-15.0 and mime-15.1. pfinnegan01 (claiming to be pfinnegan) added on 2026-08-16 07:38:15: Package: tcllib Summary
Those globals do not exist in a freshly created interpreter, and a successful
even when the Content-Type header parsed perfectly.
No error reaches the application. The message simply appears to be a flat text/plain part. ReproducerRequires no message file; the failure is independent of message content.
Observed
Expected
Priming the globals beforehand makes it behave correctly, which confirms the mechanism:
Self-healing behaviourBecause raising the error itself creates
The failure is therefore order-dependent and easy to miss in a test suite that performs any operation raising an error before the first parse. Cause
The code assumes
The error is then discarded by
so nothing propagates to the caller. All affected call sites in 1.7.2The same idiom appears in ten places across seven procedures.
Why this is currently masked in tcllib 2.0In tcllib 2.0,
Something in the 2.0 load path raises and catches an error, incidentally
creating the globals. The defective code is byte-identical between 1.7.0 and
1.7.2 — Suggested fixesOption A — per-site, idiomatic (preferred upstream)Use the return-options dictionary that
The same transformation applies to the other nine sites. Caution: the sites
differ in shape — line 2081 ( Option B — minimal, cures all sites at onceIf a small diff is preferred, defining the globals at load time is sufficient, because the only failure mode is their absence:
Verified to fix the reproducer. It leaves the questionable idiom in place, so Option A is the better long-term fix. Suggested additional hardening
Related, already fixed — reported only for completeness
Every line ending in Measured on 12 real multipart messages under 1.7.0: 0 of 12 parsed as multipart. With the stray bracket removed: 12 of 12. This is already fixed in 1.7.2 — zero occurrences. No action needed; it is noted only because the two defects produce identical symptoms, and anyone diagnosing this on tcllib 1.21 will encounter both. Impact observed in practiceEncountered while converting a document corpus containing 948 email messages.
Because multipart parsing silently collapsed, | |||||||||||||||||||
Home
Blog
Docs
Downloads
Tickets
Timeline
Branches
Tags
Wiki