]> granicus.if.org Git - php/commitdiff
fix #78624: session_gc return value for user defined session handlers
authorBrent Shaffer <betterbrent@google.com>
Wed, 2 Oct 2019 23:44:58 +0000 (16:44 -0700)
committerJoe Watkins <krakjoe@php.net>
Fri, 4 Oct 2019 04:12:02 +0000 (06:12 +0200)
NEWS
ext/session/mod_user.c
ext/session/tests/bug78624.phpt [new file with mode: 0644]
ext/session/tests/session_set_save_handler_basic.phpt

diff --git a/NEWS b/NEWS
index 550de2e252887e96e06a40fb4abee297e6070c8b..26abd33994ad939ef21e075337bef11b172e4926 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,10 @@ PHP                                                                        NEWS
   . Fixed bug #76809 (SSL settings aren't respected when persistent connections 
     are used). (fabiomsouto)
 
+- Session:
+  . Fixed bug #78624 (session_gc return value for user defined session 
+    handlers). (bshaffer)
+
 - Standard:
   . Fixed bug #76342 (file_get_contents waits twice specified timeout).
     (Thomas Calvet)
index 9ab0be13cd53d75478eabaf343440a5af2e85eec..897955166b56583610c4917df0dbdb00f7f91e89 100644 (file)
@@ -192,14 +192,15 @@ PS_GC_FUNC(user)
 
        if (Z_TYPE(retval) == IS_LONG) {
                convert_to_long(&retval);
-               return Z_LVAL(retval);
-       }
-       /* This is for older API compatibility */
-       if (Z_TYPE(retval) == IS_TRUE) {
-               return 1;
+               *nrdels = Z_LVAL(retval);
+       } else if (Z_TYPE(retval) == IS_TRUE) {
+               /* This is for older API compatibility */
+               *nrdels = 1;
+       } else {
+               /* Anything else is some kind of error */
+               *nrdels = -1; // Error
        }
-       /* Anything else is some kind of error */
-       return -1; // Error
+       return *nrdels;
 }
 
 PS_CREATE_SID_FUNC(user)
diff --git a/ext/session/tests/bug78624.phpt b/ext/session/tests/bug78624.phpt
new file mode 100644 (file)
index 0000000..9055b5a
--- /dev/null
@@ -0,0 +1,61 @@
+--TEST--
+Test session_set_save_handler() : session_gc() returns the number of deleted records.
+--INI--
+session.name=PHPSESSID
+session.save_handler=files
+--SKIPIF--
+<?php include('skipif.inc'); ?>
+--FILE--
+<?php
+
+ob_start();
+
+/*
+ * Prototype : bool session_set_save_handler(SessionHandler $handler [, bool $register_shutdown_function = true])
+ * Description : Sets user-level session storage functions
+ * Source code : ext/session/session.c
+ */
+
+echo "*** Test session_set_save_handler() : session_gc() returns the number of deleted records. ***\n";
+
+class MySession implements SessionHandlerInterface {
+    public function open($path, $name) {
+        echo 'Open', "\n";
+        return true;
+    }
+    public function read($key) {
+        echo 'Read ', session_id(), "\n";
+        return '';
+    }
+    public function write($key, $data) {
+        echo 'Write ', session_id(), "\n";
+        return true;
+    }
+    public function close() {
+        echo 'Close ', session_id(), "\n";
+        return true;
+    }
+    public function destroy($key) {
+        echo 'Destroy ', session_id(), "\n";
+        return true;
+    }
+    public function gc($ts) {
+        echo 'Garbage collect', "\n";
+        return 1;
+    }
+}
+
+$handler = new MySession;
+session_set_save_handler($handler);
+session_start();
+var_dump(session_gc());
+session_write_close();
+
+--EXPECTF--
+*** Test session_set_save_handler() : session_gc() returns the number of deleted records. ***
+Open
+Read %s
+Garbage collect
+int(1)
+Write %s
+Close %s
index bf77e77a6e3a3729ff48cbd240d208b3c031a915..bdfd928c9a9d050f0810a119ab6161414b44181c 100644 (file)
@@ -49,6 +49,12 @@ var_dump($_SESSION);
 $_SESSION['Bar'] = 'Foo';
 session_write_close();
 
+echo "Garbage collection..\n";
+session_id($session_id);
+session_start();
+var_dump(session_gc());
+session_write_close();
+
 echo "Cleanup..\n";
 session_id($session_id);
 session_start();
@@ -101,6 +107,12 @@ array(3) {
 }
 Write [%s,%s,Blah|s:12:"Hello World!";Foo|b:0;Guff|i:1234567890;Bar|s:3:"Foo";]
 Close [%s,PHPSESSID]
+Garbage collection..
+Open [%s,PHPSESSID]
+Read [%s,%s]
+int(0)
+Write [%s,%s,Blah|s:12:"Hello World!";Foo|b:0;Guff|i:1234567890;Bar|s:3:"Foo";]
+Close [%s,PHPSESSID]
 Cleanup..
 Open [%s,PHPSESSID]
 Read [%s,%s]