[PATCH XIT] common/helper: fix error trap for stack-local XError

Peter Hutterer peter.hutterer at who-t.net
Sun Nov 4 22:34:14 PST 2012


The error Xlib passes to the error handler is stack-local. Saving its
address is entertaining, but largely useless.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 tests/common/helpers.cpp           | 14 ++++++++------
 tests/common/helpers.h             |  2 +-
 tests/server/barriers-validity.cpp |  8 ++++----
 3 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/tests/common/helpers.cpp b/tests/common/helpers.cpp
index aa6079c..2484e80 100644
--- a/tests/common/helpers.cpp
+++ b/tests/common/helpers.cpp
@@ -132,9 +132,10 @@ void DeviceSetEnabled(Display *dpy, int deviceid, bool enabled)
 /* Basic error trapping */
 static struct {
     bool is_trapping;
-    XErrorEvent *error;
+    bool have_error;
+    XErrorEvent error;
     XErrorHandler prev_error_handler;
-} trap_state = { false, NULL, NULL };
+} trap_state;
 
 static int
 ErrorHandler(Display *dpy,
@@ -143,7 +144,8 @@ ErrorHandler(Display *dpy,
     if (!trap_state.is_trapping)
         ADD_FAILURE() << "Error trap received error while not trapping. WTF?";
 
-    trap_state.error = error;
+    trap_state.have_error = true;
+    trap_state.error = *error;
     return 0;
 }
 
@@ -156,7 +158,7 @@ void SetErrorTrap(Display *dpy) {
     trap_state.is_trapping = true;
 }
 
-XErrorEvent * ReleaseErrorTrap(Display *dpy) {
+const XErrorEvent* ReleaseErrorTrap(Display *dpy) {
     if (!trap_state.is_trapping)
         ADD_FAILURE() << "ReleaseErrorTrap() called while not trapping.";
 
@@ -164,9 +166,9 @@ XErrorEvent * ReleaseErrorTrap(Display *dpy) {
     XSetErrorHandler(trap_state.prev_error_handler);
     trap_state.prev_error_handler = NULL;
 
-    XErrorEvent *error = trap_state.error;
-    trap_state.error = NULL;
+    const XErrorEvent *error = trap_state.have_error ? &trap_state.error : NULL;
     trap_state.is_trapping = false;
+    trap_state.have_error = false;
     return error;
 }
 
diff --git a/tests/common/helpers.h b/tests/common/helpers.h
index 43f7c76..be2f9e7 100644
--- a/tests/common/helpers.h
+++ b/tests/common/helpers.h
@@ -92,7 +92,7 @@ void SetErrorTrap(Display *dpy);
  *
  * @return An XErrorEvent that was trapped.
  */
-XErrorEvent * ReleaseErrorTrap(Display *dpy);
+const XErrorEvent* ReleaseErrorTrap(Display *dpy);
 
 #define ASSERT_ERROR(err, code)                                         \
     ASSERT_TRUE(err != NULL) << ("Expected " #code);                    \
diff --git a/tests/server/barriers-validity.cpp b/tests/server/barriers-validity.cpp
index 8aa9343..61d1cec 100644
--- a/tests/server/barriers-validity.cpp
+++ b/tests/server/barriers-validity.cpp
@@ -43,7 +43,7 @@ TEST_F(BarrierSimpleTest, DestroyInvalidBarrier)
 
     SetErrorTrap(Display());
     XFixesDestroyPointerBarrier(Display(), -1);
-    XErrorEvent *error = ReleaseErrorTrap(Display());
+    const XErrorEvent *error = ReleaseErrorTrap(Display());
     ASSERT_ERROR(error, xfixes_error_base + BadBarrier);
 }
 
@@ -83,7 +83,7 @@ TEST_P(BarrierZeroLength, InvalidZeroLengthBarrier)
 
     SetErrorTrap(dpy);
     XFixesCreatePointerBarrier(dpy, root, 20, 20, 20, 20, directions, 0, NULL);
-    XErrorEvent *error = ReleaseErrorTrap(dpy);
+    const XErrorEvent *error = ReleaseErrorTrap(dpy);
     ASSERT_ERROR(error, BadValue);
 }
 
@@ -104,7 +104,7 @@ TEST_P(BarrierNonZeroArea, InvalidNonZeroAreaBarrier)
 
     SetErrorTrap(dpy);
     XFixesCreatePointerBarrier(dpy, root, 20, 20, 40, 40, directions, 0, NULL);
-    XErrorEvent *error = ReleaseErrorTrap(dpy);
+    const XErrorEvent *error = ReleaseErrorTrap(dpy);
     ASSERT_ERROR(error, BadValue);
 }
 INSTANTIATE_TEST_CASE_P(, BarrierNonZeroArea, VALID_DIRECTIONS);
@@ -143,7 +143,7 @@ TEST_P(BarrierConflictingDirections, InvalidConflictingDirectionsBarrier)
 
     SetErrorTrap(dpy);
     XFixesCreatePointerBarrier(dpy, root, x1, y1, x2, y2, directions, 0, NULL);
-    XErrorEvent *error = ReleaseErrorTrap(dpy);
+    const XErrorEvent *error = ReleaseErrorTrap(dpy);
 
     /* Nonsensical directions are ignored -- they don't
      * raise a BadValue. Unfortunately, there's no way
-- 
1.7.11.7



More information about the xorg-devel mailing list