Mark safe_snprintf and safe_vsnprintf as deprecated, remove uses of them

safe_snprintf and safe_vsnprintf just exist to ensure that the resulting strings are always null-terminated. The documentation for snprintf/vsnprintf states that the output of those methods are always null-terminated, thus making the safe versions obsolete.
This commit is contained in:
Tim Wojtulewicz 2019-12-18 15:58:35 -07:00
parent 6a52857f8f
commit 67fcc9b5af
16 changed files with 31 additions and 29 deletions

View file

@ -135,7 +135,7 @@ bool DbgBreakpoint::SetLocation(ParseLocationRec plr, string loc_str)
} }
at_stmt = plr.stmt; at_stmt = plr.stmt;
safe_snprintf(description, sizeof(description), "%s:%d", snprintf(description, sizeof(description), "%s:%d",
source_filename, source_line); source_filename, source_line);
debug_msg("Breakpoint %d set at %s\n", GetID(), Description()); debug_msg("Breakpoint %d set at %s\n", GetID(), Description());
@ -148,7 +148,7 @@ bool DbgBreakpoint::SetLocation(ParseLocationRec plr, string loc_str)
loc_str.c_str()); loc_str.c_str());
at_stmt = plr.stmt; at_stmt = plr.stmt;
const Location* loc = at_stmt->GetLocationInfo(); const Location* loc = at_stmt->GetLocationInfo();
safe_snprintf(description, sizeof(description), "%s at %s:%d", snprintf(description, sizeof(description), "%s at %s:%d",
function_name.c_str(), loc->filename, loc->last_line); function_name.c_str(), loc->filename, loc->last_line);
debug_msg("Breakpoint %d set at %s\n", GetID(), Description()); debug_msg("Breakpoint %d set at %s\n", GetID(), Description());
@ -171,7 +171,7 @@ bool DbgBreakpoint::SetLocation(Stmt* stmt)
AddToGlobalMap(); AddToGlobalMap();
const Location* loc = stmt->GetLocationInfo(); const Location* loc = stmt->GetLocationInfo();
safe_snprintf(description, sizeof(description), "%s:%d", snprintf(description, sizeof(description), "%s:%d",
loc->filename, loc->last_line); loc->filename, loc->last_line);
debug_msg("Breakpoint %d set at %s\n", GetID(), Description()); debug_msg("Breakpoint %d set at %s\n", GetID(), Description());

View file

@ -717,7 +717,7 @@ static char* get_prompt(bool reset_counter = false)
if ( reset_counter ) if ( reset_counter )
counter = 0; counter = 0;
safe_snprintf(prompt, sizeof(prompt), "(Zeek [%d]) ", counter++); snprintf(prompt, sizeof(prompt), "(Zeek [%d]) ", counter++);
return prompt; return prompt;
} }
@ -743,7 +743,7 @@ string get_context_description(const Stmt* stmt, const Frame* frame)
size_t buf_size = strlen(d.Description()) + strlen(loc.filename) + 1024; size_t buf_size = strlen(d.Description()) + strlen(loc.filename) + 1024;
char* buf = new char[buf_size]; char* buf = new char[buf_size];
safe_snprintf(buf, buf_size, "In %s at %s:%d", snprintf(buf, buf_size, "In %s at %s:%d",
d.Description(), loc.filename, loc.last_line); d.Description(), loc.filename, loc.last_line);
string retval(buf); string retval(buf);

View file

@ -109,7 +109,7 @@ void Specific_RE_Matcher::MakeCaseInsensitive()
char* s = new char[n + 5 /* slop */]; char* s = new char[n + 5 /* slop */];
safe_snprintf(s, n + 5, fmt, pattern_text); snprintf(s, n + 5, fmt, pattern_text);
delete [] pattern_text; delete [] pattern_text;
pattern_text = s; pattern_text = s;
@ -493,7 +493,7 @@ static RE_Matcher* matcher_merge(const RE_Matcher* re1, const RE_Matcher* re2,
int n = strlen(text1) + strlen(text2) + strlen(merge_op) + 32 /* slop */ ; int n = strlen(text1) + strlen(text2) + strlen(merge_op) + 32 /* slop */ ;
char* merge_text = new char[n]; char* merge_text = new char[n];
safe_snprintf(merge_text, n, "(%s)%s(%s)", text1, merge_op, text2); snprintf(merge_text, n, "(%s)%s(%s)", text1, merge_op, text2);
RE_Matcher* merge = new RE_Matcher(merge_text); RE_Matcher* merge = new RE_Matcher(merge_text);
delete [] merge_text; delete [] merge_text;

View file

@ -430,7 +430,7 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out,
{ {
va_list aq; va_list aq;
va_copy(aq, ap); va_copy(aq, ap);
int n = safe_vsnprintf(buffer, size, fmt, aq); int n = vsnprintf(buffer, size, fmt, aq);
va_end(aq); va_end(aq);
if ( postfix ) if ( postfix )
@ -451,7 +451,7 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out,
if ( postfix && *postfix ) if ( postfix && *postfix )
// Note, if you change this fmt string, adjust the additional // Note, if you change this fmt string, adjust the additional
// buffer size above. // buffer size above.
safe_snprintf(buffer + strlen(buffer), size - strlen(buffer), " (%s)", postfix); snprintf(buffer + strlen(buffer), size - strlen(buffer), " (%s)", postfix);
bool raise_event = true; bool raise_event = true;

View file

@ -2584,7 +2584,7 @@ RecordVal* RecordVal::CoerceTo(const RecordType* t, Val* aggr, bool allow_orphan
continue; continue;
char buf[512]; char buf[512];
safe_snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
"orphan field \"%s\" in initialization", "orphan field \"%s\" in initialization",
rv_t->FieldName(i)); rv_t->FieldName(i));
Error(buf); Error(buf);
@ -2614,7 +2614,7 @@ RecordVal* RecordVal::CoerceTo(const RecordType* t, Val* aggr, bool allow_orphan
! ar_t->FieldDecl(i)->FindAttr(ATTR_OPTIONAL) ) ! ar_t->FieldDecl(i)->FindAttr(ATTR_OPTIONAL) )
{ {
char buf[512]; char buf[512];
safe_snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
"non-optional field \"%s\" missing in initialization", ar_t->FieldName(i)); "non-optional field \"%s\" missing in initialization", ar_t->FieldName(i));
Error(buf); Error(buf);
} }

View file

@ -889,7 +889,7 @@ void SMTP_Analyzer::UnexpectedCommand(const int cmd_code, const int reply_code)
// If this happens, please fix the SMTP state machine! // If this happens, please fix the SMTP state machine!
// ### Eventually, these should be turned into "weird" events. // ### Eventually, these should be turned into "weird" events.
static char buf[512]; static char buf[512];
int len = safe_snprintf(buf, sizeof(buf), int len = snprintf(buf, sizeof(buf),
"%s reply = %d state = %d", "%s reply = %d state = %d",
SMTP_CMD_WORD(cmd_code), reply_code, state); SMTP_CMD_WORD(cmd_code), reply_code, state);
if ( len > (int) sizeof(buf) ) if ( len > (int) sizeof(buf) )
@ -902,7 +902,7 @@ void SMTP_Analyzer::UnexpectedReply(const int cmd_code, const int reply_code)
// If this happens, please fix the SMTP state machine! // If this happens, please fix the SMTP state machine!
// ### Eventually, these should be turned into "weird" events. // ### Eventually, these should be turned into "weird" events.
static char buf[512]; static char buf[512];
int len = safe_snprintf(buf, sizeof(buf), int len = snprintf(buf, sizeof(buf),
"%d state = %d, last command = %s", "%d state = %d, last command = %s",
reply_code, state, SMTP_CMD_WORD(cmd_code)); reply_code, state, SMTP_CMD_WORD(cmd_code));
Unexpected (1, "unexpected reply", len, buf); Unexpected (1, "unexpected reply", len, buf);

View file

@ -71,7 +71,7 @@ void TCPStateStats::PrintStats(BroFile* file, const char* prefix)
if ( n > 0 ) if ( n > 0 )
{ {
char buf[32]; char buf[32];
safe_snprintf(buf, sizeof(buf), "%-8d", state_cnt[i][j]); snprintf(buf, sizeof(buf), "%-8d", state_cnt[i][j]);
file->Write(buf); file->Write(buf);
} }
else else

View file

@ -85,7 +85,7 @@ bool BPF_Program::Compile(pcap_t* pcap, const char* filter, uint32_t netmask,
if ( pcap_compile(pcap, &m_program, (char *) filter, optimize, netmask) < 0 ) if ( pcap_compile(pcap, &m_program, (char *) filter, optimize, netmask) < 0 )
{ {
if ( errbuf ) if ( errbuf )
safe_snprintf(errbuf, errbuf_len, snprintf(errbuf, errbuf_len,
"pcap_compile(%s): %s", filter, "pcap_compile(%s): %s", filter,
pcap_geterr(pcap)); pcap_geterr(pcap));

View file

@ -93,7 +93,7 @@ void PktSrc::Opened(const Properties& arg_props)
if ( Packet::GetLinkHeaderSize(arg_props.link_type) < 0 ) if ( Packet::GetLinkHeaderSize(arg_props.link_type) < 0 )
{ {
char buf[512]; char buf[512];
safe_snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
"unknown data link type 0x%x", arg_props.link_type); "unknown data link type 0x%x", arg_props.link_type);
Error(buf); Error(buf);
Close(); Close();

View file

@ -62,7 +62,7 @@ void PcapSource::OpenLive()
if ( pcap_findalldevs(&devs, tmp_errbuf) < 0 ) if ( pcap_findalldevs(&devs, tmp_errbuf) < 0 )
{ {
safe_snprintf(errbuf, sizeof(errbuf), snprintf(errbuf, sizeof(errbuf),
"pcap_findalldevs: %s", tmp_errbuf); "pcap_findalldevs: %s", tmp_errbuf);
Error(errbuf); Error(errbuf);
return; return;
@ -75,7 +75,7 @@ void PcapSource::OpenLive()
if ( props.path.empty() ) if ( props.path.empty() )
{ {
safe_snprintf(errbuf, sizeof(errbuf), snprintf(errbuf, sizeof(errbuf),
"pcap_findalldevs: empty device name"); "pcap_findalldevs: empty device name");
Error(errbuf); Error(errbuf);
return; return;
@ -83,7 +83,7 @@ void PcapSource::OpenLive()
} }
else else
{ {
safe_snprintf(errbuf, sizeof(errbuf), snprintf(errbuf, sizeof(errbuf),
"pcap_findalldevs: no devices found"); "pcap_findalldevs: no devices found");
Error(errbuf); Error(errbuf);
return; return;
@ -263,7 +263,7 @@ bool PcapSource::SetFilter(int index)
if ( ! code ) if ( ! code )
{ {
safe_snprintf(errbuf, sizeof(errbuf), snprintf(errbuf, sizeof(errbuf),
"No precompiled pcap filter for index %d", "No precompiled pcap filter for index %d",
index); index);
Error(errbuf); Error(errbuf);

View file

@ -136,7 +136,7 @@ const char* fmt_conn_id(const IPAddr& src_addr, uint32_t src_port,
{ {
static char buffer[512]; static char buffer[512];
safe_snprintf(buffer, sizeof(buffer), "%s:%d > %s:%d", snprintf(buffer, sizeof(buffer), "%s:%d > %s:%d",
string(src_addr).c_str(), src_port, string(src_addr).c_str(), src_port,
string(dst_addr).c_str(), dst_port); string(dst_addr).c_str(), dst_port);

View file

@ -197,7 +197,7 @@ finger { rules_lval.val = Rule::FINGER; return TOK_PATTERN_TYPE; }
const char fmt[] = "(?i:%s)"; const char fmt[] = "(?i:%s)";
int n = len + strlen(fmt); int n = len + strlen(fmt);
char* s = new char[n + 5 /* slop */]; char* s = new char[n + 5 /* slop */];
safe_snprintf(s, n + 5, fmt, yytext + 1); snprintf(s, n + 5, fmt, yytext + 1);
rules_lval.str = s; rules_lval.str = s;
} }
else else

View file

@ -1096,7 +1096,7 @@ function hexdump%(data_str: string%) : string
if ( x == 0 ) if ( x == 0 )
{ {
char offset[5]; char offset[5];
safe_snprintf(offset, sizeof(offset), snprintf(offset, sizeof(offset),
"%.4x", data_ptr - data); "%.4x", data_ptr - data);
memcpy(hex_data_ptr, offset, 4); memcpy(hex_data_ptr, offset, 4);
hex_data_ptr += 6; hex_data_ptr += 6;
@ -1104,7 +1104,7 @@ function hexdump%(data_str: string%) : string
} }
char hex_byte[3]; char hex_byte[3];
safe_snprintf(hex_byte, sizeof(hex_byte), snprintf(hex_byte, sizeof(hex_byte),
"%.2x", (u_char) *data_ptr); "%.2x", (u_char) *data_ptr);
int val = (u_char) *data_ptr; int val = (u_char) *data_ptr;

View file

@ -79,7 +79,7 @@ const char* BasicThread::Fmt(const char* format, ...)
va_list al; va_list al;
va_start(al, format); va_start(al, format);
int n = safe_vsnprintf(buf, buf_len, format, al); int n = vsnprintf(buf, buf_len, format, al);
va_end(al); va_end(al);
if ( (unsigned int) n >= buf_len ) if ( (unsigned int) n >= buf_len )
@ -89,7 +89,7 @@ const char* BasicThread::Fmt(const char* format, ...)
// Is it portable to restart? // Is it portable to restart?
va_start(al, format); va_start(al, format);
n = safe_vsnprintf(buf, buf_len, format, al); n = vsnprintf(buf, buf_len, format, al);
va_end(al); va_end(al);
} }

View file

@ -752,14 +752,14 @@ const char* fmt(const char* format, va_list al)
va_list alc; va_list alc;
va_copy(alc, al); va_copy(alc, al);
int n = safe_vsnprintf(buf, buf_len, format, al); int n = vsnprintf(buf, buf_len, format, al);
if ( (unsigned int) n >= buf_len ) if ( (unsigned int) n >= buf_len )
{ // Not enough room, grow the buffer. { // Not enough room, grow the buffer.
buf_len = n + 32; buf_len = n + 32;
buf = (char*) safe_realloc(buf, buf_len); buf = (char*) safe_realloc(buf, buf_len);
n = safe_vsnprintf(buf, buf_len, format, alc); n = vsnprintf(buf, buf_len, format, alc);
if ( (unsigned int) n >= buf_len ) if ( (unsigned int) n >= buf_len )
reporter->InternalError("confusion reformatting in fmt()"); reporter->InternalError("confusion reformatting in fmt()");
@ -1638,7 +1638,7 @@ FILE* rotate_file(const char* name, RecordVal* rotate_info)
char newname[buflen], tmpname[buflen+4]; char newname[buflen], tmpname[buflen+4];
safe_snprintf(newname, buflen, "%s.%d.%.06f.tmp", snprintf(newname, buflen, "%s.%d.%.06f.tmp",
name, getpid(), network_time); name, getpid(), network_time);
newname[buflen-1] = '\0'; newname[buflen-1] = '\0';
strcpy(tmpname, newname); strcpy(tmpname, newname);

View file

@ -510,6 +510,7 @@ inline char* safe_strncpy(char* dest, const char* src, size_t n)
return result; return result;
} }
ZEEK_DEPRECATED("Remove in v4.1: Use system snprintf instead")
inline int safe_snprintf(char* str, size_t size, const char* format, ...) inline int safe_snprintf(char* str, size_t size, const char* format, ...)
{ {
va_list al; va_list al;
@ -521,6 +522,7 @@ inline int safe_snprintf(char* str, size_t size, const char* format, ...)
return result; return result;
} }
ZEEK_DEPRECATED("Remove in v4.1: Use system vsnprintf instead")
inline int safe_vsnprintf(char* str, size_t size, const char* format, va_list al) inline int safe_vsnprintf(char* str, size_t size, const char* format, va_list al)
{ {
int result = vsnprintf(str, size, format, al); int result = vsnprintf(str, size, format, al);