Commit 4c93771
committed
gh-138223: Fix infinite loop in email._header_value_parser._fold_mime_parameters
Fix infinite loop that occurred when processing MIME parameters with very long
parameter names during RFC 2231 encoding. The issue was in the while loop
that tried to find split points for long parameter values.
Changes made:
- In email._header_value_parser._fold_mime_parameters(): Replace infinite
'while True:' loop with 'while splitpoint > 1:' and ensure splitpoint is
always at least 1 to prevent getting stuck
- Add fallback logic to force minimal splits when values cannot fit
Testing:
- Added test_mime_parameter_folding_no_infinite_loop to TestMIMEPart class
- Test creates scenario where maxchars = 1 (edge case that caused infinite loop)
- Verifies as_string() completes successfully instead of hanging
- Test passes, confirming the fix prevents infinite loops
This fixes GitHub issue #138223 where add_attachment() with long parameter
keys would cause as_string() to hang indefinitely during MIME parameter
folding and header processing.1 parent 04debff commit 4c93771
2 files changed
Lines changed: 18 additions & 150 deletions
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1087 | 1087 | | |
1088 | 1088 | | |
1089 | 1089 | | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
1090 | 1108 | | |
1091 | 1109 | | |
1092 | 1110 | | |
0 commit comments