guard against uninitialized property write, too
[m6w6/ext-pq] / src / php_pqexc.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18
19 #include <Zend/zend_exceptions.h>
20 #include <ext/spl/spl_exceptions.h>
21
22 #include "php_pq.h"
23 #include "php_pq_object.h"
24 #include "php_pqexc.h"
25
26 static zend_class_entry *php_pqexc_interface_class_entry;
27 static zend_class_entry *php_pqexc_invalid_argument_class_entry;
28 static zend_class_entry *php_pqexc_runtime_class_entry;
29 static zend_class_entry *php_pqexc_bad_methodcall_class_entry;
30 static zend_class_entry *php_pqexc_domain_class_entry;
31
32 static zend_function_entry php_pqexc_methods[] = {
33 {0}
34 };
35
36 zend_class_entry *exce(php_pqexc_type_t type)
37 {
38 switch (type) {
39 default:
40 case EX_INVALID_ARGUMENT:
41 return php_pqexc_invalid_argument_class_entry;
42 case EX_RUNTIME:
43 case EX_CONNECTION_FAILED:
44 case EX_IO:
45 case EX_ESCAPE:
46 return php_pqexc_runtime_class_entry;
47 case EX_UNINITIALIZED:
48 case EX_BAD_METHODCALL:
49 return php_pqexc_bad_methodcall_class_entry;
50 case EX_DOMAIN:
51 case EX_SQL:
52 return php_pqexc_domain_class_entry;
53 }
54 }
55
56 zval *throw_exce(php_pqexc_type_t type TSRMLS_DC, const char *fmt, ...)
57 {
58 char *msg;
59 zval *zexc;
60 va_list argv;
61
62 va_start(argv, fmt);
63 vspprintf(&msg, 0, fmt, argv);
64 va_end(argv);
65
66 zexc = zend_throw_exception(exce(type), msg, type TSRMLS_CC);
67 efree(msg);
68
69 return zexc;
70 }
71
72 PHP_MINIT_FUNCTION(pqexc)
73 {
74 zend_class_entry ce = {0};
75
76 INIT_NS_CLASS_ENTRY(ce, "pq", "Exception", php_pqexc_methods);
77 php_pqexc_interface_class_entry = zend_register_internal_interface(&ce TSRMLS_CC);
78
79 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("INVALID_ARGUMENT"), EX_INVALID_ARGUMENT TSRMLS_CC);
80 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("RUNTIME"), EX_RUNTIME TSRMLS_CC);
81 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("CONNECTION_FAILED"), EX_CONNECTION_FAILED TSRMLS_CC);
82 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("IO"), EX_IO TSRMLS_CC);
83 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("ESCAPE"), EX_ESCAPE TSRMLS_CC);
84 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("BAD_METHODCALL"), EX_BAD_METHODCALL TSRMLS_CC);
85 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("UNINITIALIZED"), EX_UNINITIALIZED TSRMLS_CC);
86 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("DOMAIN"), EX_DOMAIN TSRMLS_CC);
87 zend_declare_class_constant_long(php_pqexc_interface_class_entry, ZEND_STRL("SQL"), EX_SQL TSRMLS_CC);
88
89 memset(&ce, 0, sizeof(ce));
90 INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "InvalidArgumentException", php_pqexc_methods);
91 php_pqexc_invalid_argument_class_entry = zend_register_internal_class_ex(&ce, spl_ce_InvalidArgumentException, "InvalidArgumentException" TSRMLS_CC);
92 zend_class_implements(php_pqexc_invalid_argument_class_entry TSRMLS_CC, 1, php_pqexc_interface_class_entry);
93
94 memset(&ce, 0, sizeof(ce));
95 INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "RuntimeException", php_pqexc_methods);
96 php_pqexc_runtime_class_entry = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException, "RuntimeException" TSRMLS_CC);
97 zend_class_implements(php_pqexc_runtime_class_entry TSRMLS_CC, 1, php_pqexc_interface_class_entry);
98
99 memset(&ce, 0, sizeof(ce));
100 INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "BadMethodCallException", php_pqexc_methods);
101 php_pqexc_bad_methodcall_class_entry = zend_register_internal_class_ex(&ce, spl_ce_BadMethodCallException, "BadMethodCallException" TSRMLS_CC);
102 zend_class_implements(php_pqexc_bad_methodcall_class_entry TSRMLS_CC, 1, php_pqexc_interface_class_entry);
103
104 memset(&ce, 0, sizeof(ce));
105 INIT_NS_CLASS_ENTRY(ce, "pq\\Exception", "DomainException", php_pqexc_methods);
106 php_pqexc_domain_class_entry = zend_register_internal_class_ex(&ce, spl_ce_DomainException, "DomainException" TSRMLS_CC);
107 zend_class_implements(php_pqexc_domain_class_entry TSRMLS_CC, 1, php_pqexc_interface_class_entry);
108 zend_declare_property_null(php_pqexc_domain_class_entry, ZEND_STRL("sqlstate"), ZEND_ACC_PUBLIC TSRMLS_CC);
109
110 return SUCCESS;
111 }
112
113 /*
114 * Local variables:
115 * tab-width: 4
116 * c-basic-offset: 4
117 * End:
118 * vim600: noet sw=4 ts=4 fdm=marker
119 * vim<600: noet sw=4 ts=4
120 */