xref: /PHP-7.4/ext/mysqli/mysqli_warning.c (revision d39edebb)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 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 */
mysqli_warning_message(mysqli_object * obj,zval * retval,zend_bool quiet)202 static int mysqli_warning_message(mysqli_object *obj, zval *retval, zend_bool quiet)
203 {
204 	MYSQLI_WARNING *w;
205 
206 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
207 		if (!quiet) {
208 			php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
209 		}
210 		ZVAL_NULL(retval);
211 
212 		return FAILURE;
213 	}
214 
215 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
216 	ZVAL_COPY(retval, &w->reason);
217 
218 	return SUCCESS;
219 }
220 /* }}} */
221 
222 /* {{{ property mysqli_warning_sqlstate */
mysqli_warning_sqlstate(mysqli_object * obj,zval * retval,zend_bool quiet)223 static int mysqli_warning_sqlstate(mysqli_object *obj, zval *retval, zend_bool quiet)
224 {
225 	MYSQLI_WARNING *w;
226 
227 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
228 		if (!quiet) {
229 			php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
230 		}
231 		ZVAL_NULL(retval);
232 
233 		return FAILURE;
234 	}
235 
236 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
237 	ZVAL_COPY(retval, &w->sqlstate);
238 
239 	return SUCCESS;
240 }
241 /* }}} */
242 
243 /* {{{ property mysqli_warning_error */
mysqli_warning_errno(mysqli_object * obj,zval * retval,zend_bool quiet)244 static int mysqli_warning_errno(mysqli_object *obj, zval *retval, zend_bool quiet)
245 {
246 	MYSQLI_WARNING *w;
247 
248 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
249 		if (!quiet) {
250 			php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(obj->zo.ce->name));
251 		}
252 		ZVAL_NULL(retval);
253 
254 		return FAILURE;
255 	}
256 
257 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
258 	ZVAL_LONG(retval, w->errorno);
259 
260 	return SUCCESS;
261 }
262 /* }}} */
263 
264 /* {{{ mysqli_warning_construct(object obj) */
PHP_METHOD(mysqli_warning,__construct)265 PHP_METHOD(mysqli_warning, __construct)
266 {
267 	zval			*z;
268 	mysqli_object	*obj;
269 #ifndef MYSQLI_USE_MYSQLND
270 	MYSQL			*hdl;
271 #endif
272 	MYSQLI_WARNING  *w;
273 	MYSQLI_RESOURCE *mysqli_resource;
274 
275 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &z) == FAILURE) {
276 		return;
277 	}
278 	obj = Z_MYSQLI_P(z);
279 
280 	if (obj->zo.ce == mysqli_link_class_entry) {
281 		MY_MYSQL *mysql;
282 		MYSQLI_FETCH_RESOURCE_CONN(mysql, z, MYSQLI_STATUS_VALID);
283 		if (mysql_warning_count(mysql->mysql)) {
284 #ifndef MYSQLI_USE_MYSQLND
285 			w = php_get_warnings(mysql->mysql);
286 #else
287 			w = php_get_warnings(mysql->mysql->data);
288 #endif
289 		} else {
290 			php_error_docref(NULL, E_WARNING, "No warnings found");
291 			RETURN_FALSE;
292 		}
293 	} else if (obj->zo.ce == mysqli_stmt_class_entry) {
294 		MY_STMT *stmt;
295 		MYSQLI_FETCH_RESOURCE_STMT(stmt, z, MYSQLI_STATUS_VALID);
296 #ifndef MYSQLI_USE_MYSQLND
297 		hdl = mysqli_stmt_get_connection(stmt->stmt);
298 		if (mysql_warning_count(hdl)) {
299 			w = php_get_warnings(hdl);
300 #else
301 		if (mysqlnd_stmt_warning_count(stmt->stmt)) {
302 			w = php_get_warnings(mysqli_stmt_get_connection(stmt->stmt));
303 #endif
304 		} else {
305 			php_error_docref(NULL, E_WARNING, "No warnings found");
306 			RETURN_FALSE;
307 		}
308 	} else {
309 		php_error_docref(NULL, E_WARNING, "invalid class argument");
310 		RETURN_FALSE;
311 	}
312 
313 	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
314 	mysqli_resource->ptr = mysqli_resource->info = (void *)w;
315 	mysqli_resource->status = MYSQLI_STATUS_VALID;
316 
317 	if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_warning_class_entry)) {
318 		MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
319 	} else {
320 		(Z_MYSQLI_P(getThis()))->ptr = mysqli_resource;
321 	}
322 
323 }
324 /* }}} */
325 
326 /* {{{ mysqli_warning_methods */
327 const zend_function_entry mysqli_warning_methods[] = {
328 	PHP_ME(mysqli_warning, __construct,		NULL, ZEND_ACC_PROTECTED)
329 	PHP_ME(mysqli_warning, next, 			NULL, ZEND_ACC_PUBLIC)
330 	PHP_FE_END
331 };
332 /* }}} */
333 
334 /* {{{ mysqli_warning_property_entries */
335 const mysqli_property_entry mysqli_warning_property_entries[] = {
336 	{"message", sizeof("message") - 1, mysqli_warning_message, NULL},
337 	{"sqlstate", sizeof("sqlstate") - 1, mysqli_warning_sqlstate, NULL},
338 	{"errno", sizeof("errno") - 1, mysqli_warning_errno, NULL},
339 	{NULL, 0, NULL, NULL}
340 };
341 /* }}} */
342