scottmac                Tue Feb 10 00:24:33 2009 UTC

  Modified files:              (Branch: PHP_5_3)
    /php-src/ext/sqlite3        sqlite3.c 
    /php-src/ext/sqlite3/tests  sqlite3_15_open_error.phpt 
                                sqlite3_21_security.phpt 
  Log:
  The constructor should also throw exceptions, make this semi useful now.
  
  
  
http://cvs.php.net/viewvc.cgi/php-src/ext/sqlite3/sqlite3.c?r1=1.1.2.31&r2=1.1.2.32&diff_format=u
Index: php-src/ext/sqlite3/sqlite3.c
diff -u php-src/ext/sqlite3/sqlite3.c:1.1.2.31 
php-src/ext/sqlite3/sqlite3.c:1.1.2.32
--- php-src/ext/sqlite3/sqlite3.c:1.1.2.31      Wed Jan 28 23:58:09 2009
+++ php-src/ext/sqlite3/sqlite3.c       Tue Feb 10 00:24:33 2009
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: sqlite3.c,v 1.1.2.31 2009/01/28 23:58:09 scottmac Exp $ */
+/* $Id: sqlite3.c,v 1.1.2.32 2009/02/10 00:24:33 scottmac Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -72,29 +72,38 @@
        char *filename, *encryption_key, *fullpath;
        int filename_len, encryption_key_len = 0;
        long flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
+       zend_error_handling error_handling;
+
        db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object 
TSRMLS_CC);
+       zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
 
        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", 
&filename, &filename_len, &flags, &encryption_key, &encryption_key_len)) {
+               zend_restore_error_handling(&error_handling TSRMLS_CC);
                return;
        }
 
+       zend_restore_error_handling(&error_handling TSRMLS_CC);
+
        if (db_obj->initialised) {
-               php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Already initialised 
DB Object");
+               zend_throw_exception(zend_exception_get_default(TSRMLS_C), 
"Already initialised DB Object", 0 TSRMLS_CC);
        }
 
        if (strncmp(filename, ":memory:", 8) != 0) {
                if (!(fullpath = expand_filepath(filename, NULL TSRMLS_CC))) {
-                       RETURN_FALSE;
+                       
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to expand 
filepath", 0 TSRMLS_CC);
+                       return;
                }
 
                if (PG(safe_mode) && (!php_checkuid(fullpath, NULL, 
CHECKUID_CHECK_FILE_AND_DIR))) {
+                       
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, 
"safe_mode prohibits opening %s", fullpath);
                        efree(fullpath);
-                       RETURN_FALSE;
+                       return;
                }
 
                if (php_check_open_basedir(fullpath TSRMLS_CC)) {
+                       
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, 
"open_basedir prohibits opening %s", fullpath);
                        efree(fullpath);
-                       RETURN_FALSE;
+                       return;
                }
        } else {
                fullpath = estrdup(filename);
@@ -105,7 +114,7 @@
 #else
        if (sqlite3_open(fullpath, &(db_obj->db)) != SQLITE_OK) {
 #endif
-               php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unable to open 
database: %s", sqlite3_errmsg(db_obj->db));
+               zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 
TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
                if (fullpath) {
                        efree(fullpath);
                }
@@ -117,6 +126,7 @@
 #if SQLITE_HAS_CODEC
        if (encryption_key_len > 0) {
                if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) 
!= SQLITE_OK) {
+                       
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, 
"Unable to open database: %s", sqlite3_errmsg(db_obj->db));
                        return;
                }
        }
@@ -129,7 +139,6 @@
        if (fullpath) {
                efree(fullpath);
        }
-       RETURN_TRUE;
 }
 /* }}} */
 
http://cvs.php.net/viewvc.cgi/php-src/ext/sqlite3/tests/sqlite3_15_open_error.phpt?r1=1.1.2.2&r2=1.1.2.3&diff_format=u
Index: php-src/ext/sqlite3/tests/sqlite3_15_open_error.phpt
diff -u php-src/ext/sqlite3/tests/sqlite3_15_open_error.phpt:1.1.2.2 
php-src/ext/sqlite3/tests/sqlite3_15_open_error.phpt:1.1.2.3
--- php-src/ext/sqlite3/tests/sqlite3_15_open_error.phpt:1.1.2.2        Sun Jul 
27 00:05:36 2008
+++ php-src/ext/sqlite3/tests/sqlite3_15_open_error.phpt        Tue Feb 10 
00:24:33 2009
@@ -7,14 +7,17 @@
 $unreadable = dirname(__FILE__) . '/unreadable.db';
 touch($unreadable);
 chmod($unreadable,  0200);
-$db = new SQLite3($unreadable);
-
-var_dump($db);
+try {
+       $db = new SQLite3($unreadable);
+} catch (Exception $e) {
+       echo $e . "\n";
+}
 echo "Done\n";
 unlink($unreadable);
 ?>
 --EXPECTF--
-Notice: SQLite3::__construct(): Unable to open database: unable to open 
database file in %s/sqlite3_15_open_error.php on line %d
-object(SQLite3)#%d (0) {
-}
+exception 'Exception' with message 'Unable to open database: unable to open 
database file' in %s/sqlite3_15_open_error.php:%d
+Stack trace:
+#0 %s/sqlite3_15_open_error.php(%d): SQLite3->__construct('%s')
+#1 {main}
 Done
http://cvs.php.net/viewvc.cgi/php-src/ext/sqlite3/tests/sqlite3_21_security.phpt?r1=1.1.2.2&r2=1.1.2.3&diff_format=u
Index: php-src/ext/sqlite3/tests/sqlite3_21_security.phpt
diff -u php-src/ext/sqlite3/tests/sqlite3_21_security.phpt:1.1.2.2 
php-src/ext/sqlite3/tests/sqlite3_21_security.phpt:1.1.2.3
--- php-src/ext/sqlite3/tests/sqlite3_21_security.phpt:1.1.2.2  Sun Jul 27 
00:05:36 2008
+++ php-src/ext/sqlite3/tests/sqlite3_21_security.phpt  Tue Feb 10 00:24:33 2009
@@ -16,8 +16,11 @@
 unlink($directory . $file);
 
 echo "Above test directory\n";
-$db = new SQLite3('../bad' . $file);
-var_dump($db);
+try {
+       $db = new SQLite3('../bad' . $file);
+} catch (Exception $e) {
+       echo $e . "\n";
+}
 
 echo "Done\n";
 ?>
@@ -28,7 +31,9 @@
 bool(true)
 Above test directory
 
-Warning: SQLite3::__construct(): open_basedir restriction in effect. File(%s) 
is not within the allowed path(s): (.) in %s on line %d
-object(SQLite3)#%d (0) {
-}
+Warning: SQLite3::__construct(): open_basedir restriction in effect. File(%s) 
is not within the allowed path(s): (.) in %s/sqlite3_21_security.php on line %d
+exception 'Exception' with message 'open_basedir prohibits opening %s' in 
%s/sqlite3_21_security.php:%d
+Stack trace:
+#0 %s/sqlite3_21_security.php(%d): SQLite3->__construct('%s')
+#1 {main}
 Done



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to