mysqli_warning.c 6.5 KB

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