[llvm] [yaml2obj] Don't use uninitialized Type (PR #123274)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 21 09:19:50 PST 2025


https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/123274

>From fbd205b9fc1ecd652f9d0fff2f471b4c1bd9e4c8 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 16 Jan 2025 18:40:51 -0800
Subject: [PATCH 1/6] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
 =?UTF-8?q?anges=20to=20main=20this=20commit=20is=20based=20on?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.4

[skip ci]
---
 llvm/lib/ObjectYAML/ELFYAML.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 7e94d01a971534..83e6cf76dd746f 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1588,7 +1588,7 @@ static bool isInteger(StringRef Val) {
 
 void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
     IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
-  ELFYAML::ELF_SHT Type;
+  ELFYAML::ELF_SHT Type = ELF::ET_NONE;
   StringRef TypeStr;
   if (IO.outputting()) {
     if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))

>From 877e727d2c9a40f012ebd78ff07cabac348b60bb Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Thu, 16 Jan 2025 18:42:37 -0800
Subject: [PATCH 2/6] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
 =?UTF-8?q?anges=20introduced=20through=20rebase?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.4

[skip ci]
---
 llvm/lib/ObjectYAML/ELFYAML.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 83e6cf76dd746f..7e94d01a971534 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1588,7 +1588,7 @@ static bool isInteger(StringRef Val) {
 
 void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
     IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
-  ELFYAML::ELF_SHT Type = ELF::ET_NONE;
+  ELFYAML::ELF_SHT Type;
   StringRef TypeStr;
   if (IO.outputting()) {
     if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))

>From a1558db7c994e18b08e86056970b3d823fba0355 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 16 Jan 2025 19:39:39 -0800
Subject: [PATCH 3/6] error()

Created using spr 1.3.4
---
 llvm/include/llvm/Support/YAMLTraits.h | 4 +++-
 llvm/lib/ObjectYAML/ELFYAML.cpp        | 2 +-
 llvm/lib/Support/YAMLTraits.cpp        | 4 ++++
 3 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index eca26e90845bf6..e707a445012b51 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -819,6 +819,7 @@ class IO {
   virtual NodeKind getNodeKind() = 0;
 
   virtual void setError(const Twine &) = 0;
+  virtual std::error_code error() = 0;
   virtual void setAllowUnknownKeys(bool Allow);
 
   template <typename T>
@@ -1448,7 +1449,7 @@ class Input : public IO {
   ~Input() override;
 
   // Check if there was an syntax or semantic error during parsing.
-  std::error_code error();
+  std::error_code error() override;
 
 private:
   bool outputting() const override;
@@ -1631,6 +1632,7 @@ class Output : public IO {
   void scalarTag(std::string &) override;
   NodeKind getNodeKind() override;
   void setError(const Twine &message) override;
+  std::error_code error() override;
   bool canElideEmptySequence() override;
 
   // These are only used by operator<<. They could be private
diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 961815392a1348..e0e941cff94c52 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1601,7 +1601,7 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
     TypeStr = getStringValue(IO, "Type");
     if (TypeStr.starts_with("SHT_") || isInteger(TypeStr)) {
       IO.mapRequired("Type", Type);
-      if (static_cast<Input&>(IO).error())
+      if (IO.error())
         Type = ELF::SHT_NULL;
     }
    }
diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp
index f326422138488c..08c0ae4b1e332d 100644
--- a/llvm/lib/Support/YAMLTraits.cpp
+++ b/llvm/lib/Support/YAMLTraits.cpp
@@ -750,6 +750,10 @@ void Output::scalarTag(std::string &Tag) {
 void Output::setError(const Twine &message) {
 }
 
+std::error_code Output::error() {
+  return {};
+}
+
 bool Output::canElideEmptySequence() {
   // Normally, with an optional key/value where the value is an empty sequence,
   // the whole key/value can be not written.  But, that produces wrong yaml

>From 94d1be74e2d7f928e45be8c70d7581037c17bde9 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Sat, 18 Jan 2025 12:28:08 -0800
Subject: [PATCH 4/6] update

Created using spr 1.3.4
---
 llvm/include/llvm/Support/YAMLTraits.h | 4 +---
 llvm/lib/Support/YAMLTraits.cpp        | 2 --
 2 files changed, 1 insertion(+), 5 deletions(-)

diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index e707a445012b51..eca26e90845bf6 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -819,7 +819,6 @@ class IO {
   virtual NodeKind getNodeKind() = 0;
 
   virtual void setError(const Twine &) = 0;
-  virtual std::error_code error() = 0;
   virtual void setAllowUnknownKeys(bool Allow);
 
   template <typename T>
@@ -1449,7 +1448,7 @@ class Input : public IO {
   ~Input() override;
 
   // Check if there was an syntax or semantic error during parsing.
-  std::error_code error() override;
+  std::error_code error();
 
 private:
   bool outputting() const override;
@@ -1632,7 +1631,6 @@ class Output : public IO {
   void scalarTag(std::string &) override;
   NodeKind getNodeKind() override;
   void setError(const Twine &message) override;
-  std::error_code error() override;
   bool canElideEmptySequence() override;
 
   // These are only used by operator<<. They could be private
diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp
index 28642e004c4f43..f326422138488c 100644
--- a/llvm/lib/Support/YAMLTraits.cpp
+++ b/llvm/lib/Support/YAMLTraits.cpp
@@ -750,8 +750,6 @@ void Output::scalarTag(std::string &Tag) {
 void Output::setError(const Twine &message) {
 }
 
-std::error_code Output::error() { return {}; }
-
 bool Output::canElideEmptySequence() {
   // Normally, with an optional key/value where the value is an empty sequence,
   // the whole key/value can be not written.  But, that produces wrong yaml

>From b340b41911de3c9f4baf9f499272c2e3e41d96fe Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 20 Jan 2025 23:01:09 -0800
Subject: [PATCH 5/6] remove comment and move initialization

Created using spr 1.3.4
---
 llvm/lib/ObjectYAML/ELFYAML.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 4b6e77783aaaf0..7c99e971adbad2 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1588,7 +1588,7 @@ static bool isInteger(StringRef Val) {
 
 void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
     IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
-  ELFYAML::ELF_SHT Type;
+  ELFYAML::ELF_SHT Type = ELF::SHT_NULL;
   StringRef TypeStr;
   if (IO.outputting()) {
     if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))
@@ -1599,8 +1599,6 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
     // When the Type string does not have a "SHT_" prefix, we know it is not a
     // description of a regular ELF output section.
     TypeStr = getStringValue(IO, "Type");
-    // To for fallback to `default` in switch below on error.
-    Type = ELF::SHT_NULL;
     if (TypeStr.starts_with("SHT_") || isInteger(TypeStr))
       IO.mapRequired("Type", Type);
   }

>From 14fb5e44b79f6d58cc81f47b9a54500dcab46a4b Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Tue, 21 Jan 2025 09:19:37 -0800
Subject: [PATCH 6/6] test

Created using spr 1.3.4
---
 .../tools/yaml2obj/ELF/section-headers.yaml   | 12 -------
 .../test/tools/yaml2obj/ELF/section-type.yaml | 32 +++++++++++++++++--
 2 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
index 619ad864b6f20f..4b61920f139a06 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
@@ -455,15 +455,3 @@ Sections:
 # LINK-NEXT:   [ 3] .ref.last  PROGBITS [[#%x,]] [[#%x,]] [[#%x,]] [[#%x,]]      4
 # LINK-NEXT:   [ 4] .shstrtab
 
-# RUN: not yaml2obj %s --docnum=15 -o %t15 2>&1 | FileCheck %s --check-prefix=UNKNOWN_SECTION
-
---- !ELF
-FileHeader:
-  Class: ELFCLASS64
-  Data:  ELFDATA2LSB
-  Type:  ET_REL
-Sections:
-  - Name: .foo
-    Type: FOO_SECTION
-
-# UNKNOWN_SECTION: error: invalid hex32 number
diff --git a/llvm/test/tools/yaml2obj/ELF/section-type.yaml b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
index ad2edd942cc2aa..63c4b9b0e583b8 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-type.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-type.yaml
@@ -1,5 +1,5 @@
-# RUN: yaml2obj %s -o %t
-# RUN: llvm-readobj --sections %t | FileCheck %s
+# RUN: yaml2obj %s --docnum=1 -o %t1
+# RUN: llvm-readobj --sections %t1 | FileCheck %s
 
 # CHECK: Name: enum
 # CHECK: Type: SHT_PROGBITS
@@ -25,3 +25,31 @@ Sections:
     Type:  0xabcd
   - Name:  decimal
     Type:  1234
+
+
+# RUN: not yaml2obj %s --docnum=2 -o %t2 2>&1 | FileCheck %s --check-prefix=UNKNOWN-TYPE
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_REL
+Sections:
+  - Name: .foo
+    Type: FOO_SECTION
+
+# UNKNOWN-TYPE: error: invalid hex32 number
+
+
+# RUN: not yaml2obj %s --docnum=3 -o %t3 -DMACHINE=EM_NONE 2>&1 | FileCheck %s --check-prefix=UNKNOWN-SHT-TYPE
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_REL
+Sections:
+  - Name: .foo
+    Type: SHT_MIPS_ABIFLAGS
+
+# UNKNOWN-SHT-TYPE: error: invalid hex32 number



More information about the llvm-commits mailing list