xref: /PHP-7.3/ext/mysqli/mysqli_warning.c (revision 273e194d)
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_ptr_dtor_str(&(w->reason));
46 		zval_ptr_dtor_str(&(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 		errno = zval_get_long(entry);
148 		zend_hash_move_forward(Z_ARRVAL(row));
149 
150 		/* 2. Here comes the reason */
151 		entry = zend_hash_get_current_data(Z_ARRVAL(row));
152 
153 		w = php_new_warning(entry, errno);
154 		/*
155 		  Don't destroy entry, because the row destroy will decrease
156 		  the refcounter. Decreased twice then mysqlnd_free_result()
157 		  will crash, because it will try to access already freed memory.
158 		*/
159 		if (!first) {
160 			first = w;
161 		}
162 		if (prev) {
163 			prev->next = (void *)w;
164 		}
165 		prev = w;
166 
167 		zval_ptr_dtor(&row);
168 	}
169 
170 	mysql_free_result(result);
171 	return first;
172 }
173 /* }}} */
174 #endif
175 
176 /* {{{ bool mysqli_warning::next() */
PHP_METHOD(mysqli_warning,next)177 PHP_METHOD(mysqli_warning, next)
178 {
179 	MYSQLI_WARNING 	*w;
180 	zval  			*mysqli_warning;
181 	mysqli_object *obj = Z_MYSQLI_P(getThis());
182 
183 	if (obj->ptr) {
184 		if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
185 										 &mysqli_warning, mysqli_warning_class_entry) == FAILURE) {
186 			return;
187 		}
188 
189 		MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, mysqli_warning, "mysqli_warning", MYSQLI_STATUS_VALID);
190 
191 		if (w && w->next) {
192 			w = w->next;
193 	        ((MYSQLI_RESOURCE *)(obj->ptr))->ptr = w;
194 			RETURN_TRUE;
195 		}
196 	}
197 	RETURN_FALSE;
198 }
199 /* }}} */
200 
201 /* {{{ property mysqli_warning_message */
202 static
mysqli_warning_message(mysqli_object * obj,zval * retval)203 zval *mysqli_warning_message(mysqli_object *obj, zval *retval)
204 {
205 	MYSQLI_WARNING *w;
206 
207 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
208 		return NULL;
209 	}
210 
211 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
212 	ZVAL_COPY(retval, &w->reason);
213 	return retval;
214 }
215 /* }}} */
216 
217 /* {{{ property mysqli_warning_sqlstate */
218 static
mysqli_warning_sqlstate(mysqli_object * obj,zval * retval)219 zval *mysqli_warning_sqlstate(mysqli_object *obj, zval *retval)
220 {
221 	MYSQLI_WARNING *w;
222 
223 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
224 		return NULL;
225 	}
226 
227 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
228 	ZVAL_COPY(retval, &w->sqlstate);
229 	return retval;
230 }
231 /* }}} */
232 
233 /* {{{ property mysqli_warning_error */
234 static
mysqli_warning_errno(mysqli_object * obj,zval * retval)235 zval *mysqli_warning_errno(mysqli_object *obj, zval *retval)
236 {
237 	MYSQLI_WARNING *w;
238 
239 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
240 		return NULL;
241 	}
242 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
243 	ZVAL_LONG(retval, w->errorno);
244 	return retval;
245 }
246 /* }}} */
247 
248 /* {{{ mysqli_warning_construct(object obj) */
PHP_METHOD(mysqli_warning,__construct)249 PHP_METHOD(mysqli_warning, __construct)
250 {
251 	zval			*z;
252 	mysqli_object	*obj;
253 #ifndef MYSQLI_USE_MYSQLND
254 	MYSQL			*hdl;
255 #endif
256 	MYSQLI_WARNING  *w;
257 	MYSQLI_RESOURCE *mysqli_resource;
258 
259 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &z) == FAILURE) {
260 		return;
261 	}
262 	obj = Z_MYSQLI_P(z);
263 
264 	if (obj->zo.ce == mysqli_link_class_entry) {
265 		MY_MYSQL *mysql;
266 		MYSQLI_FETCH_RESOURCE_CONN(mysql, z, MYSQLI_STATUS_VALID);
267 		if (mysql_warning_count(mysql->mysql)) {
268 #ifndef MYSQLI_USE_MYSQLND
269 			w = php_get_warnings(mysql->mysql);
270 #else
271 			w = php_get_warnings(mysql->mysql->data);
272 #endif
273 		} else {
274 			php_error_docref(NULL, E_WARNING, "No warnings found");
275 			RETURN_FALSE;
276 		}
277 	} else if (obj->zo.ce == mysqli_stmt_class_entry) {
278 		MY_STMT *stmt;
279 		MYSQLI_FETCH_RESOURCE_STMT(stmt, z, MYSQLI_STATUS_VALID);
280 #ifndef MYSQLI_USE_MYSQLND
281 		hdl = mysqli_stmt_get_connection(stmt->stmt);
282 		if (mysql_warning_count(hdl)) {
283 			w = php_get_warnings(hdl);
284 #else
285 		if (mysqlnd_stmt_warning_count(stmt->stmt)) {
286 			w = php_get_warnings(mysqli_stmt_get_connection(stmt->stmt));
287 #endif
288 		} else {
289 			php_error_docref(NULL, E_WARNING, "No warnings found");
290 			RETURN_FALSE;
291 		}
292 	} else {
293 		php_error_docref(NULL, E_WARNING, "invalid class argument");
294 		RETURN_FALSE;
295 	}
296 
297 	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
298 	mysqli_resource->ptr = mysqli_resource->info = (void *)w;
299 	mysqli_resource->status = MYSQLI_STATUS_VALID;
300 
301 	if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_warning_class_entry)) {
302 		MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
303 	} else {
304 		(Z_MYSQLI_P(getThis()))->ptr = mysqli_resource;
305 	}
306 
307 }
308 /* }}} */
309 
310 /* {{{ mysqli_warning_methods */
311 const zend_function_entry mysqli_warning_methods[] = {
312 	PHP_ME(mysqli_warning, __construct,		NULL, ZEND_ACC_PROTECTED)
313 	PHP_ME(mysqli_warning, next, 			NULL, ZEND_ACC_PUBLIC)
314 	PHP_FE_END
315 };
316 /* }}} */
317 
318 /* {{{ mysqli_warning_property_entries */
319 const mysqli_property_entry mysqli_warning_property_entries[] = {
320 	{"message", sizeof("message") - 1, mysqli_warning_message, NULL},
321 	{"sqlstate", sizeof("sqlstate") - 1, mysqli_warning_sqlstate, NULL},
322 	{"errno", sizeof("errno") - 1, mysqli_warning_errno, NULL},
323 	{NULL, 0, NULL, NULL}
324 };
325 /* }}} */
326 
327 /*
328  * Local variables:
329  * tab-width: 4
330  * c-basic-offset: 4
331  * indent-tabs-mode: t
332  * End:
333  * vim600: noet sw=4 ts=4 fdm=marker
334  * vim<600: noet sw=4 ts=4
335  */
336