public inbox for libabigail@sourceware.org
 help / color / mirror / Atom feed
From: Giuliano Procida <gprocida@google.com>
To: libabigail@sourceware.org
Cc: dodji@seketeli.org, kernel-team@android.com, gprocida@google.com,
	 maennich@google.com
Subject: [PATCH] Increase stability of child diff order
Date: Thu,  3 Mar 2022 14:56:19 +0000	[thread overview]
Message-ID: <20220303145619.385117-1-gprocida@google.com> (raw)

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


             reply	other threads:[~2022-03-03 14:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-03 14:56 Giuliano Procida [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220303145619.385117-1-gprocida@google.com \
    --to=gprocida@google.com \
    --cc=dodji@seketeli.org \
    --cc=kernel-team@android.com \
    --cc=libabigail@sourceware.org \
    --cc=maennich@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).