Browse Source

add Nulllogger and use it to avoid logging to stderr in productive builds.

afl-fuzzing
Roker 3 years ago
parent
commit
30f8a4cae0
10 changed files with 53 additions and 26 deletions
  1. +1
    -1
      src/Makefile
  2. +3
    -3
      src/bodyparser.cc
  3. +4
    -4
      src/headerparser.cc
  4. +11
    -0
      src/nulllogger.cc
  5. +8
    -0
      src/nulllogger.hh
  6. +4
    -4
      src/pEpMIME.cc
  7. +8
    -0
      src/pEpMIME_internal.hh
  8. +4
    -4
      src/parse_address.cc
  9. +8
    -8
      src/parse_timestamp.cc
  10. +2
    -2
      src/rules.cc

+ 1
- 1
src/Makefile View File

@ -10,7 +10,7 @@ GTEST_DIR=$(HOME)/code/googletest/
all: libpEpMIME.a unittests
libpEpMIME.a: pEpMIME.o pEpMIME_internal.o rules.o bodyparser.o \
headerparser.o parse_timestamp.o parse_address.o \
headerparser.o parse_timestamp.o parse_address.o nulllogger.o \
base64.o nfc.o nfc_sets.o to_utf8.o quoted_printable.o
${AR} rcs $@ $^


+ 3
- 3
src/bodyparser.cc View File

@ -288,9 +288,9 @@ void parse_body(message* msg, const HeaderSection& headers, const BodyLines& bod
{
throw std::runtime_error( "Cannot parse \"" + std::string{cts} + "\" as ContentType");
}
std::cerr << "<<< CT raw: " << ct << ">>>\n";
LOG << "<<< CT raw: " << ct << ">>>\n";
ct.sanitize();
std::cerr << "<<< CT san: " << ct << ">>>\n";
LOG << "<<< CT san: " << ct << ">>>\n";
if(ct.type == "text")
{
const sv charset = header_value( ct.params, "charset" );
@ -310,7 +310,7 @@ void parse_body(message* msg, const HeaderSection& headers, const BodyLines& bod
}
}else{ // Non-MIME mail
std::cerr << "<<< NO_MIME_MAIL >>> " << body.size() << " body lines.\n";
LOG << "<<< NO_MIME_MAIL >>> " << body.size() << " body lines.\n";
sv combined_body = combineLines(body);
if(isUtf8(combined_body.data(), combined_body.data()+combined_body.size()) )
{


+ 4
- 4
src/headerparser.cc View File

@ -28,7 +28,7 @@ const qi::rule<std::string::const_iterator, DS() > message_id_list = *message_id
stringlist_t* create_stringlist( const DS& v)
{
// std::cerr << "CREATE_STRINGLIST with " << v.size() << " elements..." << std::endl;
// LOG << "CREATE_STRINGLIST with " << v.size() << " elements..." << std::endl;
stringlist_t* sl = new_stringlist( nullptr );
stringlist_t* s_iter = sl;
for( const auto& s : v )
@ -93,13 +93,13 @@ struct TRuleParser : public ParserBase
{
std::string::const_iterator begin=s.begin();
T1 t1;
// std::cerr << "<TRY TO PARSE \"" << s << "\" as " << typeid(T1).name() << ">\n";
// LOG << "<TRY TO PARSE \"" << s << "\" as " << typeid(T1).name() << ">\n";
if(qi::parse(begin, s.end(), rule, t1 ))
{
// std::cerr << "<ASSIGN OK>\n";
// LOG << "<ASSIGN OK>\n";
msg->*member = (*func)(t1);
}else{
// std::cerr << "<ASSIGN NOT OKAY: begin='" << *begin << "'>\n";
// LOG << "<ASSIGN NOT OKAY: begin='" << *begin << "'>\n";
}
}
};


+ 11
- 0
src/nulllogger.cc View File

@ -0,0 +1,11 @@
#include "nulllogger.hh"
class NullBuffer : public std::streambuf
{
public:
virtual int sync() { return 0; }
};
NullBuffer nullbuffer;
std::ostream nulllogger(&nullbuffer);

+ 8
- 0
src/nulllogger.hh View File

@ -0,0 +1,8 @@
#ifndef PEP_MIME_NULL_LOGGER_HH
#define PEP_MIME_NULL_LOGGER_HH
#include <ostream>
extern std::ostream nulllogger;
#endif

+ 4
- 4
src/pEpMIME.cc View File

@ -121,7 +121,7 @@ message* parse_message2(const char* begin, const char* const end)
{
headersize = lines.size();
}
std::cerr << "Parsing result: " << headersize << " raw header lines, " << (lines.size()-headersize) << " body lines:\n" << lines;
LOG << "Parsing result: " << headersize << " raw header lines, " << (lines.size()-headersize) << " body lines:\n" << lines;
HeaderSection headers;
@ -131,7 +131,7 @@ message* parse_message2(const char* begin, const char* const end)
{
sv line = lines[i];
const int lineType = header_line_type(line);
std::cerr << "Line #" << i << ": \"" << line << "\". type=" << lineType << ". \n";
LOG << "Line #" << i << ": \"" << line << "\". type=" << lineType << ". \n";
switch(lineType)
{
case -2: throw std::runtime_error("Cannot handle line \"" + std::string(line) + "\""); return nullptr; // TODO: C'mon, don't give up, yet! :-)
@ -156,10 +156,10 @@ message* parse_message2(const char* begin, const char* const end)
headers.push_back(nv);
}
std::cerr << "Parsing result: " << headers.size() << " parsed header lines (from " << headersize << " original lines):\n";
LOG << "Parsing result: " << headers.size() << " parsed header lines (from " << headersize << " original lines):\n";
for(const auto& h : headers)
{
std::cerr << "" << h.name << "” : “" << h.value << "\n";
LOG << "" << h.name << "” : “" << h.value << "\n";
}
// strip the header lines:


+ 8
- 0
src/pEpMIME_internal.hh View File

@ -16,6 +16,14 @@
#endif
#ifdef LOG_TO_STDERR
#include <iostream>
#define LOG std::cerr
#else
#include "nulllogger.hh"
#define LOG nulllogger
#endif
namespace pEpMIME
{


+ 4
- 4
src/parse_address.cc View File

@ -44,7 +44,7 @@ const TRule<Identity> identity_parser = qi::omit[-fws] >> mailbox >> qi::omit[-f
const TRule< std::vector<Identity> > identity_list_parser = identity_parser % ',';
//(qi::lit(',')[
// px::ref(std::cerr) << " {KOMMA} " ])
// px::ref(LOG) << " {KOMMA} " ])
// ;
} // end of anonymous namespace
@ -60,7 +60,7 @@ pEp_identity* parse_address(const std::string& s)
std::string::const_iterator begin = s.begin();
bool okay = qi::parse(begin, s.end(), identity_parser, iden );
// std::cerr << " \n === Identity \"" << s << "\" is " << (okay?"okay":"NOT OKAY") << ". name=“" << iden.name << "”, addr=“" << iden.address << "”. ";
// LOG << " \n === Identity \"" << s << "\" is " << (okay?"okay":"NOT OKAY") << ". name=“" << iden.name << "”, addr=“" << iden.address << "”. ";
if(!okay)
{
@ -69,7 +69,7 @@ pEp_identity* parse_address(const std::string& s)
throw std::runtime_error( ss.str() );
}
// std::cerr << " ===\n";
// LOG << " ===\n";
return new_identity(toNFC(iden.address).c_str(), nullptr, nullptr, toNFC(iden.name).c_str());
}
@ -88,7 +88,7 @@ identity_list* parse_address_list(const std::string& s)
throw std::runtime_error( ss.str() );
}
// std::cerr << "ADDR_LIST: I found " << vi.size() << " elements in address list “" << s << "” \n";
// LOG << "ADDR_LIST: I found " << vi.size() << " elements in address list “" << s << "” \n";
if(vi.empty())
return nullptr;


+ 8
- 8
src/parse_timestamp.cc View File

@ -112,7 +112,7 @@ const Parser<timestamp> time_of_day = digit2[(&_val)->*&timestamp::tm_hour = _
const Parser<timestamp> time_parser = time_of_day[ _val = _1] >> zone[ (&_val)->*&timestamp::tm_sec -= _1 * 60];
const IntParser year = (digit4[_val=_1]
| digit2[ qi::_val = px::if_else(qi::_1 < 50 , 2000+qi::_1 , 1900 + qi::_1) ]) /* [px::ref(std::cerr) << "@Y@:" << qi::_val] */ ;
| digit2[ qi::_val = px::if_else(qi::_1 < 50 , 2000+qi::_1 , 1900 + qi::_1) ]) /* [px::ref(LOG) << "@Y@:" << qi::_val] */ ;
const Parser<timestamp> date_parser = -ows >> digit1_2 [(&_val)->*&timestamp::tm_mday = _1]
>> ows >> month_name[(&_val)->*&timestamp::tm_mon = _1]
@ -141,7 +141,7 @@ namespace pEpMIME
std::string::const_iterator begin = s.begin();
int i = -42;
bool okay = qi::parse(begin, s.end(), zone, i );
std::cerr << " \n ===Zone: \"" << s << "\" -> " << i << " === (" << okay << ") ===\n";
LOG << " \n ===Zone: \"" << s << "\" -> " << i << " === (" << okay << ") ===\n";
}
}
@ -153,12 +153,12 @@ namespace pEpMIME
std::string::const_iterator begin = s.begin();
T t{};
bool okay = qi::parse(begin, s.end(), parser, t );
std::cerr << "\t=== " << name << ": \"" << s << "\" -> " << t << " is " << (okay?"okay":"NOT OKAY");
LOG << "\t=== " << name << ": \"" << s << "\" -> " << t << " is " << (okay?"okay":"NOT OKAY");
if(!okay)
{
std::cerr << ". Error at position " << (begin - s.begin()) << ": \"" << *begin << "\" ";
LOG << ". Error at position " << (begin - s.begin()) << ": \"" << *begin << "\" ";
}
std::cerr << " ===\n";
LOG << " ===\n";
}
}
@ -183,14 +183,14 @@ timestamp* parse_timestamp(const std::string& s)
std::string::const_iterator begin = s.begin();
bool okay = qi::parse(begin, s.end(), date_time, *ts );
std::cerr << " \n === TimeStamp \"" << s << "\" is " << (okay?"okay":"NOT OKAY");
LOG << " \n === TimeStamp \"" << s << "\" is " << (okay?"okay":"NOT OKAY");
if(!okay)
{
std::cerr << ". Error at position " << (begin - s.begin()) << ": \"" << *begin << "\" ";
LOG << ". Error at position " << (begin - s.begin()) << ": \"" << *begin << "\" ";
}
std::cerr << " ===\n";
LOG << " ===\n";
// normalize values:
timegm(ts);


+ 2
- 2
src/rules.cc View File

@ -50,7 +50,7 @@ TRule<std::string, std::string, std::string, std::string> encoded_word = ( qi::l
( qi::lit("?Q?") >> (*(qi::char_-'?'))[ px::ref(qi::_b) <<= qi::_1][qi::_c = px::bind(qp::decode_header, qi::_b)] ) // "Q" encoding
)
>> qi::lit("?=") )
// [px::ref(std::cerr) << "<INPUT: \"" << qi::_b << "\", OUTPUT: \"" << qi::_c << "\".>\n"]
// [px::ref(LOG) << "<INPUT: \"" << qi::_b << "\", OUTPUT: \"" << qi::_c << "\".>\n"]
[qi::_val += px::bind(to_utf8, qi::_a, qi::_c)]; // charset conversion
@ -74,7 +74,7 @@ Rule quoted_string = '"' >> *qcontent >> '"';
Rule encoded_words = (encoded_word % qi::omit[cfws]);
Rule word = (quoted_string | encoded_words | atom_nows)
// [px::ref(std::cerr) << "<<<WORD:“" << qi::_1 << "”.>>>"]
// [px::ref(LOG) << "<<<WORD:“" << qi::_1 << "”.>>>"]
[qi::_val += qi::_1];
; // Note: putting "encoded_word" here is my own interpretation of RFC 2047


Loading…
Cancel
Save