xref: /PHP-7.2/ext/mysqli/mysqli_warning.c (revision 7a7ec01a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Georg Richter <georg@php.net>                                |
16   +----------------------------------------------------------------------+
17 
18 */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include <signal.h>
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "php_mysqli_structs.h"
29 #include "mysqli_priv.h"
30 
31 /* Define these in the PHP7 tree to make merging easy process */
32 #define ZSTR_DUPLICATE (1<<0)
33 #define ZSTR_AUTOFREE  (1<<1)
34 
35 #define ZVAL_UTF8_STRING(z, s, flags)          ZVAL_STRING((z), (char*)(s))
36 #define ZVAL_UTF8_STRINGL(z, s, l, flags)      ZVAL_STRINGL((z), (char*)(s), (l))
37 
38 /* {{{ void php_clear_warnings() */
php_clear_warnings(MYSQLI_WARNING * w)39 void php_clear_warnings(MYSQLI_WARNING *w)
40 {
41 	MYSQLI_WARNING *n;
42 
43 	while (w) {
44 		n = w;
45 		zval_dtor(&(w->reason));
46 		zval_dtor(&(w->sqlstate));
47 		w = w->next;
48 		efree(n);
49 	}
50 }
51 /* }}} */
52 
53 #ifndef MYSQLI_USE_MYSQLND
54 /* {{{ MYSQLI_WARNING *php_new_warning */
55 static
php_new_warning(const char * reason,int errorno)56 MYSQLI_WARNING *php_new_warning(const char *reason, int errorno)
57 {
58 	MYSQLI_WARNING *w;
59 
60 	w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
61 
62 	ZVAL_UTF8_STRING(&(w->reason), reason, ZSTR_DUPLICATE);
63 
64 	ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1,  ZSTR_DUPLICATE);
65 
66 	w->errorno = errorno;
67 
68 	return w;
69 }
70 /* }}} */
71 
72 /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) */
php_get_warnings(MYSQL * mysql)73 MYSQLI_WARNING *php_get_warnings(MYSQL *mysql)
74 {
75 	MYSQLI_WARNING *w, *first = NULL, *prev = NULL;
76 	MYSQL_RES		*result;
77 	MYSQL_ROW 		row;
78 
79 	if (mysql_real_query(mysql, "SHOW WARNINGS", 13)) {
80 		return NULL;
81 	}
82 
83 	result = mysql_store_result(mysql);
84 
85 	while ((row = mysql_fetch_row(result))) {
86 		w = php_new_warning(row[2], atoi(row[1]));
87 		if (!first) {
88 			first = w;
89 		}
90 		if (prev) {
91 			prev->next = w;
92 		}
93 		prev = w;
94 	}
95 	mysql_free_result(result);
96 	return first;
97 }
98 /* }}} */
99 #else
100 /* {{{ MYSQLI_WARNING *php_new_warning */
101 static
php_new_warning(zval * reason,int errorno)102 MYSQLI_WARNING *php_new_warning(zval * reason, int errorno)
103 {
104 	MYSQLI_WARNING *w;
105 
106 	w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
107 
108 	ZVAL_COPY(&w->reason, reason);
109 	convert_to_string(&w->reason);
110 
111 	ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1,  ZSTR_DUPLICATE);
112 
113 	w->errorno = errorno;
114 
115 	return w;
116 }
117 /* }}} */
118 
119 /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) */
php_get_warnings(MYSQLND_CONN_DATA * mysql)120 MYSQLI_WARNING * php_get_warnings(MYSQLND_CONN_DATA * mysql)
121 {
122 	MYSQLI_WARNING	*w, *first = NULL, *prev = NULL;
123 	MYSQL_RES		*result;
124 	zval			row;
125 
126 	if (mysql->m->query(mysql, "SHOW WARNINGS", 13)) {
127 		return NULL;
128 	}
129 
130 	result = mysql->m->use_result(mysql, 0);
131 
132 	for (;;) {
133 		zval *entry;
134 		int errno;
135 
136 		mysqlnd_fetch_into(result, MYSQLND_FETCH_NUM, &row, MYSQLND_MYSQLI);
137 		if (Z_TYPE(row) != IS_ARRAY) {
138 			zval_ptr_dtor(&row);
139 			break;
140 		}
141 		zend_hash_internal_pointer_reset(Z_ARRVAL(row));
142 		/* 0. we don't care about the first */
143 		zend_hash_move_forward(Z_ARRVAL(row));
144 
145 		/* 1. Here comes the error no */
146 		entry = zend_hash_get_current_data(Z_ARRVAL(row));
147 		convert_to_long_ex(entry);
148 		errno = Z_LVAL_P(entry);
149 		zend_hash_move_forward(Z_ARRVAL(row));
150 
151 		/* 2. Here comes the reason */
152 		entry = zend_hash_get_current_data(Z_ARRVAL(row));
153 
154 		w = php_new_warning(entry, errno);
155 		/*
156 		  Don't destroy entry, because the row destroy will decrease
157 		  the refcounter. Decreased twice then mysqlnd_free_result()
158 		  will crash, because it will try to access already freed memory.
159 		*/
160 		if (!first) {
161 			first = w;
162 		}
163 		if (prev) {
164 			prev->next = (void *)w;
165 		}
166 		prev = w;
167 
168 		zval_ptr_dtor(&row);
169 	}
170 
171 	mysql_free_result(result);
172 	return first;
173 }
174 /* }}} */
175 #endif
176 
177 /* {{{ bool mysqli_warning::next() */
PHP_METHOD(mysqli_warning,next)178 PHP_METHOD(mysqli_warning, next)
179 {
180 	MYSQLI_WARNING 	*w;
181 	zval  			*mysqli_warning;
182 	mysqli_object *obj = Z_MYSQLI_P(getThis());
183 
184 	if (obj->ptr) {
185 		if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
186 										 &mysqli_warning, mysqli_warning_class_entry) == FAILURE) {
187 			return;
188 		}
189 
190 		MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, mysqli_warning, "mysqli_warning", MYSQLI_STATUS_VALID);
191 
192 		if (w && w->next) {
193 			w = w->next;
194 	        ((MYSQLI_RESOURCE *)(obj->ptr))->ptr = w;
195 			RETURN_TRUE;
196 		}
197 	}
198 	RETURN_FALSE;
199 }
200 /* }}} */
201 
202 /* {{{ property mysqli_warning_message */
203 static
mysqli_warning_message(mysqli_object * obj,zval * retval)204 zval *mysqli_warning_message(mysqli_object *obj, zval *retval)
205 {
206 	MYSQLI_WARNING *w;
207 
208 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
209 		return NULL;
210 	}
211 
212 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
213 	ZVAL_COPY(retval, &w->reason);
214 	return retval;
215 }
216 /* }}} */
217 
218 /* {{{ property mysqli_warning_sqlstate */
219 static
mysqli_warning_sqlstate(mysqli_object * obj,zval * retval)220 zval *mysqli_warning_sqlstate(mysqli_object *obj, zval *retval)
221 {
222 	MYSQLI_WARNING *w;
223 
224 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
225 		return NULL;
226 	}
227 
228 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
229 	ZVAL_COPY(retval, &w->sqlstate);
230 	return retval;
231 }
232 /* }}} */
233 
234 /* {{{ property mysqli_warning_error */
235 static
mysqli_warning_errno(mysqli_object * obj,zval * retval)236 zval *mysqli_warning_errno(mysqli_object *obj, zval *retval)
237 {
238 	MYSQLI_WARNING *w;
239 
240 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
241 		return NULL;
242 	}
243 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
244 	ZVAL_LONG(retval, w->errorno);
245 	return retval;
246 }
247 /* }}} */
248 
249 /* {{{ mysqli_warning_construct(object obj) */
PHP_METHOD(mysqli_warning,__construct)250 PHP_METHOD(mysqli_warning, __construct)
251 {
252 	zval			*z;
253 	mysqli_object	*obj;
254 #ifndef MYSQLI_USE_MYSQLND
255 	MYSQL			*hdl;
256 #endif
257 	MYSQLI_WARNING  *w;
258 	MYSQLI_RESOURCE *mysqli_resource;
259 
260 	if (ZEND_NUM_ARGS() != 1) {
261 		WRONG_PARAM_COUNT;
262 	}
263 	if (zend_parse_parameters(1, "o", &z)==FAILURE) {
264 		return;
265 	}
266 	obj = Z_MYSQLI_P(z);
267 
268 	if (obj->zo.ce == mysqli_link_class_entry) {
269 		MY_MYSQL *mysql;
270 		MYSQLI_FETCH_RESOURCE_CONN(mysql, z, MYSQLI_STATUS_VALID);
271 		if (mysql_warning_count(mysql->mysql)) {
272 #ifndef MYSQLI_USE_MYSQLND
273 			w = php_get_warnings(mysql->mysql);
274 #else
275 			w = php_get_warnings(mysql->mysql->data);
276 #endif
277 		} else {
278 			php_error_docref(NULL, E_WARNING, "No warnings found");
279 			RETURN_FALSE;
280 		}
281 	} else if (obj->zo.ce == mysqli_stmt_class_entry) {
282 		MY_STMT *stmt;
283 		MYSQLI_FETCH_RESOURCE_STMT(stmt, z, MYSQLI_STATUS_VALID);
284 #ifndef MYSQLI_USE_MYSQLND
285 		hdl = mysqli_stmt_get_connection(stmt->stmt);
286 		if (mysql_warning_count(hdl)) {
287 			w = php_get_warnings(hdl);
288 #else
289 		if (mysqlnd_stmt_warning_count(stmt->stmt)) {
290 			w = php_get_warnings(mysqli_stmt_get_connection(stmt->stmt));
291 #endif
292 		} else {
293 			php_error_docref(NULL, E_WARNING, "No warnings found");
294 			RETURN_FALSE;
295 		}
296 	} else {
297 		php_error_docref(NULL, E_WARNING, "invalid class argument");
298 		RETURN_FALSE;
299 	}
300 
301 	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
302 	mysqli_resource->ptr = mysqli_resource->info = (void *)w;
303 	mysqli_resource->status = MYSQLI_STATUS_VALID;
304 
305 	if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_warning_class_entry)) {
306 		MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
307 	} else {
308 		(Z_MYSQLI_P(getThis()))->ptr = mysqli_resource;
309 	}
310 
311 }
312 /* }}} */
313 
314 /* {{{ mysqli_warning_methods */
315 const zend_function_entry mysqli_warning_methods[] = {
316 	PHP_ME(mysqli_warning, __construct,		NULL, ZEND_ACC_PROTECTED)
317 	PHP_ME(mysqli_warning, next, 			NULL, ZEND_ACC_PUBLIC)
318 	PHP_FE_END
319 };
320 /* }}} */
321 
322 /* {{{ mysqli_warning_property_entries */
323 const mysqli_property_entry mysqli_warning_property_entries[] = {
324 	{"message", sizeof("message") - 1, mysqli_warning_message, NULL},
325 	{"sqlstate", sizeof("sqlstate") - 1, mysqli_warning_sqlstate, NULL},
326 	{"errno", sizeof("errno") - 1, mysqli_warning_errno, NULL},
327 	{NULL, 0, NULL, NULL}
328 };
329 /* }}} */
330 
331 /*
332  * Local variables:
333  * tab-width: 4
334  * c-basic-offset: 4
335  * indent-tabs-mode: t
336  * End:
337  * vim600: noet sw=4 ts=4 fdm=marker
338  * vim<600: noet sw=4 ts=4
339  */
340