[llvm] 43ff058 - [llvm-objcopy] IHexELFBuilder::addDataSections - fix evaluation ordering static analyzer warning

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 3 04:28:15 PDT 2021


Author: Simon Pilgrim
Date: 2021-08-03T12:16:59+01:00
New Revision: 43ff058e78d9e4fa47080b61fc3811da80db1b3f

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

LOG: [llvm-objcopy] IHexELFBuilder::addDataSections - fix evaluation ordering static analyzer warning

As detailed on https://pvs-studio.com/en/blog/posts/cpp/0771/ and raised on D62583, the SecNo++ increment is not guaranteed to occur before the second use of SecNo in the same addSection() call.

This patch pulls out the increment (just for clarity) and replaces the second use of SecNo with a constant zero value (we're using stable_sort so the value isn't critical).

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

Added: 
    

Modified: 
    llvm/tools/llvm-objcopy/ELF/Object.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp
index ba91d08e5540a..6b632ae929f00 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -1342,13 +1342,16 @@ void IHexELFBuilder::addDataSections() {
       if (R.HexData.empty())
         continue;
       RecAddr = R.Addr + SegmentAddr + BaseAddr;
-      if (!Section || Section->Addr + Section->Size != RecAddr)
+      if (!Section || Section->Addr + Section->Size != RecAddr) {
         // OriginalOffset field is only used to sort section properly, so
-        // instead of keeping track of real offset in IHEX file, we use
-        // section number.
+        // instead of keeping track of real offset in IHEX file, and as
+        // Object::sortSections() uses llvm::stable_sort(), we can just set to a
+        // constant (zero).
         Section = &Obj->addSection<OwnedDataSection>(
-            ".sec" + std::to_string(SecNo++), RecAddr,
-            ELF::SHF_ALLOC | ELF::SHF_WRITE, SecNo);
+            ".sec" + std::to_string(SecNo), RecAddr,
+            ELF::SHF_ALLOC | ELF::SHF_WRITE, 0);
+        SecNo++;
+      }
       Section->appendHexData(R.HexData);
       break;
     case IHexRecord::EndOfFile:


        


More information about the llvm-commits mailing list