public inbox for libabigail@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Increase stability of child diff order
@ 2022-03-03 14:56 Giuliano Procida
  2022-03-08 17:41 ` Dodji Seketeli
  0 siblings, 1 reply; 4+ messages in thread
From: Giuliano Procida @ 2022-03-03 14:56 UTC (permalink / raw)
  To: libabigail; +Cc: dodji, kernel-team, gprocida, maennich

Bug 28939 - diff output is sensitive to implementation of std::sort

This change increases the stability of child diff order.

One test case is affected, potentially indicating the presence of a
bug (should the parameter 1 diff not be reported unconditionally?).

	* src/abg-comparison-priv.h
	(diff_less_than_functor::operator()): This now compares the
        second diff subject names if the first diff subject names are
        the same. It also handles all possble null pointer values,
        though in practice they should never occur.
	* src/abg-comparison.cc (diff::append_child_node): Replace
	std::sort with std::stable_sort.
	* tests/data/test-abidiff-exit/test-member-size-report0.txt:
	Refresh test.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 src/abg-comparison-priv.h                     | 34 +++++++++++++++----
 src/abg-comparison.cc                         |  6 ++--
 .../test-member-size-report0.txt              | 15 ++++----
 3 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/src/abg-comparison-priv.h b/src/abg-comparison-priv.h
index 8e2e59c6..bf219f96 100644
--- a/src/abg-comparison-priv.h
+++ b/src/abg-comparison-priv.h
@@ -340,13 +340,33 @@ struct diff_less_than_functor
   bool
   operator()(const diff* l, const diff* r) const
   {
-    if (!l || !r || !l->first_subject() || !r->first_subject())
-      return false;
-
-    string l_qn = get_name(l->first_subject());
-    string r_qn = get_name(r->first_subject());
-
-    return l_qn < r_qn;
+    if ((l == nullptr) != (r == nullptr))
+      return l < r;
+    if (l == nullptr)
+      return true;
+    const type_or_decl_base* l_first_subject = l->first_subject().get();
+    const type_or_decl_base* r_first_subject = r->first_subject().get();
+    if ((l_first_subject == nullptr) != (r_first_subject == nullptr))
+      return l_first_subject < r_first_subject;
+    if (l_first_subject != nullptr)
+      {
+	string l_qn = get_name(l_first_subject);
+	string r_qn = get_name(r_first_subject);
+	if (l_qn != r_qn)
+	  return l_qn < r_qn;
+      }
+    const type_or_decl_base* l_second_subject = l->second_subject().get();
+    const type_or_decl_base* r_second_subject = r->second_subject().get();
+    if ((l_second_subject == nullptr) != (r_second_subject == nullptr))
+      return l_second_subject < r_second_subject;
+    if (l_second_subject != nullptr)
+      {
+	string l_qn = get_name(l_second_subject);
+	string r_qn = get_name(r_second_subject);
+	if (l_qn != r_qn)
+	  return l_qn < r_qn;
+      }
+    return true;
   }
 
   /// An operator that takes two instances of @ref diff_sptr returns
diff --git a/src/abg-comparison.cc b/src/abg-comparison.cc
index 71048ce2..c7f043c8 100644
--- a/src/abg-comparison.cc
+++ b/src/abg-comparison.cc
@@ -1999,9 +1999,9 @@ diff::append_child_node(diff_sptr d)
   priv_->children_.push_back(d.get());
 
   diff_less_than_functor comp;
-  std::sort(priv_->children_.begin(),
-	    priv_->children_.end(),
-	    comp);
+  std::stable_sort(priv_->children_.begin(),
+		   priv_->children_.end(),
+		   comp);
 
   d->priv_->parent_ = this;
 }
diff --git a/tests/data/test-abidiff-exit/test-member-size-report0.txt b/tests/data/test-abidiff-exit/test-member-size-report0.txt
index 5c8ece62..78705efb 100644
--- a/tests/data/test-abidiff-exit/test-member-size-report0.txt
+++ b/tests/data/test-abidiff-exit/test-member-size-report0.txt
@@ -4,16 +4,15 @@ Variables changes summary: 0 Removed, 0 Changed, 0 Added variable
 2 functions with some indirect sub-type change:
 
   [C] 'function void reg1(S*, T*, T*)' at test-member-size-v1.cc:26:1 has some indirect sub-type changes:
-    parameter 1 of type 'S*' has sub-type changes:
-      in pointed to type 'struct S' at test-member-size-v1.cc:3:1:
-        type size changed from 128 to 192 (in bits)
-        1 data member insertion:
-          'int y', at offset 128 (in bits) at test-member-size-v1.cc:6:1
-        no data member change (1 filtered);
-    parameter 2 of type 'T*' has sub-type changes:
+    parameter 3 of type 'T*' has sub-type changes:
       in pointed to type 'struct T' at test-member-size-v1.cc:14:1:
         type size changed from 192 to 256 (in bits)
-        1 data member changes (1 filtered):
+        2 data member changes:
+          type of 'S s' changed:
+            type size changed from 128 to 192 (in bits)
+            1 data member insertion:
+              'int y', at offset 128 (in bits) at test-member-size-v1.cc:6:1
+            no data member change (1 filtered);
           'int a' offset changed from 128 to 192 (in bits) (by +64 bits)
 
   [C] 'function void reg2(U*)' at test-member-size-v1.cc:27:1 has some indirect sub-type changes:
-- 
2.35.1.574.g5d30c73bfb-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-03-10 10:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-03 14:56 [PATCH] Increase stability of child diff order Giuliano Procida
2022-03-08 17:41 ` Dodji Seketeli
2022-03-09 10:41   ` Giuliano Procida
2022-03-10 10:43     ` [PATCH, applied] comparison: Avoid sorting diff nodes with wrong criteria Dodji Seketeli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).