From 17c33b5abdee0bd9a82ade987c19da5fae488350 Mon Sep 17 00:00:00 2001 From: Flavio Bergamaschi Date: Fri, 14 Jul 2023 18:23:08 +0100 Subject: [PATCH] Fix use of deprecated sprintf (#27) * Fix deprecated sprintf in GTestApproxNums.cpp --- .github/workflows/github-ci.yml | 2 +- tests/GTestApproxNums.cpp | 12 ++++++++++-- tests/GTestGeneral.cpp | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index e9b3e9118..27ae55264 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -39,7 +39,7 @@ jobs: package_build: [ON, OFF] hexl: [ON, OFF] c_compiler: [gcc, clang] - os: [macos-latest, ubuntu-20.04, ubuntu-22.04] + os: [macos-latest, ubuntu-22.04] include: # Use g++ with gcc only and clang++ with clang only - c_compiler: gcc cxx_compiler: g++ diff --git a/tests/GTestApproxNums.cpp b/tests/GTestApproxNums.cpp index 1b1308b21..b17b26f75 100644 --- a/tests/GTestApproxNums.cpp +++ b/tests/GTestApproxNums.cpp @@ -13,6 +13,8 @@ #include #include +#include + #include #include #include @@ -511,10 +513,13 @@ TEST_P(GTestApproxNums, generalOpsWorks) std::vector> tmp_p(p1); // tmp = c1 helib::Ctxt tmp(c1); - sprintf(buffer, "tmp=c1>>=%d", (int)shamt); + + EXPECT_TRUE(snprintf(buffer, sizeof(buffer), "tmp=c1>>=%d", (int)shamt) > 0); + rotate(tmp_p, shamt); // ea.shift(tmp, random amount in [-nSlots/2,nSlots/2]) ea.rotate(tmp, shamt); + if (helib_test::verbose) { CheckCtxt(tmp, buffer); } @@ -527,12 +532,15 @@ TEST_P(GTestApproxNums, generalOpsWorks) } EXPECT_TRUE(ciphertextMatches(ea, secretKey, p2, c2, epsilon)); - sprintf(buffer, "c2>>>=%d", (int)rotamt); + EXPECT_TRUE(snprintf(buffer, sizeof(buffer), "c2>>>=%d", (int)rotamt) > 0); + rotate(p2, rotamt); // ea.rotate(c2, random amount in [1-nSlots, nSlots-1]) ea.rotate(c2, rotamt); + if (helib_test::verbose) { CheckCtxt(c2, buffer); } + EXPECT_TRUE(ciphertextMatches(ea, secretKey, p2, c2, epsilon)); negateVec(p1); // c1.negate() diff --git a/tests/GTestGeneral.cpp b/tests/GTestGeneral.cpp index 12ea8abd3..84470dde4 100644 --- a/tests/GTestGeneral.cpp +++ b/tests/GTestGeneral.cpp @@ -315,7 +315,7 @@ TEST_P(GTestGeneral, correctlyImplementsMixOfOperationsOverFourCiphertexts) helib::PtxtArray tmp_p(p1); // tmp = c1 helib::Ctxt tmp(c1); - sprintf(buffer, "tmp=c1>>=%d", (int)shamt); + EXPECT_TRUE(snprintf(buffer, sizeof(buffer), "tmp=c1>>=%d", (int)shamt) > 0); shift(tmp_p, shamt); // ea.shift(tmp, random amount in [-nSlots/2,nSlots/2]) ea.shift(tmp, shamt); if (!helib_test::noPrint) @@ -328,7 +328,7 @@ TEST_P(GTestGeneral, correctlyImplementsMixOfOperationsOverFourCiphertexts) CheckCtxt(c2, "c2+=tmp"); EXPECT_TRUE(ciphertextMatches(ea, secretKey, p2, c2)); - sprintf(buffer, "c2>>>=%d", (int)rotamt); + EXPECT_TRUE(snprintf(buffer, sizeof(buffer), "c2>>>=%d", (int)rotamt) > 0); rotate(p2, rotamt); // ea.rotate(c2, random amount in [1-nSlots, nSlots-1]) ea.rotate(c2, rotamt); if (!helib_test::noPrint)