[flang-commits] [flang] ecd6917 - [flang][unittests] Fix recent snprintf() changes to use correct buffer lengths

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Jul 18 10:28:24 PDT 2023


Author: Peter Klausler
Date: 2023-07-18T10:28:16-07:00
New Revision: ecd691750648b72b9e0ce6879fcfa60396693a77

URL: https://github.com/llvm/llvm-project/commit/ecd691750648b72b9e0ce6879fcfa60396693a77
DIFF: https://github.com/llvm/llvm-project/commit/ecd691750648b72b9e0ce6879fcfa60396693a77.diff

LOG: [flang][unittests] Fix recent snprintf() changes to use correct buffer lengths

A recent patch replaced two sprintf() calls with snprintf(), but didn't
use the right length for the remaining space in the buffer.

Differential Revision: https://reviews.llvm.org/D155224

Added: 
    

Modified: 
    flang/unittests/Decimal/quick-sanity-test.cpp
    flang/unittests/Decimal/thorough-test.cpp

Removed: 
    


################################################################################
diff  --git a/flang/unittests/Decimal/quick-sanity-test.cpp b/flang/unittests/Decimal/quick-sanity-test.cpp
index 74434c1b8f2bd5..dafb075307ab6a 100644
--- a/flang/unittests/Decimal/quick-sanity-test.cpp
+++ b/flang/unittests/Decimal/quick-sanity-test.cpp
@@ -53,7 +53,8 @@ void testReadback(float x, int flags) {
       ++expo;
     }
     if (q >= buffer && q < buffer + sizeof buffer) {
-      std::snprintf(q + result.length, sizeof buffer, "e%d", expo);
+      std::snprintf(q + result.length,
+          buffer + sizeof buffer - (q + result.length), "e%d", expo);
     }
     const char *p{q};
     auto rflags{ConvertDecimalToFloat(&p, &y, RoundNearest)};

diff  --git a/flang/unittests/Decimal/thorough-test.cpp b/flang/unittests/Decimal/thorough-test.cpp
index 2261a1c3b03e97..46951f33eb8fee 100644
--- a/flang/unittests/Decimal/thorough-test.cpp
+++ b/flang/unittests/Decimal/thorough-test.cpp
@@ -51,7 +51,8 @@ void testReadback(float x, int flags) {
       if (*q == '-' || *q == '+') {
         ++expo;
       }
-      std::snprintf(q + result.length, sizeof buffer, "e%d", expo);
+      std::snprintf(q + result.length,
+          buffer + sizeof buffer - (q + result.length), "e%d", expo);
     }
     const char *p{q};
     auto rflags{ConvertDecimalToFloat(&p, &y, RoundNearest)};


        


More information about the flang-commits mailing list