diff options
author | Aliaksey Kandratsenka <alk@tut.by> | 2013-11-16 12:03:35 -0800 |
---|---|---|
committer | Aliaksey Kandratsenka <alk@tut.by> | 2013-11-16 12:03:35 -0800 |
commit | e0102230ec7e8304155798bf7f03d6abcd5991ee (patch) | |
tree | 238b9012e2ffc3e557befa546c9951f127a4e546 | |
parent | 2bf83af65664a2badbaebdb722ad498e8b38548c (diff) | |
download | gperftools-e0102230ec7e8304155798bf7f03d6abcd5991ee.tar.gz |
issue-588: Fix profiler_unittest.cc fork()
As suggested by Hannes Weisbach.
Call heap-profiler_unittest with the arguments 1 -2 (one iteration, 2
fork()ed children).
Instead of running the test, the program crashes with a std::bad_alloc
exception. This is caused by unconditionally passing the
number-of-threads-argument (0 or positive for threads, negative for
fork()s) in RunManyThreads(), thus allocating an array of pthread_t of
size -2. Depending on the sign of the thread number argument either
RunManyThreads or fork() should be called.
-rw-r--r-- | src/tests/profiler_unittest.cc | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/tests/profiler_unittest.cc b/src/tests/profiler_unittest.cc index 542cf0b..321f848 100644 --- a/src/tests/profiler_unittest.cc +++ b/src/tests/profiler_unittest.cc @@ -111,27 +111,31 @@ int main(int argc, char** argv) { ProfilerFlush(); // just because we can // The other threads, if any, will run only half as long as the main thread - RunManyThreads(test_other_thread, num_threads); - + if(num_threads > 0) { + RunManyThreads(test_other_thread, num_threads); + } else { // Or maybe they asked to fork. The fork test is only interesting // when we use CPUPROFILE to name, so check for that #ifdef HAVE_UNISTD_H - for (; num_threads < 0; ++num_threads) { // -<num_threads> to fork - if (filename) { - printf("FORK test only makes sense when no filename is specified.\n"); - return 2; - } - switch (fork()) { - case -1: - printf("FORK failed!\n"); - return 1; - case 0: // child - return execl(argv[0], argv[0], argv[1], NULL); - default: - wait(NULL); // we'll let the kids run one at a time + for (; num_threads < 0; ++num_threads) { // -<num_threads> to fork + if (filename) { + printf("FORK test only makes sense when no filename is specified.\n"); + return 2; + } + switch (fork()) { + case -1: + printf("FORK failed!\n"); + return 1; + case 0: // child + return execl(argv[0], argv[0], argv[1], NULL); + default: + wait(NULL); // we'll let the kids run one at a time + } } - } +#else + fprintf(stderr, "%s was compiled without support for fork() and exec()\n", argv[0]); #endif + } test_main_thread(); |