This is an automated email from the ASF dual-hosted git repository.

yjhjstz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit fffa69a19cc7ea6fecb51cc149457b039e5040e7
Author: Soumyadeep Chakraborty <[email protected]>
AuthorDate: Tue Aug 23 17:35:12 2022 -0700

    DatumStreamWrite teardown: NULL out after pfree
    
    We weren't doing so before and we recently got a complaint about a
    double free situation with DatumStreamBlockWrite->datum_buffer. Snuff
    out that possibility and similar possibilities.
    
    Co-authored-by: Lei (Alexandra) Wang <[email protected]>
    Co-authored-by: Ashwin Agrawal <[email protected]>
---
 src/backend/access/aocs/aocsam.c                 |  1 +
 src/backend/catalog/pg_compression.c             |  1 +
 src/backend/cdb/cdbappendonlystoragewrite.c      |  2 ++
 src/backend/utils/datumstream/datumstream.c      |  1 +
 src/backend/utils/datumstream/datumstreamblock.c | 22 +++++++++++++++++++++-
 5 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/backend/access/aocs/aocsam.c b/src/backend/access/aocs/aocsam.c
index dfc21d117e..482d4c2562 100644
--- a/src/backend/access/aocs/aocsam.c
+++ b/src/backend/access/aocs/aocsam.c
@@ -2250,6 +2250,7 @@ aocs_addcol_finish(AOCSAddColumnDesc desc)
        for (i = 0; i < desc->num_newcols; ++i)
                destroy_datumstreamwrite(desc->dsw[i]);
        pfree(desc->dsw);
+       desc->dsw = NULL;
 
        pfree(desc);
 }
diff --git a/src/backend/catalog/pg_compression.c 
b/src/backend/catalog/pg_compression.c
index d4d1fcf0e0..00af5ac638 100644
--- a/src/backend/catalog/pg_compression.c
+++ b/src/backend/catalog/pg_compression.c
@@ -263,6 +263,7 @@ zlib_destructor(PG_FUNCTION_ARGS)
        if (cs != NULL && cs->opaque != NULL)
        {
                pfree(cs->opaque);
+               cs->opaque = NULL;
        }
 
        PG_RETURN_VOID();
diff --git a/src/backend/cdb/cdbappendonlystoragewrite.c 
b/src/backend/cdb/cdbappendonlystoragewrite.c
index 76684f7e9a..52f44c4982 100755
--- a/src/backend/cdb/cdbappendonlystoragewrite.c
+++ b/src/backend/cdb/cdbappendonlystoragewrite.c
@@ -233,11 +233,13 @@ 
AppendOnlyStorageWrite_FinishSession(AppendOnlyStorageWrite *storageWrite)
                if (storageWrite->compressionState != NULL)
                {
                        pfree(storageWrite->compressionState);
+                       storageWrite->compressionState = NULL;
                }
 
                if (storageWrite->verifyWriteCompressionState != NULL)
                {
                        
callCompressionDestructor(storageWrite->compression_functions[COMPRESSION_DESTRUCTOR],
 storageWrite->verifyWriteCompressionState);
+                       storageWrite->verifyWriteCompressionState = NULL;
                }
        }
 
diff --git a/src/backend/utils/datumstream/datumstream.c 
b/src/backend/utils/datumstream/datumstream.c
index 8fc8cec954..24b387f286 100644
--- a/src/backend/utils/datumstream/datumstream.c
+++ b/src/backend/utils/datumstream/datumstream.c
@@ -776,6 +776,7 @@ destroy_datumstreamwrite(DatumStreamWrite * ds)
        if (ds->title)
        {
                pfree(ds->title);
+               ds->title = NULL;
        }
        pfree(ds);
 }
diff --git a/src/backend/utils/datumstream/datumstreamblock.c 
b/src/backend/utils/datumstream/datumstreamblock.c
index 2076cfbd46..76783e28fd 100755
--- a/src/backend/utils/datumstream/datumstreamblock.c
+++ b/src/backend/utils/datumstream/datumstreamblock.c
@@ -4487,25 +4487,45 @@ DatumStreamBlockWrite_Finish(
 
        oldCtxt = MemoryContextSwitchTo(dsw->memctxt);
        if (dsw->null_bitmap_buffer != NULL)
+       {
                pfree(dsw->null_bitmap_buffer);
+               dsw->null_bitmap_buffer = NULL;
+       }
 
-       if (dsw->datum_buffer != NULL)
+       if (dsw->datum_buffer != NULL) {
                pfree(dsw->datum_buffer);
+               dsw->datum_buffer = NULL;
+       }
 
        if (dsw->rle_compress_bitmap_buffer != NULL)
+       {
                pfree(dsw->rle_compress_bitmap_buffer);
+               dsw->rle_compress_bitmap_buffer = NULL;
+       }
 
        if (dsw->rle_repeatcounts != NULL)
+       {
                pfree(dsw->rle_repeatcounts);
+               dsw->rle_repeatcounts = NULL;
+       }
 
        if (dsw->delta_bitmap_buffer != NULL)
+       {
                pfree(dsw->delta_bitmap_buffer);
+               dsw->delta_bitmap_buffer = NULL;
+       }
 
        if (dsw->deltas != NULL)
+       {
                pfree(dsw->deltas);
+               dsw->deltas = NULL;
+       }
 
        if (dsw->delta_sign != NULL)
+       {
                pfree(dsw->delta_sign);
+               dsw->delta_sign = NULL;
+       }
 
        MemoryContextSwitchTo(oldCtxt);
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to