Tcl Library Source Code

View Ticket
Login
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 mime Affected versions: 1.7.0 (tcllib 1.21) and 1.7.2 (tcllib 2.0 — current) Tested on: Tcl 8.6.15, openSUSE Leap 16, x86_64 Severity: silent data loss — no error is raised to the caller


Summary

::mime::parsetype reads the global variables errorCode and errorInfo unconditionally after a catch, including when the catch succeeded.

Those globals do not exist in a freshly created interpreter, and a successful catch does not create them. The read therefore raises

can't read "errorCode": no such variable

even when the Content-Type header parsed perfectly.

::mime::parsepart invokes parsetype inside an if {![catch ...]} guard and discards the error. The result is that state(content) is never assigned, the message silently defaults to text/plain, no MIME parts are constructed, and ::mime::getbody returns the entire raw body — including base64-encoded attachments — as if it were the message text.

No error reaches the application. The message simply appears to be a flat text/plain part.


Reproducer

Requires no message file; the failure is independent of message content.

package require mime

# Reproduce the state of a freshly started interpreter. In a genuinely fresh
# interpreter these globals are already absent; they are unset explicitly here
# so the defect is isolated from incidental load-time side effects (see
# "Why this is masked in 1.7.2" below).
unset -nocomplain ::errorCode ::errorInfo

set msg "From: [email protected]\nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=SEP\n\n--SEP\nContent-Type: text/plain\n\nbody\n--SEP--\n"
set f [file join [file dirname [info script]] repro.eml]
set fh [open $f w] ; puts -nonewline $fh $msg ; close $fh

set tok [::mime::initialize -file $f]
puts "content: [::mime::getproperty $tok content]"

Observed

content: text/plain

Expected

content: multipart/mixed

Priming the globals beforehand makes it behave correctly, which confirms the mechanism:

catch {error priming}      ;# creates ::errorCode and ::errorInfo
set tok [::mime::initialize -file $f]
puts [::mime::getproperty $tok content]   ;# -> multipart/mixed

Self-healing behaviour

Because raising the error itself creates ::errorCode and ::errorInfo, only the first affected call in an interpreter fails. Same file, same interpreter, three times:

parse 1 -> text/plain        <-- wrong
parse 2 -> multipart/mixed
parse 3 -> multipart/mixed

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

mime.tcl, ::mime::parsetype (line 1017 in 1.7.2; identical in 1.7.0):

proc ::mime::parsetype {token string} {
    global errorCode errorInfo                                  ;# 1018
    ...
    set code [catch {mime::parsetypeaux $token $string} result] ;# 1033
    set ecode $errorCode                                        ;# 1034  <-- raises when catch succeeded
    set einfo $errorInfo                                        ;# 1035
    ...
    return -code $code -errorinfo $einfo -errorcode $ecode $result
}

The code assumes catch always populates ::errorCode and ::errorInfo. It does not — they are only set when an error occurs. Demonstrated:

fresh interpreter :  errorCode exists=0  errorInfo exists=0
after ok catch    :  errorCode exists=0  errorInfo exists=0
after error catch :  errorCode exists=1  errorInfo exists=1

The error is then discarded by ::mime::parsepart (line 723):

if {![catch {set x [parsetype $token $value]}]} {
    set state(content) [lindex $x 0]      ;# never reached
    set state(params)  [lindex $x 1]
}

so nothing propagates to the caller.


All affected call sites in 1.7.2

The same idiom appears in ten places across seven procedures. parsetype is the one reached during ordinary message parsing, but the others are latent instances of the same defect:

Line Procedure
624 ::mime::initializeaux
1034 ::mime::parsetype
1682, 1699, 1742, 1757 ::mime::getbody
1858 ::mime::copymessage
2081 ::mime::buildmessage
2513 ::mime::parseaddress
2727 ::mime::addr_next

Why this is currently masked in tcllib 2.0

In tcllib 2.0, ::errorCode and ::errorInfo already exist by the time package require mime returns:

tcllib 1.21 — after require mime:  errorInfo exists=0   <-- defect fires
tcllib 2.0  — after require mime:  errorInfo exists=1   <-- defect masked

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 — diff over parsetype shows no change. The bug is therefore not fixed, only hidden, and will resurface if load-time behaviour changes or if an application clears the globals.


Suggested fixes

Option A — per-site, idiomatic (preferred upstream)

Use the return-options dictionary that catch provides, and drop the globals entirely:

proc ::mime::parsetype {token string} {
    # FRINK: nocheck
    variable $token
    upvar 0 $token state
    ...
    set code [catch {mime::parsetypeaux $token $string} result opts]

    unset {*}{
        state(input) state(buffer) state(lastC)
        state(comment) state(tokenL) state(lexemeL)
    }

    return -options $opts $result
}

return -options $opts $result reproduces the original semantics exactly: it re-raises with the original -errorcode / -errorinfo when code is non-zero, and returns normally when it is zero. The global errorCode errorInfo declaration can be deleted.

The same transformation applies to the other nine sites. Caution: the sites differ in shape — line 2081 (buildmessage) assigns ecode conditionally, and line 2727 (addr_next) sits inside a switch default — so each needs individual attention rather than a blanket substitution. (I broke initializeaux doing exactly that.)

Option B — minimal, cures all sites at once

If a small diff is preferred, defining the globals at load time is sufficient, because the only failure mode is their absence:

if {![info exists ::errorCode]} { set ::errorCode NONE }
if {![info exists ::errorInfo]} { set ::errorInfo {} }

Verified to fix the reproducer. It leaves the questionable idiom in place, so Option A is the better long-term fix.

Suggested additional hardening

parsepart line 723 silently discards any failure from parsetype, including genuine malformed-header errors. A malformed Content-Type currently produces a message that looks valid but has lost its structure. Logging or propagating that failure would have made this bug apparent immediately rather than presenting as silent data loss.


mime 1.7.0 contains a separate one-character typo at line 684, in the CR-stripping branch of parsepart:

set line [string range $line 0 $x-2]]
                                    ^ stray literal ]

Every line ending in \r — i.e. every header of every RFC 822 message with CRLF line endings — gets a ] appended. Content-Type: multipart/mixed; boundary=001a…ee] then fails type parsing with expecting ";" (found ]), and parsepart discards that error too, producing the same text/plain collapse.

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 practice

Encountered while converting a document corpus containing 948 email messages. Because multipart parsing silently collapsed, ::mime::getbody returned raw bodies with base64 attachments inline: 948 messages produced 471.7 MB of extracted text, mean 509 KB per message. Correct part-walking yields 7.6 MB, mean 8 KB — a 62× difference, with no error raised at any point.