Open Bug 2068760 Opened 2 months ago Updated 2 months ago

Reduce raw pointer use and modernise memory management in libmime

Categories

(MailNews Core :: MIME, task)

Desktop
All
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_strcatn calls with PR_smprintf.
  • Eliminate unsafe const-casting and in-place string mutation in favour of std::string_view slicing.
  • 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_view pre-allocation trimming before PL_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

Attachment #9635615 - Attachment description: Bug 2068760 - Modernize pointer and memory management as well as dead code removal in libmime. r=#thunderbird-back-end-reviewers → Bug 2068760 - Modernise pointer and memory management and remove unused code branches in libmime. r=#thunderbird-back-end-reviewers
See Also: → 2068939
Attachment #9635615 - Attachment description: Bug 2068760 - Modernise pointer and memory management and remove unused code branches in libmime. r=#thunderbird-back-end-reviewers → WIP: Bug 2068760 - Modernise pointer and memory management and remove unused code branches in libmime. r=#thunderbird-back-end-reviewers
Attachment #9635615 - Attachment description: WIP: Bug 2068760 - Modernise pointer and memory management and remove unused code branches in libmime. r=#thunderbird-back-end-reviewers → Bug 2068760 - Modernise pointer and memory management and remove unused code branches in libmime. r=#thunderbird-back-end-reviewers
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: