xref: /php-src/ext/mysqli/mysqli_warning.c (revision c2bdb31b)
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_mysqli_structs.h"
25 #include "mysqli_priv.h"
26 
27 /* {{{ void php_clear_warnings() */
php_clear_warnings(MYSQLI_WARNING * w)28 void php_clear_warnings(MYSQLI_WARNING *w)
29 {
30 	MYSQLI_WARNING *n;
31 
32 	while (w) {
33 		n = w;
34 		zval_ptr_dtor_str(&(w->reason));
35 		zval_ptr_dtor_str(&(w->sqlstate));
36 		w = w->next;
37 		efree(n);
38 	}
39 }
40 /* }}} */
41 
42 /* {{{ MYSQLI_WARNING *php_new_warning */
43 static
php_new_warning(zval * reason,int errorno)44 MYSQLI_WARNING *php_new_warning(zval * reason, int errorno)
45 {
46 	MYSQLI_WARNING *w;
47 
48 	w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
49 
50 	ZVAL_COPY(&w->reason, reason);
51 	convert_to_string(&w->reason);
52 
53 	ZVAL_STRINGL(&(w->sqlstate), "HY000", sizeof("HY000") - 1);
54 
55 	w->errorno = errorno;
56 
57 	return w;
58 }
59 /* }}} */
60 
61 /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) */
php_get_warnings(MYSQLND_CONN_DATA * mysql)62 MYSQLI_WARNING * php_get_warnings(MYSQLND_CONN_DATA * mysql)
63 {
64 	MYSQLI_WARNING	*w, *first = NULL, *prev = NULL;
65 	MYSQL_RES		*result;
66 	zval			row;
67 
68 	if (mysql->m->query(mysql, "SHOW WARNINGS", 13)) {
69 		return NULL;
70 	}
71 
72 	result = mysql->m->use_result(mysql);
73 
74 	for (;;) {
75 		zval *entry;
76 		int mysqli_errno;
77 
78 		mysqlnd_fetch_into(result, MYSQLND_FETCH_NUM, &row);
79 		if (Z_TYPE(row) != IS_ARRAY) {
80 			zval_ptr_dtor(&row);
81 			break;
82 		}
83 		zend_hash_internal_pointer_reset(Z_ARRVAL(row));
84 		/* 0. we don't care about the first */
85 		zend_hash_move_forward(Z_ARRVAL(row));
86 
87 		/* 1. Here comes the error no */
88 		entry = zend_hash_get_current_data(Z_ARRVAL(row));
89 		mysqli_errno = zval_get_long(entry);
90 		zend_hash_move_forward(Z_ARRVAL(row));
91 
92 		/* 2. Here comes the reason */
93 		entry = zend_hash_get_current_data(Z_ARRVAL(row));
94 
95 		w = php_new_warning(entry, mysqli_errno);
96 		/*
97 		  Don't destroy entry, because the row destroy will decrease
98 		  the refcounter. Decreased twice then mysqlnd_free_result()
99 		  will crash, because it will try to access already freed memory.
100 		*/
101 		if (!first) {
102 			first = w;
103 		}
104 		if (prev) {
105 			prev->next = (void *)w;
106 		}
107 		prev = w;
108 
109 		zval_ptr_dtor(&row);
110 	}
111 
112 	mysql_free_result(result);
113 	return first;
114 }
115 /* }}} */
116 
117 /* {{{ bool mysqli_warning::next() */
PHP_METHOD(mysqli_warning,next)118 PHP_METHOD(mysqli_warning, next)
119 {
120 	MYSQLI_WARNING 	*w;
121 	mysqli_object *obj = Z_MYSQLI_P(ZEND_THIS);
122 
123 	if (zend_parse_parameters_none() == FAILURE) {
124 		RETURN_THROWS();
125 	}
126 
127 	if (obj->ptr) {
128 		MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, ZEND_THIS, "mysqli_warning", MYSQLI_STATUS_VALID);
129 
130 		if (w && w->next) {
131 			w = w->next;
132 	        ((MYSQLI_RESOURCE *)(obj->ptr))->ptr = w;
133 			RETURN_TRUE;
134 		}
135 	}
136 	RETURN_FALSE;
137 }
138 /* }}} */
139 
140 /* {{{ property mysqli_warning_message */
mysqli_warning_message(mysqli_object * obj,zval * retval,bool quiet)141 static zend_result mysqli_warning_message(mysqli_object *obj, zval *retval, bool quiet)
142 {
143 	MYSQLI_WARNING *w;
144 
145 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
146 		if (!quiet) {
147 			zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
148 		}
149 
150 		return FAILURE;
151 	}
152 
153 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
154 	ZVAL_COPY(retval, &w->reason);
155 
156 	return SUCCESS;
157 }
158 /* }}} */
159 
160 /* {{{ property mysqli_warning_sqlstate */
mysqli_warning_sqlstate(mysqli_object * obj,zval * retval,bool quiet)161 static zend_result mysqli_warning_sqlstate(mysqli_object *obj, zval *retval, bool quiet)
162 {
163 	MYSQLI_WARNING *w;
164 
165 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
166 		if (!quiet) {
167 			zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
168 		}
169 
170 		return FAILURE;
171 	}
172 
173 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
174 	ZVAL_COPY(retval, &w->sqlstate);
175 
176 	return SUCCESS;
177 }
178 /* }}} */
179 
180 /* {{{ property mysqli_warning_error */
mysqli_warning_errno(mysqli_object * obj,zval * retval,bool quiet)181 static zend_result mysqli_warning_errno(mysqli_object *obj, zval *retval, bool quiet)
182 {
183 	MYSQLI_WARNING *w;
184 
185 	if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
186 		if (!quiet) {
187 			zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name));
188 		}
189 
190 		return FAILURE;
191 	}
192 
193 	w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
194 	ZVAL_LONG(retval, w->errorno);
195 
196 	return SUCCESS;
197 }
198 /* }}} */
199 
PHP_METHOD(mysqli_warning,__construct)200 PHP_METHOD(mysqli_warning, __construct)
201 {
202 	ZEND_PARSE_PARAMETERS_NONE();
203 
204 	zend_throw_error(NULL, "Cannot directly construct mysqli_warning");
205 }
206 
207 /* {{{ mysqli_warning_property_entries */
208 const mysqli_property_entry mysqli_warning_property_entries[] = {
209 	{"message", sizeof("message") - 1, mysqli_warning_message, NULL},
210 	{"sqlstate", sizeof("sqlstate") - 1, mysqli_warning_sqlstate, NULL},
211 	{"errno", sizeof("errno") - 1, mysqli_warning_errno, NULL},
212 	{NULL, 0, NULL, NULL}
213 };
214 /* }}} */
215