xref: /php-src/ext/mysqli/mysqli_warning.c (revision d84dfa32)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Georg Richter <georg@php.net>                                |
14   +----------------------------------------------------------------------+
15 
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include <signal.h>
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "php_mysqli_structs.h"
27 #include "mysqli_priv.h"
28 
29 /* Define these in the PHP7 tree to make merging easy process */
30 #define ZSTR_DUPLICATE (1<<0)
31 #define ZSTR_AUTOFREE  (1<<1)
32 
33 #define ZVAL_UTF8_STRING(z, s, flags)          ZVAL_STRING((z), (char*)(s))
34 #define ZVAL_UTF8_STRINGL(z, s, l, flags)      ZVAL_STRINGL((z), (char*)(s), (l))
35 
36 /* {{{ void php_clear_warnings() */
php_clear_warnings(MYSQLI_WARNING * w)37 void php_clear_warnings(MYSQLI_WARNING *w)
38 {
39 	MYSQLI_WARNING *n;
40 
41 	while (w) {
42 		n = w;
43 		zval_ptr_dtor_str(&(w->reason));
44 		zval_ptr_dtor_str(&(w->sqlstate));
45 		w = w->next;
46 		efree(n);
47 	}
48 }
49 /* }}} */
50 
51 /* {{{ MYSQLI_WARNING *php_new_warning */
52 static
php_new_warning(zval * reason,int errorno)53 MYSQLI_WARNING *php_new_warning(zval * reason, int errorno)
54 {
55 	MYSQLI_WARNING *w;
56 
57 	w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
58 
59 	ZVAL_COPY(&w->reason, reason);
60 	convert_to_string(&w->reason);
61 
62 	ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1,  ZSTR_DUPLICATE);
63 
64 	w->errorno = errorno;
65 
66 	return w;
67 }
68 /* }}} */
69 
70 /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) */
php_get_warnings(MYSQLND_CONN_DATA * mysql)71 MYSQLI_WARNING * php_get_warnings(MYSQLND_CONN_DATA * mysql)
72 {
73 	MYSQLI_WARNING	*w, *first = NULL, *prev = NULL;
74 	MYSQL_RES		*result;
75 	zval			row;
76 
77 	if (mysql->m->query(mysql, "SHOW WARNINGS", 13)) {
78 		return NULL;
79 	}
80 
81 	result = mysql->m->use_result(mysql);
82 
83 	for (;;) {
84 		zval *entry;
85 		int mysqli_errno;
86 
87 		mysqlnd_fetch_into(result, MYSQLND_FETCH_NUM, &row);
88 		if (Z_TYPE(row) != IS_ARRAY) {
89 			zval_ptr_dtor(&row);
90 			break;
91 		}
92 		zend_hash_internal_pointer_reset(Z_ARRVAL(row));
93 		/* 0. we don't care about the first */
94 		zend_hash_move_forward(Z_ARRVAL(row));
95 
96 		/* 1. Here comes the error no */
97 		entry = zend_hash_get_current_data(Z_ARRVAL(row));
98 		mysqli_errno = zval_get_long(entry);
99 		zend_hash_move_forward(Z_ARRVAL(row));
100 
101 		/* 2. Here comes the reason */
102 		entry = zend_hash_get_current_data(Z_ARRVAL(row));
103 
104 		w = php_new_warning(entry, mysqli_errno);
105 		/*
106 		  Don't destroy entry, because the row destroy will decrease
107 		  the refcounter. Decreased twice then mysqlnd_free_result()
108 		  will crash, because it will try to access already freed memory.
109 		*/
110 		if (!first) {
111 			first = w;
112 		}
113 		if (prev) {
114 			prev->next = (void *)w;
115 		}
116 		prev = w;
117 
118 		zval_ptr_dtor(&row);
119 	}
120 
121 	mysql_free_result(result);
122 	return first;
123 }
124 /* }}} */
125 
126 /* {{{ bool mysqli_warning::next() */
PHP_METHOD(mysqli_warning,next)127 PHP_METHOD(mysqli_warning, next)
128 {
129 	MYSQLI_WARNING 	*w;
130 	mysqli_object *obj = Z_MYSQLI_P(ZEND_THIS);
131 
132 	if (zend_parse_parameters_none() == FAILURE) {
133 		RETURN_THROWS();
134 	}
135 
136 	if (obj->ptr) {
137 		MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, ZEND_THIS, "mysqli_warning", MYSQLI_STATUS_VALID);
138 
139 		if (w && w->next) {
140 			w = w->next;
141 	        ((MYSQLI_RESOURCE *)(obj->ptr))->ptr = w;
142 			RETURN_TRUE;
143 		}
144 	}
145 	RETURN_FALSE;
146 }
147 /* }}} */
148 
149 /* {{{ property mysqli_warning_message */
mysqli_warning_message(mysqli_object * obj,zval * retval,bool quiet)150 static int mysqli_warning_message(mysqli_object *obj, zval *retval, bool quiet)
151 {
152 	MYSQLI_WARNING *w;
153 
154 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
155 		if (!quiet) {
156 			zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
157 		}
158 
159 		return FAILURE;
160 	}
161 
162 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
163 	ZVAL_COPY(retval, &w->reason);
164 
165 	return SUCCESS;
166 }
167 /* }}} */
168 
169 /* {{{ property mysqli_warning_sqlstate */
mysqli_warning_sqlstate(mysqli_object * obj,zval * retval,bool quiet)170 static int mysqli_warning_sqlstate(mysqli_object *obj, zval *retval, bool quiet)
171 {
172 	MYSQLI_WARNING *w;
173 
174 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
175 		if (!quiet) {
176 			zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
177 		}
178 
179 		return FAILURE;
180 	}
181 
182 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
183 	ZVAL_COPY(retval, &w->sqlstate);
184 
185 	return SUCCESS;
186 }
187 /* }}} */
188 
189 /* {{{ property mysqli_warning_error */
mysqli_warning_errno(mysqli_object * obj,zval * retval,bool quiet)190 static int mysqli_warning_errno(mysqli_object *obj, zval *retval, bool quiet)
191 {
192 	MYSQLI_WARNING *w;
193 
194 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
195 		if (!quiet) {
196 			zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
197 		}
198 
199 		return FAILURE;
200 	}
201 
202 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
203 	ZVAL_LONG(retval, w->errorno);
204 
205 	return SUCCESS;
206 }
207 /* }}} */
208 
PHP_METHOD(mysqli_warning,__construct)209 PHP_METHOD(mysqli_warning, __construct)
210 {
211 	ZEND_PARSE_PARAMETERS_NONE();
212 
213 	zend_throw_error(NULL, "Cannot directly construct mysqli_warning");
214 }
215 
216 /* {{{ mysqli_warning_property_entries */
217 const mysqli_property_entry mysqli_warning_property_entries[] = {
218 	{"message", sizeof("message") - 1, mysqli_warning_message, NULL},
219 	{"sqlstate", sizeof("sqlstate") - 1, mysqli_warning_sqlstate, NULL},
220 	{"errno", sizeof("errno") - 1, mysqli_warning_errno, NULL},
221 	{NULL, 0, NULL, NULL}
222 };
223 /* }}} */
224