Skip to content

Commit 90f5c0e

Browse files
committed
Using snprintf instead of sprintf for a tools
[764/770] Building CXX object tools/quantize/CMakeFiles/ncnn2int8.dir/ncnn2int8.cpp.o ../tools/quantize/ncnn2int8.cpp:153:9: warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations] 153 | sprintf(key, "%s_param_0", layers[i]->name.c_str()); | ^ Signed-off-by: Evgeny Proydakov <e.proydakov@gmail.com>
1 parent a64aa7f commit 90f5c0e

File tree

10 files changed

+86
-85
lines changed

10 files changed

+86
-85
lines changed

src/datareader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ int DataReaderFromMemory::scan(const char* format, void* p) const
111111
{
112112
size_t fmtlen = strlen(format);
113113

114-
char* format_with_n = new char[fmtlen + 4];
115-
sprintf(format_with_n, "%s%%n", format);
114+
const size_t nlen = fmtlen + 4;
115+
char* format_with_n = new char[nlen];
116+
snprintf(format_with_n, nlen, "%s%%n", format);
116117

117118
int nconsumed = 0;
118119
int nscan = sscanf((const char*)d->mem, format_with_n, p, &nconsumed);

tools/caffe/caffe2ncnn.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int main(int argc, char** argv)
240240
bottom_reference[blob_name] = refidx;
241241

242242
char splitsuffix[256];
243-
sprintf(splitsuffix, "_splitncnn_%d", refidx);
243+
snprintf(splitsuffix, 256, "_splitncnn_%d", refidx);
244244
blob_name = blob_name + splitsuffix;
245245
}
246246

@@ -1127,7 +1127,7 @@ int main(int argc, char** argv)
11271127
if (refcount > 1)
11281128
{
11291129
char splitname[256];
1130-
sprintf(splitname, "splitncnn_%d", internal_split);
1130+
snprintf(splitname, 256, "splitncnn_%d", internal_split);
11311131
fprintf(pp, "%-16s %-16s %d %d", "Split", splitname, 1, refcount);
11321132
fprintf(pp, " %s", blob_name.c_str());
11331133

@@ -1152,7 +1152,7 @@ int main(int argc, char** argv)
11521152
if (refcount > 1)
11531153
{
11541154
char splitname[256];
1155-
sprintf(splitname, "splitncnn_%d", internal_split);
1155+
snprintf(splitname, 256, "splitncnn_%d", internal_split);
11561156
fprintf(pp, "%-16s %-16s %d %d", "Split", splitname, 1, refcount);
11571157
fprintf(pp, " %s", blob_name.c_str());
11581158

tools/mlir/mlir2ncnn.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ int main(int argc, char** argv)
539539
}
540540

541541
char splitname[256];
542-
sprintf(splitname, "splitncnn_%d", internal_split);
542+
snprintf(splitname, 256, "splitncnn_%d", internal_split);
543543
fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount);
544544

545545
fprintf(pp, " %s", input_name.c_str());
@@ -768,7 +768,7 @@ int main(int argc, char** argv)
768768
}
769769

770770
char opid_name[64];
771-
sprintf(opid_name, "op_%d", opid);
771+
snprintf(opid_name, 64, "op_%d", opid);
772772

773773
fprintf(pp, " %-24s %d %d", opid_name, num_input, num_output);
774774

@@ -788,7 +788,7 @@ int main(int argc, char** argv)
788788
split_node_reference[input_name] = refidx;
789789

790790
char splitsuffix[256];
791-
sprintf(splitsuffix, "_splitncnn_%d", refidx);
791+
snprintf(splitsuffix, 256, "_splitncnn_%d", refidx);
792792
input_name = input_name + splitsuffix;
793793
}
794794

@@ -1784,7 +1784,7 @@ int main(int argc, char** argv)
17841784
if (refcount > 1)
17851785
{
17861786
char splitname[256];
1787-
sprintf(splitname, "splitncnn_%d", internal_split);
1787+
snprintf(splitname, 256, "splitncnn_%d", internal_split);
17881788
fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount);
17891789

17901790
fprintf(pp, " %s", output_name.c_str());

tools/mxnet/mxnet2ncnn.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ static bool read_mxnet_json(const char* jsonpath, std::vector<MXNetNode>& nodes)
428428
{
429429
// assign default unknown name
430430
char unknownname[256];
431-
sprintf(unknownname, "unknownncnn_%d", internal_unknown);
431+
snprintf(unknownname, 256, "unknownncnn_%d", internal_unknown);
432432

433433
n.name = unknownname;
434434

@@ -438,7 +438,7 @@ static bool read_mxnet_json(const char* jsonpath, std::vector<MXNetNode>& nodes)
438438
{
439439
// workaround for potential duplicated _plus0
440440
char underscorename[256];
441-
sprintf(underscorename, "underscorencnn_%d%s", internal_underscore, n.name.c_str());
441+
snprintf(underscorename, 256, "underscorencnn_%d%s", internal_underscore, n.name.c_str());
442442

443443
n.name = underscorename;
444444

@@ -857,7 +857,7 @@ static void fuse_shufflechannel(std::vector<MXNetNode>& nodes, std::vector<MXNet
857857
new_node.name = n3.name;
858858
new_node.output_size = n3.output_size;
859859
char group[16];
860-
sprintf(group, "%d", shape[2]);
860+
snprintf(group, 16, "%d", shape[2]);
861861
new_node.attrs["group"] = group;
862862
new_node.inputs = n.inputs;
863863
new_node.subinputs = n.subinputs;
@@ -914,8 +914,8 @@ static void fuse_hardsigmoid_hardswish(std::vector<MXNetNode>& nodes, std::vecto
914914
new_node.name = n2.name;
915915
new_node.output_size = n2.output_size;
916916
char alpha[16], beta[16];
917-
sprintf(alpha, "%f", 1.f / 6.f);
918-
sprintf(beta, "%f", 3.f / 6.f);
917+
snprintf(alpha, 16, "%f", 1.f / 6.f);
918+
snprintf(beta, 16, "%f", 3.f / 6.f);
919919
new_node.attrs["alpha"] = alpha;
920920
new_node.attrs["beta"] = beta;
921921
new_node.inputs = n.inputs;
@@ -940,8 +940,8 @@ static void fuse_hardsigmoid_hardswish(std::vector<MXNetNode>& nodes, std::vecto
940940
new_node.name = n3.name;
941941
new_node.output_size = n3.output_size;
942942
char alpha[16], beta[16];
943-
sprintf(alpha, "%f", 1.f / 6.f);
944-
sprintf(beta, "%f", 3.f / 6.f);
943+
snprintf(alpha, 16, "%f", 1.f / 6.f);
944+
snprintf(beta, 16, "%f", 3.f / 6.f);
945945
new_node.attrs["alpha"] = alpha;
946946
new_node.attrs["beta"] = beta;
947947
new_node.inputs = n.inputs;
@@ -1004,7 +1004,7 @@ int main(int argc, char** argv)
10041004

10051005
// non-unique name detected, append index as suffix
10061006
char suffix[32];
1007-
sprintf(suffix, "_%d", (int)i);
1007+
snprintf(suffix, 32, "_%d", (int)i);
10081008
n.name = n.name + std::string(suffix);
10091009
}
10101010
}
@@ -1094,7 +1094,7 @@ int main(int argc, char** argv)
10941094
if (subinput_index != 0)
10951095
{
10961096
char subinputsuffix[256];
1097-
sprintf(subinputsuffix, "_subncnn_%d", subinput_index);
1097+
snprintf(subinputsuffix, 256, "_subncnn_%d", subinput_index);
10981098
input_name = input_name + subinputsuffix;
10991099
}
11001100

@@ -1118,7 +1118,7 @@ int main(int argc, char** argv)
11181118
for (int j = 1; j < n.output_size; j++)
11191119
{
11201120
char subinputsuffix[256];
1121-
sprintf(subinputsuffix, "_%d", j);
1121+
snprintf(subinputsuffix, 256, "_%d", j);
11221122
std::string output_name_j = output_name + subinputsuffix;
11231123
blob_names.insert(output_name_j);
11241124
}
@@ -1576,7 +1576,7 @@ int main(int argc, char** argv)
15761576
if (subinput_index != 0)
15771577
{
15781578
char subinputsuffix[256];
1579-
sprintf(subinputsuffix, "_subncnn_%d", subinput_index);
1579+
snprintf(subinputsuffix, 256, "_subncnn_%d", subinput_index);
15801580
input_name = input_name + subinputsuffix;
15811581
}
15821582

@@ -1587,7 +1587,7 @@ int main(int argc, char** argv)
15871587
node_reference[input_uid] = refidx;
15881588

15891589
char splitsuffix[256];
1590-
sprintf(splitsuffix, "_splitncnn_%d", refidx);
1590+
snprintf(splitsuffix, 256, "_splitncnn_%d", refidx);
15911591
input_name = input_name + splitsuffix;
15921592
}
15931593

@@ -2735,7 +2735,7 @@ int main(int argc, char** argv)
27352735
std::string output_name = n.name;
27362736

27372737
char splitname[256];
2738-
sprintf(splitname, "splitncnn_%d", internal_split);
2738+
snprintf(splitname, 256, "splitncnn_%d", internal_split);
27392739
fprintf(pp, "%-16s %-32s %d %d", "Split", splitname, 1, refcount);
27402740
if (j == 0)
27412741
{

tools/onnx/onnx2ncnn.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,7 +3464,7 @@ For more information, please refer to https://github.com/pnnx/pnnx\n");
34643464
}
34653465

34663466
char splitname[256];
3467-
sprintf(splitname, "splitncnn_input%d", j);
3467+
snprintf(splitname, 256, "splitncnn_input%d", j);
34683468
fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount);
34693469
fprintf(pp, " %s", trunc_name(input_name).c_str());
34703470

@@ -3533,7 +3533,7 @@ For more information, please refer to https://github.com/pnnx/pnnx\n");
35333533
}
35343534

35353535
char splitname[256];
3536-
sprintf(splitname, "splitncnn_%d", internal_split);
3536+
snprintf(splitname, 256, "splitncnn_%d", internal_split);
35373537
fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount);
35383538

35393539
fprintf(pp, " %s", trunc_name(input_name).c_str());
@@ -3998,7 +3998,7 @@ For more information, please refer to https://github.com/pnnx/pnnx\n");
39983998
split_node_reference[input_name] = refidx;
39993999

40004000
char splitsuffix[256];
4001-
sprintf(splitsuffix, "_splitncnn_%d", refidx);
4001+
snprintf(splitsuffix, 256, "_splitncnn_%d", refidx);
40024002
input_name = input_name + splitsuffix;
40034003
}
40044004

@@ -6116,7 +6116,7 @@ For more information, please refer to https://github.com/pnnx/pnnx\n");
61166116
if (refcount > 1)
61176117
{
61186118
char splitname[256];
6119-
sprintf(splitname, "splitncnn_%d", internal_split);
6119+
snprintf(splitname, 256, "splitncnn_%d", internal_split);
61206120
fprintf(pp, "%-16s %-24s %d %d", "Split", splitname, 1, refcount);
61216121

61226122
fprintf(pp, " %s", trunc_name(output_name).c_str());

tools/pnnx/src/ir.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ std::string Parameter::encode_to_string(const Parameter& param)
458458
if (param.type == 10)
459459
{
460460
char buf[128];
461-
sprintf(buf, "%e+%ej", param.c.real(), param.c.imag());
461+
snprintf(buf, 128, "%e+%ej", param.c.real(), param.c.imag());
462462
return std::string(buf);
463463
}
464464
if (param.type == 11)
@@ -467,7 +467,7 @@ std::string Parameter::encode_to_string(const Parameter& param)
467467
for (size_t i = 0; i < param.ac.size(); i++)
468468
{
469469
char buf[128];
470-
sprintf(buf, "%e+%ej", param.ac[i].real(), param.ac[i].imag());
470+
snprintf(buf, 128, "%e+%ej", param.ac[i].real(), param.ac[i].imag());
471471
s += std::string(buf);
472472
if (i + 1 != param.ac.size())
473473
s += std::string(",");

tools/pnnx/src/pass_level1.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void pass_level1(const torch::jit::Module& mod, const std::shared_ptr<torch::jit
6969
const auto& in = g->inputs()[i];
7070

7171
char name[32];
72-
sprintf(name, "pnnx_input_%d", i - 1);
72+
snprintf(name, 32, "pnnx_input_%d", i - 1);
7373

7474
Operator* op = pg.new_operator("pnnx.Input", name);
7575
Operand* r = pg.new_operand(in);
@@ -151,7 +151,7 @@ void pass_level1(const torch::jit::Module& mod, const std::shared_ptr<torch::jit
151151
else if (n->kind() == c10::prim::Constant) // || n->kind() == c10::prim::ListConstruct)
152152
{
153153
char name[32];
154-
sprintf(name, "pnnx_%d", pnnx_unknown_index++);
154+
snprintf(name, 32, "pnnx_%d", pnnx_unknown_index++);
155155

156156
Operator* op = pg.new_operator(n->kind().toDisplayString(), name);
157157

@@ -321,7 +321,7 @@ void pass_level1(const torch::jit::Module& mod, const std::shared_ptr<torch::jit
321321
for (auto attr : constant_attr_nodes)
322322
{
323323
char name[32];
324-
sprintf(name, "pnnx_%02d", pnnx_moduleop_unknown_index);
324+
snprintf(name, 32, "pnnx_%02d", pnnx_moduleop_unknown_index);
325325
op->attrs[name] = attr.second->t(torch::jit::attr::value);
326326
pnnx_moduleop_unknown_index++;
327327
}
@@ -395,7 +395,7 @@ void pass_level1(const torch::jit::Module& mod, const std::shared_ptr<torch::jit
395395
else
396396
{
397397
char name[32];
398-
sprintf(name, "pnnx_%d", pnnx_unknown_index++);
398+
snprintf(name, 32, "pnnx_%d", pnnx_unknown_index++);
399399

400400
Operator* op = pg.new_operator(n->kind().toDisplayString(), name);
401401

@@ -422,7 +422,7 @@ void pass_level1(const torch::jit::Module& mod, const std::shared_ptr<torch::jit
422422
const auto& in = g->outputs()[i];
423423

424424
char name[32];
425-
sprintf(name, "pnnx_output_%d", i);
425+
snprintf(name, 32, "pnnx_output_%d", i);
426426
Operator* op = pg.new_operator("pnnx.Output", name);
427427
Operand* r = pg.get_operand(in->debugName());
428428
r->consumers.push_back(op);

0 commit comments

Comments
 (0)