be more robust to convert BS into valid NFC UTF-8.

afl-fuzzing
Roker 4 years ago
parent 02844e2d37
commit 1be7f37f8c

@ -3,6 +3,7 @@
#include "parse_address.hh"
#include "parse_timestamp.hh"
#include "rules.hh"
#include "to_utf8.hh"
#include <pEp/pEp_string.h>
#include <pEp/stringlist.h>
@ -118,10 +119,22 @@ TRuleParser<T1, T2>* P(T2 message::* member, const qi::rule<std::string::const_i
}
std::string robust_to_utf8(const std::string& s)
{
std::string ret;
try{
ret = toNFC(s);
}catch(illegal_utf8& e)
{
ret = to_utf8("ISO-8859-1", s);
}
return ret;
}
// make sure the C string is allocated on the correct heap, especially on MS Windows. *sigh*
char* copy_string(const std::string& s)
{
const std::string nfc = toNFC(s); // TODO: use views/ranges to avoid copying
const std::string nfc = robust_to_utf8(s); // TODO: use views/ranges to avoid copying
return new_string(nfc.c_str(), nfc.size());
}
@ -129,7 +142,7 @@ char* copy_string(const std::string& s)
const std::map<std::string, struct ParserBase*> parsers =
{
{"date", P(&message::recv, &parse_timestamp) },
{"message-ID", P(&message::id, message_id, &copy_string) },
{"message-id", P(&message::id, message_id, &copy_string) },
{"subject", P(&message::shortmsg, phrase, &copy_string) },
{"from", P(&message::from, &parse_address) },
{"to", P(&message::to, &parse_address_list) },
@ -155,8 +168,9 @@ void parse_header(message* msg, const HeaderSection& header)
if(p == parsers.end())
{
// unknown header with no special parser: just dump it in opt_fields
const std::string nfc = toNFC(h.value); // TODO: use views/ranges to avoid copying
auto sp = new_stringpair( h.name.c_str(), nfc.c_str() );
const std::string nfc_name = robust_to_utf8(h.name); // TODO: use views/ranges to avoid copying
const std::string nfc_value = robust_to_utf8(h.value); // TODO: use views/ranges to avoid copying
auto sp = new_stringpair( nfc_name.c_str(), nfc_value.c_str() );
auto f = stringpair_list_add( msg->opt_fields, sp);
// the function above has a strange semantic:

Loading…
Cancel
Save