glx/tests: Fix leaks in the unit tests.
authorEric Anholt <eric@anholt.net>
Fri, 4 Dec 2020 19:47:46 +0000 (11:47 -0800)
committerMarge Bot <eric+marge@anholt.net>
Tue, 15 Dec 2020 19:39:29 +0000 (19:39 +0000)
Needed for meson test with asan enabled.

Reviewed-by: Adam Jackson <ajax@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7936>

src/glx/tests/clientinfo_unittest.cpp
src/glx/tests/create_context_unittest.cpp

index da62c8a..fe168d2 100644 (file)
@@ -56,8 +56,8 @@ public:
    ~fake_glx_display()
    {
       for (int i = 0; i < this->dpy->nscreens; i++) {
-        if (this->screens[i] != NULL)
-           delete this->screens[i];
+         if (this->screens[i] != NULL)
+            delete (fake_glx_screen *)this->screens[i];
       }
 
       delete [] this->screens;
@@ -71,6 +71,7 @@ public:
    glX_send_client_info_test();
    virtual ~glX_send_client_info_test();
    virtual void SetUp();
+   virtual void TearDown();
 
    void common_protocol_expected_false_test(unsigned major, unsigned minor,
                                            const char *glx_ext, bool *value);
@@ -131,7 +132,7 @@ xcb_glx_client_info(xcb_connection_t *c,
    ClientInfo_was_sent = true;
    connection_used = c;
 
-   gl_ext_string = (char *) malloc(str_len);
+   gl_ext_string = new char[str_len];
    memcpy(gl_ext_string, string, str_len);
    gl_ext_length = str_len;
 
@@ -254,6 +255,17 @@ glX_send_client_info_test::SetUp()
 }
 
 void
+glX_send_client_info_test::TearDown()
+{
+   if (gl_ext_string)
+      delete [] gl_ext_string;
+   if (glx_ext_string)
+      delete [] glx_ext_string;
+   if (gl_versions)
+      delete [] gl_versions;
+}
+
+void
 glX_send_client_info_test::create_single_screen_display(unsigned major,
                                                        unsigned minor,
                                                        const char *glx_ext)
index 448dbea..c4d3892 100644 (file)
@@ -130,6 +130,7 @@ __glXSendError(Display * dpy, int_fast8_t errorCode, uint_fast32_t resourceID,
 class glXCreateContextAttribARB_test : public ::testing::Test {
 public:
    virtual void SetUp();
+   virtual void TearDown();
 
    /**
     * Replace the existing screen with a direct-rendering screen
@@ -137,6 +138,7 @@ public:
    void use_direct_rendering_screen();
 
    mock_XDisplay *dpy;
+   GLXContext ctx;
    struct glx_config fbc;
 };
 
@@ -153,6 +155,19 @@ glXCreateContextAttribARB_test::SetUp()
 
    memset(&this->fbc, 0, sizeof(this->fbc));
    this->fbc.fbconfigID = 0xbeefcafe;
+
+   this->ctx = NULL;
+}
+
+void
+glXCreateContextAttribARB_test::TearDown()
+{
+   if (ctx)
+      delete (fake_glx_context *)ctx;
+
+   delete (fake_glx_screen *)psc;
+
+   delete this->dpy;
 }
 
 void
@@ -163,7 +178,7 @@ glXCreateContextAttribARB_test::use_direct_rendering_screen()
                                 psc->scr,
                                 psc->serverGLXexts);
 
-   delete psc;
+   delete (fake_glx_screen *)psc;
    psc = direct_psc;
 }
 
@@ -192,7 +207,7 @@ TEST_F(glXCreateContextAttribARB_test, NULL_fbconfig_returns_None)
 
 TEST_F(glXCreateContextAttribARB_test, NULL_screen_returns_None)
 {
-   delete psc;
+   delete (fake_glx_screen *)psc;
    psc = NULL;
 
    GLXContext ctx =
@@ -210,7 +225,7 @@ TEST_F(glXCreateContextAttribARB_test, NULL_screen_returns_None)
 /*@{*/
 TEST_F(glXCreateContextAttribARB_test, does_send_protocol)
 {
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, NULL);
 
    EXPECT_TRUE(CreateContextAttribsARB_was_sent);
@@ -218,15 +233,14 @@ TEST_F(glXCreateContextAttribARB_test, does_send_protocol)
 
 TEST_F(glXCreateContextAttribARB_test, sent_correct_context)
 {
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, NULL);
-
    EXPECT_EQ(99u, req.context);
 }
 
 TEST_F(glXCreateContextAttribARB_test, sent_correct_fbconfig)
 {
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, NULL);
 
    EXPECT_EQ(0xbeefcafe, req.fbconfig);
@@ -240,16 +254,18 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_share_list)
 
    ASSERT_NE((GLXContext) 0, share);
 
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, share,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, share,
                              False, NULL);
 
    struct glx_context *glx_ctx = (struct glx_context *) share;
    EXPECT_EQ(glx_ctx->xid, req.share_list);
+
+   delete (fake_glx_context *)share;
 }
 
 TEST_F(glXCreateContextAttribARB_test, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_true)
 {
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              True, NULL);
 
    EXPECT_FALSE(req.is_direct);
@@ -257,7 +273,7 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_is_direct_for_indirect_scree
 
 TEST_F(glXCreateContextAttribARB_test, sent_correct_is_direct_for_indirect_screen_and_direct_set_to_false)
 {
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, NULL);
 
    EXPECT_FALSE(req.is_direct);
@@ -267,7 +283,7 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_is_direct_for_direct_screen_
 {
    this->use_direct_rendering_screen();
 
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              True, NULL);
 
    EXPECT_TRUE(req.is_direct);
@@ -277,7 +293,7 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_is_direct_for_direct_screen_
 {
    this->use_direct_rendering_screen();
 
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, NULL);
 
    EXPECT_FALSE(req.is_direct);
@@ -288,7 +304,7 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_screen)
    this->fbc.screen = 7;
    psc->scr = 7;
 
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, NULL);
 
    EXPECT_EQ(7u, req.screen);
@@ -311,7 +327,7 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_num_attribs)
       0, 0
    };
 
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, attribs);
 
    EXPECT_EQ(4u, req.num_attribs);
@@ -323,7 +339,7 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_num_attribs_empty_list)
       0,
    };
 
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, attribs);
 
    EXPECT_EQ(0u, req.num_attribs);
@@ -331,7 +347,7 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_num_attribs_empty_list)
 
 TEST_F(glXCreateContextAttribARB_test, sent_correct_num_attribs_NULL_list_pointer)
 {
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, NULL);
 
    EXPECT_EQ(0u, req.num_attribs);
@@ -346,7 +362,7 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_attrib_list)
       0
    };
 
-   glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                              False, attribs);
 
    for (unsigned i = 0; i < 6; i++) {
@@ -361,8 +377,7 @@ TEST_F(glXCreateContextAttribARB_test, sent_correct_attrib_list)
 /*@{*/
 TEST_F(glXCreateContextAttribARB_test, correct_context)
 {
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 False, NULL);
 
    /* Since the server did not return an error, the GLXContext should not be
@@ -377,8 +392,7 @@ TEST_F(glXCreateContextAttribARB_test, correct_context)
 
 TEST_F(glXCreateContextAttribARB_test, correct_context_xid)
 {
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 False, NULL);
 
    /* Since the server did not return an error, the GLXContext should not be
@@ -407,12 +421,14 @@ TEST_F(glXCreateContextAttribARB_test, correct_context_share_xid)
    struct glx_context *share = (struct glx_context *) first;
    struct glx_context *ctx = (struct glx_context *) second;
    EXPECT_EQ(share->xid, ctx->share_xid);
+
+   delete (fake_glx_context *)first;
+   delete (fake_glx_context *)second;
 }
 
 TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_indirect_screen_and_direct_set_to_true)
 {
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 True, NULL);
 
    ASSERT_NE((GLXContext) 0, ctx);
@@ -424,8 +440,7 @@ TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_indirect_scr
 
 TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_indirect_screen_and_direct_set_to_false)
 {
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 False, NULL);
 
    ASSERT_NE((GLXContext) 0, ctx);
@@ -439,8 +454,7 @@ TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_direct_scree
 {
    this->use_direct_rendering_screen();
 
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 True, NULL);
 
    ASSERT_NE((GLXContext) 0, ctx);
@@ -454,8 +468,7 @@ TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_direct_scree
 {
    this->use_direct_rendering_screen();
 
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 False, NULL);
 
    ASSERT_NE((GLXContext) 0, ctx);
@@ -467,8 +480,7 @@ TEST_F(glXCreateContextAttribARB_test, correct_context_isDirect_for_direct_scree
 
 TEST_F(glXCreateContextAttribARB_test, correct_indirect_context_client_state_private)
 {
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 False, NULL);
 
    ASSERT_NE((GLXContext) 0, ctx);
@@ -482,8 +494,7 @@ TEST_F(glXCreateContextAttribARB_test, correct_indirect_context_client_state_pri
 
 TEST_F(glXCreateContextAttribARB_test, correct_indirect_context_config)
 {
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 False, NULL);
 
    ASSERT_NE((GLXContext) 0, ctx);
@@ -498,8 +509,7 @@ TEST_F(glXCreateContextAttribARB_test, correct_context_screen_number)
    this->fbc.screen = 7;
    psc->scr = 7;
 
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 False, NULL);
 
    ASSERT_NE((GLXContext) 0, ctx);
@@ -511,8 +521,7 @@ TEST_F(glXCreateContextAttribARB_test, correct_context_screen_number)
 
 TEST_F(glXCreateContextAttribARB_test, correct_context_screen_pointer)
 {
-   GLXContext ctx =
-      glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
+   ctx = glXCreateContextAttribsARB(this->dpy, (GLXFBConfig) &this->fbc, 0,
                                 False, NULL);
 
    ASSERT_NE((GLXContext) 0, ctx);