Skip to content

Commit 25ab8f8

Browse files
committed
Replace sprintf with snprintf
1 parent 95a4d87 commit 25ab8f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1490
-1495
lines changed

examples/dbcrypt/CryptApplication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class App
113113
throw "setDbCryptCallback";
114114

115115
char s[256];
116-
sprintf(s, "localhost:%s", dbName);
116+
snprintf(s, sizeof(s), "localhost:%s", dbName);
117117
att = p->attachDatabase(status, s, 0, NULL);
118118
if (status->getState() & IStatus::STATE_ERRORS)
119119
throw "attachDatabase";

examples/empbuild/empbuild.epp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ else
7474
/* Create the database */
7575

7676
printf ("creating database %s\n", Db_name);
77-
sprintf (cmd, "CREATE DATABASE \"%s\"", Db_name);
77+
snprintf(cmd, sizeof(cmd), "CREATE DATABASE \"%s\"", Db_name);
7878
gds_trans = 0;
7979

8080
EXEC SQL EXECUTE IMMEDIATE :cmd;
@@ -94,39 +94,39 @@ if (SQLCODE)
9494
}
9595

9696
printf ("Turning forced writes off\n");
97-
sprintf (cmd, "gfix -write async %s", Db_name);
97+
snprintf(cmd, sizeof(cmd), "gfix -write async %s", Db_name);
9898
if (system (cmd))
9999
{
100100
printf ("Couldn't turn forced writes off\n");
101101
exit (FINI_ERROR);
102102
}
103103

104104
printf ("Creating tables\n");
105-
sprintf (cmd, "isql %s -q -i empddl.sql", Db_name);
105+
snprintf(cmd, sizeof(cmd), "isql %s -q -i empddl.sql", Db_name);
106106
if (system (cmd))
107107
{
108108
printf ("Couldn't create tables \n");
109109
exit (FINI_ERROR);
110110
}
111111

112112
printf ("Turning off indices and triggers \n");
113-
sprintf (cmd, "isql %s -i indexoff.sql", Db_name);
113+
snprintf(cmd, sizeof(cmd), "isql %s -i indexoff.sql", Db_name);
114114
if (system (cmd))
115115
{
116116
printf ("Couldn't turn off indices and triggers \n");
117117
exit (FINI_ERROR);
118118
}
119119

120120
printf ("Loading column data\n");
121-
sprintf (cmd, "isql %s -i empdml.sql", Db_name);
121+
snprintf(cmd, sizeof(cmd), "isql %s -i empdml.sql", Db_name);
122122
if (system (cmd))
123123
{
124124
printf ("Couldn't load column data \n");
125125
exit (FINI_ERROR);
126126
}
127127

128128
printf ("Turning on indices and triggers \n");
129-
sprintf (cmd, "isql %s -i indexon.sql", Db_name);
129+
snprintf(cmd, sizeof(cmd), "isql %s -i indexon.sql", Db_name);
130130
if (system (cmd))
131131
{
132132
printf ("Couldn't turn on indices and triggers \n");

examples/extauth/TcWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void check(ThrowStatusWrapper* status, int err, const char* text)
5858
return;
5959

6060
char buf[256];
61-
sprintf(buf, "%s: %s", text, error_to_string(err));
61+
snprintf(buf, sizeof(buf), "%s: %s", text, error_to_string(err));
6262
error(status, buf);
6363
}
6464

examples/interfaces/04.print_table.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ int main()
106106

107107
default:
108108
{
109-
sprintf(s, "Unknown type %d for %s", t, meta->getField(&status, j));
109+
snprintf(s, sizeof(s), "Unknown type %d for %s", t, meta->getField(&status, j));
110110
throw s;
111111
}
112112
continue;

examples/replication/fbSampleReplicator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static const ISC_STATUS wrn[] = { isc_arg_gds, isc_random, isc_arg_string, (ISC_
124124
ReplPlugin::ReplPlugin(IPluginConfig* conf)
125125
{
126126
char fn[100];
127-
sprintf(fn, "session_%08x_%d.log", (unsigned)time(nullptr), logCounter++);
127+
snprintf(fn, sizeof(fn), "session_%08x_%d.log", (unsigned)time(nullptr), logCounter++);
128128
log = fopen(fn, "w");
129129
WriteLog(log, "%p\tReplicatedSession constructed\n", this);
130130
status = master->getStatus();

examples/udr/Triggers.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ FB_UDR_BEGIN_TRIGGER(replicate)
135135
const char* name = triggerMetadata->getField(status, i);
136136

137137
strcat(buffer, " p");
138-
sprintf(buffer + strlen(buffer), "%d type of column \"%s\".\"%s\" = ?", i, table, name);
138+
const size_t buflen = strlen(buffer);
139+
snprintf(buffer + buflen, sizeof(buffer) - buflen, "%d type of column \"%s\".\"%s\" = ?", i, table, name);
139140
}
140141

141142
strcat(buffer,
@@ -175,7 +176,8 @@ FB_UDR_BEGIN_TRIGGER(replicate)
175176
if (i > 0)
176177
strcat(buffer, ", ");
177178
strcat(buffer, ":p");
178-
sprintf(buffer + strlen(buffer), "%d", i);
179+
const size_t buflen = strlen(buffer);
180+
snprintf(buffer + buflen, sizeof(buffer) - buflen, "%d", i);
179181
}
180182

181183
strcat(buffer, ")\n on external data source '");

src/burp/mvol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,7 @@ bool MVOL_split_hdr_write()
21192119
time_t seconds = time(NULL);
21202120

21212121
Firebird::string nm = tdgbl->toSystem(tdgbl->action->act_file->fil_name);
2122-
sprintf(buffer, "%s%.24s , file No. %4d of %4d, %-27.27s",
2122+
snprintf(buffer, sizeof(buffer), "%s%.24s , file No. %4d of %4d, %-27.27s",
21232123
HDR_SPLIT_TAG, ctime(&seconds), tdgbl->action->act_file->fil_seq,
21242124
tdgbl->action->act_total, nm.c_str());
21252125

src/common/DecFloat.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ Decimal64 Decimal64::set(Int128 value, DecimalStatus decSt, int scale)
386386
Decimal64 Decimal64::set(SINT64 value, DecimalStatus decSt, int scale)
387387
{
388388
{
389-
char s[30]; // for sure enough for int64
390-
sprintf(s, "%" SQUADFORMAT, value);
389+
char s[30];
390+
snprintf(s, sizeof(s), "%" SQUADFORMAT, value);
391391
DecimalContext context(this, decSt);
392392
decDoubleFromString(&dec, s, &context);
393393
}
@@ -408,7 +408,7 @@ Decimal64 Decimal64::set(const char* value, DecimalStatus decSt)
408408
Decimal64 Decimal64::set(double value, DecimalStatus decSt)
409409
{
410410
char s[50];
411-
sprintf(s, "%.016e", value);
411+
snprintf(s, sizeof(s), "%.016e", value);
412412
DecimalContext context(this, decSt);
413413
decDoubleFromString(&dec, s, &context);
414414

@@ -685,7 +685,7 @@ Decimal128 Decimal128::set(const char* value, DecimalStatus decSt)
685685
Decimal128 Decimal128::set(double value, DecimalStatus decSt)
686686
{
687687
char s[50];
688-
sprintf(s, "%.016e", value);
688+
snprintf(s, sizeof(s), "%.016e", value);
689689
DecimalContext context(this, decSt);
690690
decQuadFromString(&dec, s, &context);
691691

src/common/SimilarToRegex.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ namespace
501501

502502
if (c > 0)
503503
{
504-
sprintf(hex, "\\x00-\\x{%X}", (int) c - 1);
504+
snprintf(hex, sizeof(hex), "\\x00-\\x{%X}", (int) c - 1);
505505
re2PatternStr.append(hex);
506506
}
507507

@@ -510,7 +510,7 @@ namespace
510510

511511
if (c < maxChar)
512512
{
513-
sprintf(hex, "\\x{%X}-\\x{%X}", (int) c + 1, maxChar);
513+
snprintf(hex, sizeof(hex), "\\x{%X}-\\x{%X}", (int) c + 1, maxChar);
514514
re2PatternStr.append(hex);
515515
}
516516
}
@@ -572,7 +572,7 @@ namespace
572572
else if (invalidInclude)
573573
{
574574
char str[30];
575-
sprintf(str, "[^\\x{0}-\\x{%X}]", maxChar);
575+
snprintf(str, sizeof(str), "[^\\x{0}-\\x{%X}]", maxChar);
576576
re2PatternStr.append(str);
577577
}
578578
else

src/common/StatusArg.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,19 +407,19 @@ Num::Num(ISC_STATUS s) noexcept :
407407
Int64::Int64(SINT64 val) noexcept :
408408
Str(text)
409409
{
410-
sprintf(text, "%" SQUADFORMAT, val);
410+
snprintf(text, sizeof(text), "%" SQUADFORMAT, val);
411411
}
412412

413413
Int64::Int64(FB_UINT64 val) noexcept :
414414
Str(text)
415415
{
416-
sprintf(text, "%" UQUADFORMAT, val);
416+
snprintf(text, sizeof(text), "%" UQUADFORMAT, val);
417417
}
418418

419419
Quad::Quad(const ISC_QUAD* quad) noexcept :
420420
Str(text)
421421
{
422-
sprintf(text, "%x:%x", quad->gds_quad_high, quad->gds_quad_low);
422+
snprintf(text, sizeof(text), "%x:%x", quad->gds_quad_high, quad->gds_quad_low);
423423
}
424424

425425
Interpreted::Interpreted(const char* text) noexcept :

0 commit comments

Comments
 (0)