Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/timw/sprintf-warnings'
Browse files Browse the repository at this point in the history
* origin/topic/timw/sprintf-warnings:
  Fix sprintf warnings on macOS
  • Loading branch information
timwoj committed Jan 3, 2023
2 parents 35f5646 + 797b765 commit a778e31
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
5.2.0-dev.447 | 2023-01-03 15:44:39 -0700

* Fix sprintf warnings on macOS (Tim Wojtulewicz, Corelight)

The most recent compiler update for macOS marked sprintf as
deprecated, so we started getting warnings from all of the places
that use it.

5.2.0-dev.445 | 2023-01-03 12:10:20 -0700

* Update COPYING to 2023 (Tim Wojtulewicz, Corelight)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.2.0-dev.445
5.2.0-dev.447
9 changes: 6 additions & 3 deletions src/DFA.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,17 @@ void DFA_State::Dump(FILE* f, DFA_Machine* m)
if ( xtions[i] != s )
break;

char xbuf[512];
constexpr int xbuf_size = 512;
char* xbuf = new char[xbuf_size];

int r = m->Rep(sym);
if ( ! r )
r = '.';

if ( i == sym + 1 )
sprintf(xbuf, "'%c'", r);
snprintf(xbuf, xbuf_size, "'%c'", r);
else
sprintf(xbuf, "'%c'-'%c'", r, m->Rep(i - 1));
snprintf(xbuf, xbuf_size, "'%c'-'%c'", r, m->Rep(i - 1));

if ( s == DFA_UNCOMPUTED_STATE_PTR )
fprintf(f, "%stransition on %s to <uncomputed>", ++num_trans == 1 ? "\t" : "\n\t",
Expand All @@ -249,6 +250,8 @@ void DFA_State::Dump(FILE* f, DFA_Machine* m)
fprintf(f, "%stransition on %s to state %d", ++num_trans == 1 ? "\t" : "\n\t", xbuf,
s->StateNum());

delete[] xbuf;

sym = i - 1;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ template <typename T> class Dictionary
if ( binary )
{
char key = char(random() % 26) + 'A';
sprintf(key_file, "%d.%d-%c.key", Length(), max_distance, key);
snprintf(key_file, 100, "%d.%d-%c.key", Length(), max_distance, key);
std::ofstream f(key_file, std::ios::binary | std::ios::out | std::ios::trunc);
for ( int idx = 0; idx < Capacity(); idx++ )
if ( ! table[idx].Empty() )
Expand All @@ -1005,7 +1005,7 @@ template <typename T> class Dictionary
else
{
char key = char(random() % 26) + 'A';
sprintf(key_file, "%d.%d-%d.ckey", Length(), max_distance, key);
snprintf(key_file, 100, "%d.%d-%d.ckey", Length(), max_distance, key);
std::ofstream f(key_file, std::ios::out | std::ios::trunc);
for ( int idx = 0; idx < Capacity(); idx++ )
if ( ! table[idx].Empty() )
Expand Down
2 changes: 1 addition & 1 deletion src/ZeekString.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ char* String::Render(int format, int* len) const

*sp++ = '\\';
*sp++ = 'x';
sprintf(hex_fmt, "%02x", b[i]);
snprintf(hex_fmt, 16, "%02x", b[i]);
*sp++ = hex_fmt[0];
*sp++ = hex_fmt[1];
}
Expand Down
2 changes: 1 addition & 1 deletion src/strings.bif
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ function string_to_ascii_hex%(s: string%): string
const u_char* sp = s->Bytes();
for ( int i = 0; i < s->Len(); ++i )
sprintf(x + i * 2, "%02x", sp[i]);
snprintf(x + i * 2, 3, "%02x", sp[i]);
return zeek::make_intrusive<zeek::StringVal>(new zeek::String(1, (u_char*) x, s->Len() * 2));
%}
Expand Down
5 changes: 3 additions & 2 deletions src/zeek.bif
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,10 @@ static bool prepare_environment(zeek::TableVal* tbl, bool set)
static int do_system(const char* s)
{
const char* system_fmt = "(%s) 1>&2 &"; // output to stderr
char* cmd = new char[strlen(system_fmt) + strlen(s) + 1];
auto cmd_len = strlen(system_fmt) + strlen(s) + 1;
char* cmd = new char[cmd_len];

sprintf(cmd, system_fmt, s);
snprintf(cmd, cmd_len, system_fmt, s);
int status = system(cmd);
delete [] cmd;

Expand Down

0 comments on commit a778e31

Please sign in to comment.