xref: /PHP-8.0/ext/mysqli/mysqli_warning.c (revision 7e6b8407)
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   | http://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 #ifndef MYSQLI_USE_MYSQLND
52 /* {{{ MYSQLI_WARNING *php_new_warning */
53 static
php_new_warning(const char * reason,int errorno)54 MYSQLI_WARNING *php_new_warning(const char *reason, int errorno)
55 {
56 	MYSQLI_WARNING *w;
57 
58 	w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
59 
60 	ZVAL_UTF8_STRING(&(w->reason), reason, ZSTR_DUPLICATE);
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(MYSQL * mysql)71 MYSQLI_WARNING *php_get_warnings(MYSQL *mysql)
72 {
73 	MYSQLI_WARNING *w, *first = NULL, *prev = NULL;
74 	MYSQL_RES		*result;
75 	MYSQL_ROW 		row;
76 
77 	if (mysql_real_query(mysql, "SHOW WARNINGS", 13)) {
78 		return NULL;
79 	}
80 
81 	result = mysql_store_result(mysql);
82 
83 	while ((row = mysql_fetch_row(result))) {
84 		w = php_new_warning(row[2], atoi(row[1]));
85 		if (!first) {
86 			first = w;
87 		}
88 		if (prev) {
89 			prev->next = w;
90 		}
91 		prev = w;
92 	}
93 	mysql_free_result(result);
94 	return first;
95 }
96 /* }}} */
97 #else
98 /* {{{ MYSQLI_WARNING *php_new_warning */
99 static
php_new_warning(zval * reason,int errorno)100 MYSQLI_WARNING *php_new_warning(zval * reason, int errorno)
101 {
102 	MYSQLI_WARNING *w;
103 
104 	w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
105 
106 	ZVAL_COPY(&w->reason, reason);
107 	convert_to_string(&w->reason);
108 
109 	ZVAL_UTF8_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1,  ZSTR_DUPLICATE);
110 
111 	w->errorno = errorno;
112 
113 	return w;
114 }
115 /* }}} */
116 
117 /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) */
php_get_warnings(MYSQLND_CONN_DATA * mysql)118 MYSQLI_WARNING * php_get_warnings(MYSQLND_CONN_DATA * mysql)
119 {
120 	MYSQLI_WARNING	*w, *first = NULL, *prev = NULL;
121 	MYSQL_RES		*result;
122 	zval			row;
123 
124 	if (mysql->m->query(mysql, "SHOW WARNINGS", 13)) {
125 		return NULL;
126 	}
127 
128 	result = mysql->m->use_result(mysql, 0);
129 
130 	for (;;) {
131 		zval *entry;
132 		int errno;
133 
134 		mysqlnd_fetch_into(result, MYSQLND_FETCH_NUM, &row, MYSQLND_MYSQLI);
135 		if (Z_TYPE(row) != IS_ARRAY) {
136 			zval_ptr_dtor(&row);
137 			break;
138 		}
139 		zend_hash_internal_pointer_reset(Z_ARRVAL(row));
140 		/* 0. we don't care about the first */
141 		zend_hash_move_forward(Z_ARRVAL(row));
142 
143 		/* 1. Here comes the error no */
144 		entry = zend_hash_get_current_data(Z_ARRVAL(row));
145 		errno = zval_get_long(entry);
146 		zend_hash_move_forward(Z_ARRVAL(row));
147 
148 		/* 2. Here comes the reason */
149 		entry = zend_hash_get_current_data(Z_ARRVAL(row));
150 
151 		w = php_new_warning(entry, errno);
152 		/*
153 		  Don't destroy entry, because the row destroy will decrease
154 		  the refcounter. Decreased twice then mysqlnd_free_result()
155 		  will crash, because it will try to access already freed memory.
156 		*/
157 		if (!first) {
158 			first = w;
159 		}
160 		if (prev) {
161 			prev->next = (void *)w;
162 		}
163 		prev = w;
164 
165 		zval_ptr_dtor(&row);
166 	}
167 
168 	mysql_free_result(result);
169 	return first;
170 }
171 /* }}} */
172 #endif
173 
174 /* {{{ bool mysqli_warning::next() */
PHP_METHOD(mysqli_warning,next)175 PHP_METHOD(mysqli_warning, next)
176 {
177 	MYSQLI_WARNING 	*w;
178 	mysqli_object *obj = Z_MYSQLI_P(ZEND_THIS);
179 
180 	if (zend_parse_parameters_none() == FAILURE) {
181 		RETURN_THROWS();
182 	}
183 
184 	if (obj->ptr) {
185 		MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, ZEND_THIS, "mysqli_warning", MYSQLI_STATUS_VALID);
186 
187 		if (w && w->next) {
188 			w = w->next;
189 	        ((MYSQLI_RESOURCE *)(obj->ptr))->ptr = w;
190 			RETURN_TRUE;
191 		}
192 	}
193 	RETURN_FALSE;
194 }
195 /* }}} */
196 
197 /* {{{ property mysqli_warning_message */
mysqli_warning_message(mysqli_object * obj,zval * retval,zend_bool quiet)198 static int mysqli_warning_message(mysqli_object *obj, zval *retval, zend_bool quiet)
199 {
200 	MYSQLI_WARNING *w;
201 
202 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
203 		if (!quiet) {
204 			zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
205 		}
206 
207 		return FAILURE;
208 	}
209 
210 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
211 	ZVAL_COPY(retval, &w->reason);
212 
213 	return SUCCESS;
214 }
215 /* }}} */
216 
217 /* {{{ property mysqli_warning_sqlstate */
mysqli_warning_sqlstate(mysqli_object * obj,zval * retval,zend_bool quiet)218 static int mysqli_warning_sqlstate(mysqli_object *obj, zval *retval, zend_bool quiet)
219 {
220 	MYSQLI_WARNING *w;
221 
222 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
223 		if (!quiet) {
224 			zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
225 		}
226 
227 		return FAILURE;
228 	}
229 
230 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
231 	ZVAL_COPY(retval, &w->sqlstate);
232 
233 	return SUCCESS;
234 }
235 /* }}} */
236 
237 /* {{{ property mysqli_warning_error */
mysqli_warning_errno(mysqli_object * obj,zval * retval,zend_bool quiet)238 static int mysqli_warning_errno(mysqli_object *obj, zval *retval, zend_bool quiet)
239 {
240 	MYSQLI_WARNING *w;
241 
242 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
243 		if (!quiet) {
244 			zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
245 		}
246 
247 		return FAILURE;
248 	}
249 
250 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
251 	ZVAL_LONG(retval, w->errorno);
252 
253 	return SUCCESS;
254 }
255 /* }}} */
256 
PHP_METHOD(mysqli_warning,__construct)257 PHP_METHOD(mysqli_warning, __construct)
258 {
259 	ZEND_PARSE_PARAMETERS_NONE();
260 
261 	zend_throw_error(NULL, "Cannot directly construct mysqli_warning");
262 }
263 
264 /* {{{ mysqli_warning_property_entries */
265 const mysqli_property_entry mysqli_warning_property_entries[] = {
266 	{"message", sizeof("message") - 1, mysqli_warning_message, NULL},
267 	{"sqlstate", sizeof("sqlstate") - 1, mysqli_warning_sqlstate, NULL},
268 	{"errno", sizeof("errno") - 1, mysqli_warning_errno, NULL},
269 	{NULL, 0, NULL, NULL}
270 };
271 /* }}} */
272