MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
segfault-t.cc
1 /* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 // Always include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include "test_utils.h"
21 #include "my_stacktrace.h"
22 #include "m_string.h"
23 #include "hash_filo.h"
24 
25 namespace segfault_unittest {
26 
29 
30 class FatalSignalDeathTest : public ::testing::Test
31 {
32 protected:
33  virtual void SetUp()
34  {
35  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
36  initializer.SetUp();
37  }
38  virtual void TearDown() { initializer.TearDown(); }
39 
40  Server_initializer initializer;
41 };
42 
43 
44 TEST_F(FatalSignalDeathTest, Abort)
45 {
46 #if defined(__WIN__)
47  EXPECT_DEATH_IF_SUPPORTED(abort(), ".* UTC - mysqld got exception.*");
48 #else
49  EXPECT_DEATH_IF_SUPPORTED(abort(), ".* UTC - mysqld got signal 6.*");
50 #endif
51 }
52 
53 
54 TEST_F(FatalSignalDeathTest, Segfault)
55 {
56  int *pint= NULL;
57 #if defined(__WIN__)
58  /*
59  After upgrading from gtest 1.5 to 1.6 this segfault is no longer
60  caught by handle_fatal_signal(). We get an empty error message from the
61  gtest library instead.
62  */
63  EXPECT_DEATH_IF_SUPPORTED(*pint= 42, "");
64 #elif defined(__SANITIZE_ADDRESS__)
65  /* gcc 4.8.1 with '-fsanitize=address -O1' */
66  EXPECT_DEATH_IF_SUPPORTED(*pint= 42, ".*ASAN:SIGSEGV.*");
67 #else
68  /*
69  On most platforms we get SIGSEGV == 11, but SIGBUS == 10 is also possible.
70  And on Mac OsX we can get SIGILL == 4 (but only in optmized mode).
71  */
72  EXPECT_DEATH_IF_SUPPORTED(*pint= 42, ".* UTC - mysqld got signal .*");
73 #endif
74 }
75 
76 
77 // A simple helper function to determine array size.
78 template <class T, int size>
79 int array_size(const T (&)[size])
80 {
81  return size;
82 }
83 
84 
85 // Verifies that my_safe_utoa behaves like sprintf(_, "%llu", _)
86 TEST(PrintUtilities, Utoa)
87 {
88  char buff[22];
89  ulonglong intarr[]= { 0, 1, 8, 12, 1234, 88888, ULONG_MAX, ULONGLONG_MAX };
90  char sprintbuff[22];
91  for (int ix= 0; ix < array_size(intarr); ++ix)
92  {
93  char *my_res;
94  sprintf(sprintbuff, "%llu", intarr[ix]);
95  my_res= my_safe_utoa(10, intarr[ix], &buff[sizeof(buff)-1]);
96  EXPECT_STREQ(sprintbuff, my_res);
97 
98  if (intarr[ix] <= ULONG_MAX)
99  {
100  sprintf(sprintbuff, "%lu", (ulong) intarr[ix]);
101  my_res= my_safe_utoa(10, (ulong) intarr[ix], &buff[sizeof(buff)-1]);
102  EXPECT_STREQ(sprintbuff, my_res);
103  }
104  }
105 }
106 
107 
108 // Verifies that my_safe_itoa behaves like sprintf(_, "%lld", _)
109 TEST(PrintUtilities, Itoa)
110 {
111  char buff[22];
112  char sprintbuff[22];
113  longlong intarr[]= { 0, 1, 8, 12, 1234, 88888, LONG_MAX, LONGLONG_MAX };
114 
115  for (int ix= 0; ix < array_size(intarr); ++ix)
116  {
117  char *my_res;
118  sprintf(sprintbuff, "%lld", intarr[ix]);
119  my_res= my_safe_itoa(10, intarr[ix], &buff[sizeof(buff)-1]);
120  EXPECT_STREQ(sprintbuff, my_res);
121 
122  ll2str(intarr[ix], buff, 10, 0);
123  EXPECT_STREQ(sprintbuff, buff);
124 
125  sprintf(sprintbuff, "%lld", -intarr[ix]);
126  my_res= my_safe_itoa(10, -intarr[ix], &buff[sizeof(buff)-1]);
127  EXPECT_STREQ(sprintbuff, my_res);
128 
129  // This one fails ....
130  // ll2str(-intarr[ix], buff, 10, 0);
131  // EXPECT_STREQ(sprintbuff, buff)
132  // << "failed for " << -intarr[ix];
133 
134  sprintf(sprintbuff, "%llx", intarr[ix]);
135  my_res= my_safe_itoa(16, intarr[ix], &buff[sizeof(buff)-1]);
136  EXPECT_STREQ(sprintbuff, my_res);
137 
138  ll2str(intarr[ix], buff, 16, 0);
139  EXPECT_STREQ(sprintbuff, buff);
140 
141  sprintf(sprintbuff, "%llx", -intarr[ix]);
142  my_res= my_safe_itoa(16, -intarr[ix], &buff[sizeof(buff)-1]);
143  EXPECT_STREQ(sprintbuff, my_res)
144  << "failed for " << -intarr[ix];
145 
146  ll2str(-intarr[ix], buff, 16, 0);
147  EXPECT_STREQ(sprintbuff, buff);
148  }
149 }
150 
151 
152 // Various tests for my_safe_snprintf.
153 TEST(PrintUtilities, Printf)
154 {
155  char buff[512];
156  char sprintfbuff[512];
157  const char *null_str= NULL;
158 
159  my_safe_snprintf(buff, sizeof(buff), "hello");
160  EXPECT_STREQ("hello", buff);
161 
162  my_safe_snprintf(buff, sizeof(buff), "hello %s hello", "hello");
163  EXPECT_STREQ("hello hello hello", buff);
164  my_safe_snprintf(buff, sizeof(buff), "hello %s hello", null_str);
165  EXPECT_STREQ("hello (null) hello", buff);
166 
167  my_safe_snprintf(buff, sizeof(buff), "hello %d hello", 42);
168  EXPECT_STREQ("hello 42 hello", buff);
169  my_safe_snprintf(buff, sizeof(buff), "hello %i hello", 42);
170  EXPECT_STREQ("hello 42 hello", buff);
171  my_safe_snprintf(buff, sizeof(buff), "hello %u hello", (unsigned) 42);
172  EXPECT_STREQ("hello 42 hello", buff);
173 
174  my_safe_snprintf(buff, sizeof(buff), "hello %llu hello", ULONGLONG_MAX);
175  sprintf(sprintfbuff, "hello %llu hello", ULONGLONG_MAX);
176  EXPECT_STREQ(sprintfbuff, buff);
177 
178  my_safe_snprintf(buff, sizeof(buff), "hello %x hello", 42);
179  EXPECT_STREQ("hello 2a hello", buff);
180 
181  my_safe_snprintf(buff, sizeof(buff), "hello %x hello", -42);
182  sprintf(sprintfbuff, "hello %x hello", -42);
183  EXPECT_STREQ("hello ffffffd6 hello", buff);
184  EXPECT_STREQ(sprintfbuff, buff);
185 
186  my_safe_snprintf(buff, sizeof(buff), "hello %llx hello", (longlong) -42);
187  sprintf(sprintfbuff, "hello %llx hello", (longlong) -42);
188  EXPECT_STREQ("hello ffffffffffffffd6 hello", buff);
189  EXPECT_STREQ(sprintfbuff, buff);
190 
191  void *p= this;
192  my_safe_snprintf(buff, sizeof(buff), "hello 0x%p hello", p);
193  my_snprintf(sprintfbuff, sizeof(sprintfbuff), "hello %p hello", p);
194  EXPECT_STREQ(sprintfbuff, buff);
195 }
196 
197 
198 // After the fix for Bug#14689561, this is no longer a death test.
199 TEST(HashFiloTest, TestHashFiloZeroSize)
200 {
201  hash_filo *t_cache;
202  t_cache= new hash_filo(5, 0, 0,
203  (my_hash_get_key) NULL,
204  (my_hash_free_key) NULL,
205  NULL);
206  t_cache->clear();
207  t_cache->resize(0);
209  // After resize (to zero) it tries to dereference last_link which is NULL.
210  t_cache->add(&entry);
211  delete t_cache;
212 }
213 
214 }