Reduce raw pointer use and modernise memory management in libmime
Categories
(MailNews Core :: MIME, task)
Tracking
(Not tracked)
People
(Reporter: maxe, Assigned: maxe)
References
Details
Attachments
(1 external file)
Hello all,
Not too long ago, I was unfortunately guilty of this myself trying to make my patches very in line with surrounding code, but it turns out there are no good reasons to stick with the old mannerisms as the whole project requires C++20 and no one in their right mind will try to use libmime outside of Thunderbird.
In this patch, I propose the following to improve memory safety and readability:
- Replace manual buffer sizing and chained
PL_strcatncalls withPR_smprintf. - Eliminate unsafe
const-casting and in-place string mutation in favour ofstd::string_viewslicing. - Replace raw pointer increments (
*out++ = *in) with bounds-checked Mozilla string helpers (StripChars,nsPrintfCString). - Replace manual heap allocation/deallocation bookkeeping with RAII string adoption (
Adopt). - Replace post-allocation string mutation and index loops with
std::string_viewpre-allocation trimming beforePL_strndup. - Use standard Mozilla C++ types as well as standard-library features
std::(string_view,starts_with,find_last_not_of) where sensible. - Remove some fully dead/unused code branches
The fun really begins when we end up with a significant net decrease in LOC. I even did some analysis of the compiled ASM and we end up with a net decrease in instructions while in other cases security improvements are obvious. It would be absurd to bench, but I assume a minimal performance improvement.
Reviewers can push this to the bottom of the queue as the expected behaviour is no observable change at runtime.
Cheers
Max
| Assignee | ||
Comment 1•2 months ago
|
||
Updated•2 months ago
|
Updated•2 months ago
|
Updated•2 months ago
|
Description
•