From cfc68732e7dca05c0dad3bfbb75b12c7fe0d3e28 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 25 Feb 2022 22:53:07 +0200 Subject: [PATCH] message-contact: remove control characters from names I.e., issue #2216. --- lib/mu-message-contact.cc | 24 +++++++++++++++++++++- lib/mu-message-contact.hh | 42 +++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/lib/mu-message-contact.cc b/lib/mu-message-contact.cc index cae91262..fb1c2028 100644 --- a/lib/mu-message-contact.cc +++ b/lib/mu-message-contact.cc @@ -139,12 +139,33 @@ test_ctor_02() g_assert_true(c.personal); g_assert_cmpuint(c.frequency,==,13); g_assert_cmpuint(c.tstamp,==,12345); - assert_equal(c.name, "Blinky"); g_assert_cmpuint(c.message_date,==,1645215014); assert_equal(c.display_name(), "Blinky "); } +static void +test_ctor_03() +{ + MessageContact c{ + "bar@example.com", + "Bli\nky", + 1645215014, + true, /* personal */ + 13, /*freq*/ + 12345 /* tstamp */ + }; + + assert_equal(c.email, "bar@example.com"); + assert_equal(c.name, "Bli ky"); + g_assert_true(c.personal); + g_assert_cmpuint(c.frequency,==,13); + g_assert_cmpuint(c.tstamp,==,12345); + g_assert_cmpuint(c.message_date,==,1645215014); + + assert_equal(c.display_name(), "Bli ky "); +} + static void @@ -173,6 +194,7 @@ main(int argc, char* argv[]) g_test_add_func("/lib/message-contacts/ctor-01", test_ctor_01); g_test_add_func("/lib/message-contacts/ctor-02", test_ctor_02); + g_test_add_func("/lib/message-contacts/ctor-cleanup", test_ctor_cleanup); g_test_add_func("/lib/message-contacts/make-contacts", test_make_contacts); return g_test_run(); diff --git a/lib/mu-message-contact.hh b/lib/mu-message-contact.hh index 39af06b0..6318867d 100644 --- a/lib/mu-message-contact.hh +++ b/lib/mu-message-contact.hh @@ -34,11 +34,11 @@ struct _InternetAddressList; namespace Mu { -/** +/** * Get the hash value for a lowercase value of s; useful for email-addresses - * + * * @param s a string - * + * * @return a hash value. */ size_t lowercase_hash(const std::string& s); @@ -66,11 +66,10 @@ struct MessageContact { * @param message_date_ data for the message for this contact */ MessageContact(const std::string& email_, const std::string& name_ = "", - Type type_ = Type::Unknown, time_t message_date_ = 0) + Type type_ = Type::Unknown, time_t message_date_ = 0) : email{email_}, name{name_}, type{type_}, message_date{message_date_}, personal{}, frequency{1}, tstamp{} - { - } + { cleanup_name(); } /** * Construct a new MessageContact @@ -83,13 +82,12 @@ struct MessageContact { * @param tstamp_ timestamp for last change */ MessageContact(const std::string& email_, const std::string& name_, - time_t message_date_, bool personal_, size_t freq_, - int64_t tstamp_) + time_t message_date_, bool personal_, size_t freq_, + int64_t tstamp_) : email{email_}, name{name_}, type{Type::Unknown}, message_date{message_date_}, personal{personal_}, frequency{freq_}, tstamp{tstamp_} - { - } + { cleanup_name();} /** * Get the "display name" for this contact; basically, if there's a @@ -100,19 +98,19 @@ struct MessageContact { * @return the display name */ std::string display_name() const; - - /** + + /** * Operator==; based on the hash values (ie. lowercase e-mail address) - * + * * @param rhs some other MessageContact - * + * * @return true orf false. */ bool operator== (const MessageContact& rhs) const noexcept { return hash() == rhs.hash(); } - /** + /** * Get a hash-value for this contact, which gets lazily calculated. This * is for use with container classes. This uses the _lowercase_ email * address. @@ -126,7 +124,7 @@ struct MessageContact { } return cached_hash; } - + /* * data members */ @@ -135,11 +133,17 @@ struct MessageContact { std::string name; /**< Name for this contact; can be empty. */ Type type{Type::Unknown}; /**< Type of contact */ ::time_t message_date; /**< date of the message from which the - * contact originates */ + * contact originates */ bool personal; /**< A personal message? */ size_t frequency; /**< Frequency of this contact */ int64_t tstamp; /**< Timestamp for this contact */ +private: + void cleanup_name() { // replace control characters by spaces. + for (auto& c: name) + if (iscntrl(c)) + c = ' '; + } }; using MessageContacts = std::vector; @@ -156,7 +160,7 @@ using MessageContacts = std::vector; */ MessageContacts make_message_contacts(/*const*/ struct _InternetAddressList* addr_lst, - MessageContact::Type type, ::time_t message_date); + MessageContact::Type type, ::time_t message_date); /** * Create a sequence of MessageContact objects from an InternetAddressList @@ -172,7 +176,7 @@ make_message_contacts(const std::string& addrs, MessageContact::Type type, ::time_t message_date); } // namespace Mu -/** +/** * Implement our hash int std:: */ template<> struct std::hash {