mysqli_insert_packet_overflow.phpt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. --TEST--
  2. INSERT and packet overflow
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifconnectfailure.inc');
  7. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  8. die(sprintf("SKIP [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
  9. $max_len = pow(2, 24);
  10. if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
  11. die(sprintf("SKIP [%d] %s\n", mysqli_errno($link), mysqli_error($link)));
  12. if (!mysqli_query($link, "SET NAMES 'latin1'"))
  13. die(sprintf("SKIP [%d] %s\n", mysqli_errno($link), mysqli_error($link)));
  14. mysqli_close($link);
  15. ?>
  16. --INI--
  17. memory_limit=256M
  18. --FILE--
  19. <?php
  20. require('connect.inc');
  21. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  22. printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  23. if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
  24. printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  25. if (!$row = mysqli_fetch_assoc($res))
  26. printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  27. mysqli_free_result($res);
  28. if (0 === ($org_max_allowed_packet = (int)$row['Value']))
  29. printf("[004] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
  30. $max_len = pow(2, 24);
  31. if ($org_max_allowed_packet < $max_len) {
  32. if (!mysqli_query($link, "SET GLOBAL max_allowed_packet = " . ($max_len + 100))) {
  33. if (1227 == mysqli_errno($link)) {
  34. /* [1227] Access denied; you need the SUPER privilege for this operation */
  35. print "done!";
  36. exit(0);
  37. } else {
  38. printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  39. }
  40. }
  41. }
  42. mysqli_close($link);
  43. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  44. printf("[006] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  45. if (!mysqli_query($link, "SET NAMES 'latin1'"))
  46. printf("[007] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  47. if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
  48. printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  49. if (!$row = mysqli_fetch_assoc($res))
  50. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  51. mysqli_free_result($res);
  52. if (0 === ($max_allowed_packet = (int)$row['Value']))
  53. printf("[010] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
  54. $max_len = pow(2, 24);
  55. if ($max_allowed_packet < $max_len) {
  56. printf("[011] Failed to change max_allowed_packet");
  57. }
  58. if (!mysqli_query($link, "CREATE TABLE test(col_blob LONGBLOB) ENGINE=" . $engine))
  59. printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  60. $query_prefix = "INSERT INTO test(col_blob) VALUES ('";
  61. $query_postfix = "')";
  62. $query_len = strlen($query_prefix) + strlen($query_postfix);
  63. $com_query_len = 2;
  64. $blob = str_repeat('a', $max_len - $com_query_len - $query_len);
  65. $query = sprintf("%s%s%s", $query_prefix, $blob, $query_postfix);
  66. if (!mysqli_query($link, $query))
  67. printf("[013] max_allowed_packet = %d, strlen(query) = %d, [%d] %s\n", $max_allowed_packet, strlen($query), mysqli_errno($link), mysqli_error($link));
  68. if (!$res = mysqli_query($link, "SELECT col_blob FROM test"))
  69. printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  70. if (!$row = mysqli_fetch_assoc($res)) {
  71. printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  72. } else {
  73. if ($row['col_blob'] != $blob) {
  74. printf("[016] Blob seems wrong, dumping data\n");
  75. var_dump(strlen($row['col_blob']));
  76. var_dump(strlen($blob));
  77. }
  78. mysqli_free_result($res);
  79. }
  80. if (!mysqli_query($link, "SET GLOBAL max_allowed_packet = " . $org_max_allowed_packet))
  81. if (1227 != mysqli_errno($link))
  82. printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  83. mysqli_close($link);
  84. print "done!";
  85. ?>
  86. --CLEAN--
  87. <?php
  88. require_once("clean_table.inc");
  89. ?>
  90. --EXPECTF--
  91. done!