gcc 6.2 warning fix

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

gcc 6.2 warning fix

David Faure
Hi Ted,

I'd like to reply to this comment in a recent commit (from TriggerSegment.cpp)

+    // ??? With GCC 6.2.0, this results in a warning:
+    //     warning: nonnull argument ‘this’ compared to NULL [-Wnonnull-compare]
+    //     Assuming "this" is always non-null is overly optimistic.  The
+    //     following can happen:
+    //         TriggerSegmentRec *p = NULL;
+    //         p->ExpandInto(...);
+    //     Defending against this possibility shouldn't be punished with a
+    //     warning.  In this case, the warning is wrong, and we should
+    //     probably just suppress the warning until the compiler is fixed.
+    #pragma GCC diagnostic push
+    #pragma GCC diagnostic ignored "-Wnonnull-compare"
     if (!this || !getSegment() || getSegment()->empty())
         { return false; }
+    #pragma GCC diagnostic pop

The compiler won't ever get "fixed", since it's correct: calling a method on a null pointer is invalid.
Declaring this to be invalid allows compilers - and developers - to always know that
"this" is never null, which removes the need for checking this in each and every method
(that would be crazy overhead).

I added a check at the calling site instead, see patch attached.

(the other calling site is just after "new", and typically in Qt code we assume new to succeed,
because on a desktop machine you'll have rebooted the machine before it even gets to the
point where new returns null).

David Faure, [hidden email], http://www.davidfaure.fr
Working on KDE Frameworks 5

Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
Rosegarden-devel mailing list
[hidden email] - use the link below to unsubscribe

this-nonnull-check.diff (1K) Download Attachment