Fixing a bunch of format strings.

Also leveraging GCC if available to check format specificier.

Closes #567.
This commit is contained in:
Robin Sommer 2011-10-18 17:39:40 -07:00
parent d86525ce61
commit 63b46a0ae2
14 changed files with 38 additions and 31 deletions

@ -1 +1 @@
Subproject commit 31c3d1d9efa706969f0cd98223444c5f064a6fa7 Subproject commit aff2b1eba11d9df4e580ec0a039e01c94bb9c6a2

View file

@ -644,7 +644,7 @@ const char* CompositeHash::RecoverOneVal(const HashKey* k, const char* kp0,
Func* f = Func::GetFuncPtrByID(*kp); Func* f = Func::GetFuncPtrByID(*kp);
if ( ! f ) if ( ! f )
reporter->InternalError("failed to look up unique function id %"PRIu32" in CompositeHash::RecoverOneVal()"); reporter->InternalError("failed to look up unique function id %" PRIu32 " in CompositeHash::RecoverOneVal()", *kp);
pval = new Val(f); pval = new Val(f);

View file

@ -285,7 +285,7 @@ FILE* BroFile::BringIntoCache()
if ( ! f ) if ( ! f )
{ {
reporter->Error("can't open %s", this); reporter->Error("can't open %s", name);
f = fopen("/dev/null", "w"); f = fopen("/dev/null", "w");

View file

@ -103,7 +103,7 @@ protected:
void Error(const char* msg) void Error(const char* msg)
{ {
reporter->Error(msg); reporter->Error("%s", msg);
err = true; err = true;
} }

View file

@ -3085,7 +3085,7 @@ void RemoteSerializer::FatalError(const char* msg)
{ {
msg = fmt("fatal error, shutting down communication: %s", msg); msg = fmt("fatal error, shutting down communication: %s", msg);
Log(LogError, msg); Log(LogError, msg);
reporter->Error(msg); reporter->Error("%s", msg);
closed = true; closed = true;
kill(child_pid, SIGQUIT); kill(child_pid, SIGQUIT);

View file

@ -15,6 +15,13 @@
class Connection; class Connection;
class Location; class Location;
// Check printf-style variadic arguments if we can.
#if __GNUC__
#define FMT_ATTR __attribute__((format(printf, 2, 3))) // sic! 1st is "this" I guess.
#else
#define FMT_ATTR
#endif
class Reporter { class Reporter {
public: public:
Reporter(); Reporter();
@ -22,25 +29,25 @@ public:
// Report an informational message, nothing that needs specific // Report an informational message, nothing that needs specific
// attention. // attention.
void Info(const char* fmt, ...); void Info(const char* fmt, ...) FMT_ATTR;
// Report a warning that may indicate a problem. // Report a warning that may indicate a problem.
void Warning(const char* fmt, ...); void Warning(const char* fmt, ...) FMT_ATTR;
// Report a non-fatal error. Processing proceeds normally after the error // Report a non-fatal error. Processing proceeds normally after the error
// has been reported. // has been reported.
void Error(const char* fmt, ...); void Error(const char* fmt, ...) FMT_ATTR;
// Returns the number of errors reported so far. // Returns the number of errors reported so far.
int Errors() { return errors; } int Errors() { return errors; }
// Report a fatal error. Bro will terminate after the message has been // Report a fatal error. Bro will terminate after the message has been
// reported. // reported.
void FatalError(const char* fmt, ...); void FatalError(const char* fmt, ...) FMT_ATTR;
// Report a fatal error. Bro will terminate after the message has been // Report a fatal error. Bro will terminate after the message has been
// reported and always generate a core dump. // reported and always generate a core dump.
void FatalErrorWithCore(const char* fmt, ...); void FatalErrorWithCore(const char* fmt, ...) FMT_ATTR;
// Report a traffic weirdness, i.e., an unexpected protocol situation // Report a traffic weirdness, i.e., an unexpected protocol situation
// that may lead to incorrectly processing a connnection. // that may lead to incorrectly processing a connnection.
@ -51,15 +58,15 @@ public:
// Syslog a message. This methods does nothing if we're running // Syslog a message. This methods does nothing if we're running
// offline from a trace. // offline from a trace.
void Syslog(const char* fmt, ...); void Syslog(const char* fmt, ...) FMT_ATTR;
// Report about a potential internal problem. Bro will continue // Report about a potential internal problem. Bro will continue
// normally. // normally.
void InternalWarning(const char* fmt, ...); void InternalWarning(const char* fmt, ...) FMT_ATTR;
// Report an internal program error. Bro will terminate with a core // Report an internal program error. Bro will terminate with a core
// dump after the message has been reported. // dump after the message has been reported.
void InternalError(const char* fmt, ...); void InternalError(const char* fmt, ...) FMT_ATTR;
// Toggle whether non-fatal messages should be reported through the // Toggle whether non-fatal messages should be reported through the
// scripting layer rather on standard output. Fatal errors are always // scripting layer rather on standard output. Fatal errors are always

View file

@ -909,7 +909,7 @@ bool FileSerializer::Read(UnserialInfo* info, const char* file, bool header)
void FileSerializer::ReportError(const char* str) void FileSerializer::ReportError(const char* str)
{ {
reporter->Error(str); reporter->Error("%s", str);
} }
void FileSerializer::GotID(ID* id, Val* val) void FileSerializer::GotID(ID* id, Val* val)

View file

@ -261,7 +261,7 @@ public:
virtual ~CloneSerializer() { } virtual ~CloneSerializer() { }
protected: protected:
virtual void ReportError(const char* msg) { reporter->Error(msg); } virtual void ReportError(const char* msg) { reporter->Error("%s", msg); }
virtual void GotID(ID* id, Val* val) { } virtual void GotID(ID* id, Val* val) { }
virtual void GotEvent(const char* name, double time, virtual void GotEvent(const char* name, double time,
EventHandlerPtr event, val_list* args) { } EventHandlerPtr event, val_list* args) { }

View file

@ -2118,7 +2118,7 @@ Val* TableVal::Delete(const Val* index)
Val* va = v ? (v->Value() ? v->Value() : this->Ref()) : 0; Val* va = v ? (v->Value() ? v->Value() : this->Ref()) : 0;
if ( subnets && ! subnets->Remove(index) ) if ( subnets && ! subnets->Remove(index) )
reporter->InternalError( "index not in prefix table" ); reporter->InternalError("index not in prefix table");
if ( LoggingAccess() ) if ( LoggingAccess() )
{ {
@ -2160,7 +2160,7 @@ Val* TableVal::Delete(const HashKey* k)
{ {
Val* index = table_hash->RecoverVals(k); Val* index = table_hash->RecoverVals(k);
if ( ! subnets->Remove(index) ) if ( ! subnets->Remove(index) )
reporter->InternalError( "index not in prefix table" ); reporter->InternalError("index not in prefix table");
Unref(index); Unref(index);
} }
@ -2417,7 +2417,7 @@ void TableVal::DoExpire(double t)
{ {
Val* index = RecoverIndex(k); Val* index = RecoverIndex(k);
if ( ! subnets->Remove(index) ) if ( ! subnets->Remove(index) )
reporter->InternalError( "index not in prefix table" ); reporter->InternalError("index not in prefix table");
Unref(index); Unref(index);
} }

View file

@ -1855,7 +1855,7 @@ function decode_base64%(s: string%): string
return new StringVal(t); return new StringVal(t);
else else
{ {
reporter->Error("error in decoding string %s", @ARG@[0]); reporter->Error("error in decoding string %s", s->CheckString());
return new StringVal(""); return new StringVal("");
} }
%} %}
@ -1987,8 +1987,8 @@ function precompile_pcap_filter%(id: PcapFilterID, s: string%): bool
if ( ! pkt_srcs[i]->PrecompileFilter(id->ForceAsInt(), if ( ! pkt_srcs[i]->PrecompileFilter(id->ForceAsInt(),
s->CheckString()) ) s->CheckString()) )
{ {
reporter->Error( "precompile_pcap_filter: %s", reporter->Error("precompile_pcap_filter: %s",
pkt_srcs[i]->ErrorMsg() ); pkt_srcs[i]->ErrorMsg());
success = false; success = false;
} }
} }

View file

@ -951,7 +951,7 @@ int main(int argc, char** argv)
{ {
reporter->Info("invoked event handlers:"); reporter->Info("invoked event handlers:");
for ( int i = 0; i < alive_handlers->length(); ++i ) for ( int i = 0; i < alive_handlers->length(); ++i )
reporter->Info((*alive_handlers)[i]); reporter->Info("%s", (*alive_handlers)[i]);
} }
delete alive_handlers; delete alive_handlers;

View file

@ -283,7 +283,7 @@ uint32 mask_addr(uint32 a, uint32 top_bits_to_keep)
{ {
if ( top_bits_to_keep > 32 ) if ( top_bits_to_keep > 32 )
{ {
reporter->Error("bad address mask value %s", top_bits_to_keep); reporter->Error("bad address mask value %d", top_bits_to_keep);
return a; return a;
} }

View file

@ -1593,7 +1593,7 @@ resolve_id:
$$ = lookup_ID($1, current_module.c_str()); $$ = lookup_ID($1, current_module.c_str());
if ( ! $$ ) if ( ! $$ )
reporter->Error("identifier not defined:", $1); reporter->Error("identifier not defined: %s", $1);
delete [] $1; delete [] $1;
} }
@ -1650,7 +1650,7 @@ int yyerror(const char msg[])
strcat(msgbuf, "\nDocumentation mode is enabled: " strcat(msgbuf, "\nDocumentation mode is enabled: "
"remember to check syntax of ## style comments\n"); "remember to check syntax of ## style comments\n");
reporter->Error(msgbuf); reporter->Error("%s", msgbuf);
return 0; return 0;
} }

View file

@ -50,7 +50,7 @@ char last_tok[128];
// a read fails. // a read fails.
#define YY_INPUT(buf,result,max_size) \ #define YY_INPUT(buf,result,max_size) \
if ( ((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin) ) \ if ( ((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin) ) \
reporter->Error(fmt("read failed with \"%s\"", strerror(errno))); reporter->Error("read failed with \"%s\"", strerror(errno));
// reST documents that we've created (or have at least opened so far). // reST documents that we've created (or have at least opened so far).
std::list<BroDoc*> docs_generated; std::list<BroDoc*> docs_generated;
@ -408,7 +408,7 @@ F RET_CONST(new Val(false, TYPE_BOOL))
uint32 p = atoi(yytext); uint32 p = atoi(yytext);
if ( p > 65535 ) if ( p > 65535 )
{ {
reporter->Error("bad port number -", yytext); reporter->Error("bad port number - %s", yytext);
p = 0; p = 0;
} }
RET_CONST(new PortVal(p, TRANSPORT_TCP)) RET_CONST(new PortVal(p, TRANSPORT_TCP))
@ -417,7 +417,7 @@ F RET_CONST(new Val(false, TYPE_BOOL))
uint32 p = atoi(yytext); uint32 p = atoi(yytext);
if ( p > 65535 ) if ( p > 65535 )
{ {
reporter->Error("bad port number -", yytext); reporter->Error("bad port number - %s", yytext);
p = 0; p = 0;
} }
RET_CONST(new PortVal(p, TRANSPORT_UDP)) RET_CONST(new PortVal(p, TRANSPORT_UDP))
@ -426,7 +426,7 @@ F RET_CONST(new Val(false, TYPE_BOOL))
uint32 p = atoi(yytext); uint32 p = atoi(yytext);
if ( p > 255 ) if ( p > 255 )
{ {
reporter->Error("bad port number -", yytext); reporter->Error("bad port number - %s", yytext);
p = 0; p = 0;
} }
RET_CONST(new PortVal(p, TRANSPORT_ICMP)) RET_CONST(new PortVal(p, TRANSPORT_ICMP))
@ -435,7 +435,7 @@ F RET_CONST(new Val(false, TYPE_BOOL))
uint32 p = atoi(yytext); uint32 p = atoi(yytext);
if ( p > 255 ) if ( p > 255 )
{ {
reporter->Error("bad port number -", yytext); reporter->Error("bad port number - %s", yytext);
p = 0; p = 0;
} }
RET_CONST(new PortVal(p, TRANSPORT_UNKNOWN)) RET_CONST(new PortVal(p, TRANSPORT_UNKNOWN))
@ -499,7 +499,7 @@ F RET_CONST(new Val(false, TYPE_BOOL))
<RE>[/\\\n] return yytext[0]; <RE>[/\\\n] return yytext[0];
<*>. reporter->Error("unrecognized character -", yytext); <*>. reporter->Error("unrecognized character - %s", yytext);
<<EOF>> last_tok[0] = '\0'; return EOF; <<EOF>> last_tok[0] = '\0'; return EOF;