Blame SOURCES/perl-5.18.2-Destroy-GDBM-NDBM-ODBM-SDBM-_File-objects-only-from-.patch

276c98
From f793042f2bac2ace9a5c0030b47b41c4db561a5b Mon Sep 17 00:00:00 2001
276c98
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
276c98
Date: Fri, 6 Jun 2014 14:31:59 +0200
276c98
Subject: [PATCH] Destroy {GDBM,NDBM,ODBM,SDBM}_File objects only from original
276c98
 thread context
276c98
MIME-Version: 1.0
276c98
Content-Type: text/plain; charset=UTF-8
276c98
Content-Transfer-Encoding: 8bit
276c98
276c98
This patch fixes a crash when destroing a hash tied to a *_File
276c98
database after spawning a thread:
276c98
276c98
use Fcntl;
276c98
use SDBM_File;
276c98
use threads;
276c98
tie(my %dbtest, 'SDBM_File', "test.db", O_RDWR|O_CREAT, 0666);
276c98
threads->new(sub {})->join;
276c98
276c98
This crashed or paniced depending on how perl was configured.
276c98
276c98
Closes RT#61912.
276c98
276c98
Signed-off-by: Petr Písař <ppisar@redhat.com>
276c98
---
276c98
 ext/GDBM_File/GDBM_File.xs | 16 ++++++++++------
276c98
 ext/NDBM_File/NDBM_File.xs | 16 ++++++++++------
276c98
 ext/ODBM_File/ODBM_File.xs | 18 +++++++++++-------
276c98
 ext/SDBM_File/SDBM_File.xs |  4 +++-
276c98
 t/lib/dbmt_common.pl       | 35 +++++++++++++++++++++++++++++++++++
276c98
 5 files changed, 69 insertions(+), 20 deletions(-)
276c98
276c98
diff --git a/ext/GDBM_File/GDBM_File.xs b/ext/GDBM_File/GDBM_File.xs
276c98
index 33e08e2..7160f54 100644
276c98
--- a/ext/GDBM_File/GDBM_File.xs
276c98
+++ b/ext/GDBM_File/GDBM_File.xs
276c98
@@ -13,6 +13,7 @@
276c98
 #define store_value 3
276c98
 
276c98
 typedef struct {
276c98
+	tTHX    owner;
276c98
 	GDBM_FILE 	dbp ;
276c98
 	SV *    filter[4];
276c98
 	int     filtering ;
276c98
@@ -89,6 +90,7 @@ gdbm_TIEHASH(dbtype, name, read_write, mode)
276c98
 	    if ((dbp =  gdbm_open(name, GDBM_BLOCKSIZE, read_write, mode,
276c98
 	       	     	          (FATALFUNC) croak_string))) {
276c98
 	        RETVAL = (GDBM_File)safecalloc(1, sizeof(GDBM_File_type)) ;
276c98
+		RETVAL->owner = aTHX;
276c98
 		RETVAL->dbp = dbp ;
276c98
 	    }
276c98
 	    
276c98
@@ -109,12 +111,14 @@ gdbm_DESTROY(db)
276c98
 	PREINIT:
276c98
 	int i = store_value;
276c98
 	CODE:
276c98
-	gdbm_close(db);
276c98
-	do {
276c98
-	    if (db->filter[i])
276c98
-		SvREFCNT_dec(db->filter[i]);
276c98
-	} while (i-- > 0);
276c98
-	safefree(db);
276c98
+	if (db && db->owner == aTHX) {
276c98
+	    gdbm_close(db);
276c98
+	    do {
276c98
+		if (db->filter[i])
276c98
+		    SvREFCNT_dec(db->filter[i]);
276c98
+	    } while (i-- > 0);
276c98
+	    safefree(db);
276c98
+	}
276c98
 
276c98
 #define gdbm_FETCH(db,key)			gdbm_fetch(db->dbp,key)
276c98
 datum_value
276c98
diff --git a/ext/NDBM_File/NDBM_File.xs b/ext/NDBM_File/NDBM_File.xs
276c98
index 52e60fc..af223e5 100644
276c98
--- a/ext/NDBM_File/NDBM_File.xs
276c98
+++ b/ext/NDBM_File/NDBM_File.xs
276c98
@@ -33,6 +33,7 @@ END_EXTERN_C
276c98
 #define store_value 3
276c98
 
276c98
 typedef struct {
276c98
+	tTHX    owner;
276c98
 	DBM * 	dbp ;
276c98
 	SV *    filter[4];
276c98
 	int     filtering ;
276c98
@@ -71,6 +72,7 @@ ndbm_TIEHASH(dbtype, filename, flags, mode)
276c98
 	    RETVAL = NULL ;
276c98
 	    if ((dbp =  dbm_open(filename, flags, mode))) {
276c98
 	        RETVAL = (NDBM_File)safecalloc(1, sizeof(NDBM_File_type));
276c98
+		RETVAL->owner = aTHX;
276c98
 		RETVAL->dbp = dbp ;
276c98
 	    }
276c98
 	    
276c98
@@ -84,12 +86,14 @@ ndbm_DESTROY(db)
276c98
 	PREINIT:
276c98
 	int i = store_value;
276c98
 	CODE:
276c98
-	dbm_close(db->dbp);
276c98
-	do {
276c98
-	    if (db->filter[i])
276c98
-		SvREFCNT_dec(db->filter[i]);
276c98
-	} while (i-- > 0);
276c98
-	safefree(db);
276c98
+	if (db && db->owner == aTHX) {
276c98
+	    dbm_close(db->dbp);
276c98
+	    do {
276c98
+		if (db->filter[i])
276c98
+		    SvREFCNT_dec(db->filter[i]);
276c98
+	    } while (i-- > 0);
276c98
+	    safefree(db);
276c98
+	}
276c98
 
276c98
 #define ndbm_FETCH(db,key)			dbm_fetch(db->dbp,key)
276c98
 datum_value
276c98
diff --git a/ext/ODBM_File/ODBM_File.xs b/ext/ODBM_File/ODBM_File.xs
276c98
index d1ece7f..f7e00a0 100644
276c98
--- a/ext/ODBM_File/ODBM_File.xs
276c98
+++ b/ext/ODBM_File/ODBM_File.xs
276c98
@@ -45,6 +45,7 @@ datum	nextkey(datum key);
276c98
 #define store_value 3
276c98
 
276c98
 typedef struct {
276c98
+	tTHX    owner;
276c98
 	void * 	dbp ;
276c98
 	SV *    filter[4];
276c98
 	int     filtering ;
276c98
@@ -112,6 +113,7 @@ odbm_TIEHASH(dbtype, filename, flags, mode)
276c98
 	    }
276c98
 	    dbp = (void*)(dbminit(filename) >= 0 ? &dbmrefcnt : 0);
276c98
 	    RETVAL = (ODBM_File)safecalloc(1, sizeof(ODBM_File_type));
276c98
+	    RETVAL->owner = aTHX;
276c98
 	    RETVAL->dbp = dbp ;
276c98
 	}
276c98
 	OUTPUT:
276c98
@@ -124,13 +126,15 @@ DESTROY(db)
276c98
 	dMY_CXT;
276c98
 	int i = store_value;
276c98
 	CODE:
276c98
-	dbmrefcnt--;
276c98
-	dbmclose();
276c98
-	do {
276c98
-	    if (db->filter[i])
276c98
-		SvREFCNT_dec(db->filter[i]);
276c98
-	} while (i-- > 0);
276c98
-	safefree(db);
276c98
+	if (db && db->owner == aTHX) {
276c98
+	    dbmrefcnt--;
276c98
+	    dbmclose();
276c98
+	    do {
276c98
+		if (db->filter[i])
276c98
+		    SvREFCNT_dec(db->filter[i]);
276c98
+	    } while (i-- > 0);
276c98
+	    safefree(db);
276c98
+	}
276c98
 
276c98
 datum_value
276c98
 odbm_FETCH(db, key)
276c98
diff --git a/ext/SDBM_File/SDBM_File.xs b/ext/SDBM_File/SDBM_File.xs
276c98
index 291e41b..0bdae9a 100644
276c98
--- a/ext/SDBM_File/SDBM_File.xs
276c98
+++ b/ext/SDBM_File/SDBM_File.xs
276c98
@@ -10,6 +10,7 @@
276c98
 #define store_value 3
276c98
 
276c98
 typedef struct {
276c98
+	tTHX    owner;
276c98
 	DBM * 	dbp ;
276c98
 	SV *    filter[4];
276c98
 	int     filtering ;
276c98
@@ -49,6 +50,7 @@ sdbm_TIEHASH(dbtype, filename, flags, mode)
276c98
 	    }
276c98
 	    if (dbp) {
276c98
 	        RETVAL = (SDBM_File)safecalloc(1, sizeof(SDBM_File_type));
276c98
+		RETVAL->owner = aTHX;
276c98
 		RETVAL->dbp = dbp ;
276c98
 	    }
276c98
 	    
276c98
@@ -60,7 +62,7 @@ void
276c98
 sdbm_DESTROY(db)
276c98
 	SDBM_File	db
276c98
 	CODE:
276c98
-	if (db) {
276c98
+	if (db && db->owner == aTHX) {
276c98
 	    int i = store_value;
276c98
 	    sdbm_close(db->dbp);
276c98
 	    do {
276c98
diff --git a/t/lib/dbmt_common.pl b/t/lib/dbmt_common.pl
276c98
index 5d4098c..a0a4d52 100644
276c98
--- a/t/lib/dbmt_common.pl
276c98
+++ b/t/lib/dbmt_common.pl
276c98
@@ -511,5 +511,40 @@ unlink <Op_dbmx*>, $Dfile;
276c98
    unlink <Op1_dbmx*>;
276c98
 }
276c98
 
276c98
+{
276c98
+   # Check DBM back-ends do not destroy objects from then-spawned threads.
276c98
+   # RT#61912.
276c98
+   SKIP: {
276c98
+      my $threads_count = 2;
276c98
+      skip 'Threads are disabled', 3 + 2 * $threads_count
276c98
+        unless $Config{usethreads};
276c98
+      use_ok('threads');
276c98
+
276c98
+      my %h;
276c98
+      unlink <Op1_dbmx*>;
276c98
+
276c98
+      my $db = tie %h, $DBM_Class, 'Op1_dbmx', $create, 0640;
276c98
+      isa_ok($db, $DBM_Class);
276c98
+
276c98
+      for (1 .. 2) {
276c98
+         ok(threads->create(
276c98
+            sub {
276c98
+               $SIG{'__WARN__'} = sub { fail(shift) }; # debugging perl panics
276c98
+                        # report it by spurious TAP line
276c98
+               1;
276c98
+            }), "Thread $_ created");
276c98
+      }
276c98
+      for (threads->list) {
276c98
+         is($_->join, 1, "A thread exited successfully");
276c98
+      }
276c98
+
276c98
+      pass("Tied object survived exiting threads");
276c98
+
276c98
+      undef $db;
276c98
+      untie %h;
276c98
+      unlink <Op1_dbmx*>;
276c98
+   }
276c98
+}
276c98
+
276c98
 done_testing();
276c98
 1;
276c98
-- 
276c98
1.9.3
276c98