|
| 1 | +diff --git a/code/Common/Assimp.cpp b/code/Common/Assimp.cpp |
| 2 | +index ef3ee7b..22e16bd 100644 |
| 3 | +--- a/code/Common/Assimp.cpp |
| 4 | ++++ b/code/Common/Assimp.cpp |
| 5 | +@@ -359,20 +359,25 @@ void CallbackToLogRedirector(const char *msg, char *dt) { |
| 6 | + s->write(msg); |
| 7 | + } |
| 8 | + |
| 9 | ++static LogStream *DefaultStream = nullptr; |
| 10 | ++ |
| 11 | + // ------------------------------------------------------------------------------------------------ |
| 12 | + ASSIMP_API aiLogStream aiGetPredefinedLogStream(aiDefaultLogStream pStream, const char *file) { |
| 13 | + aiLogStream sout; |
| 14 | + |
| 15 | + ASSIMP_BEGIN_EXCEPTION_REGION(); |
| 16 | +- LogStream *stream = LogStream::createDefaultStream(pStream, file); |
| 17 | +- if (!stream) { |
| 18 | ++ if (DefaultStream == nullptr) { |
| 19 | ++ DefaultStream = LogStream::createDefaultStream(pStream, file); |
| 20 | ++ } |
| 21 | ++ |
| 22 | ++ if (!DefaultStream) { |
| 23 | + sout.callback = nullptr; |
| 24 | + sout.user = nullptr; |
| 25 | + } else { |
| 26 | + sout.callback = &CallbackToLogRedirector; |
| 27 | +- sout.user = (char *)stream; |
| 28 | ++ sout.user = (char *)DefaultStream; |
| 29 | + } |
| 30 | +- gPredefinedStreams.push_back(stream); |
| 31 | ++ gPredefinedStreams.push_back(DefaultStream); |
| 32 | + ASSIMP_END_EXCEPTION_REGION(aiLogStream); |
| 33 | + return sout; |
| 34 | + } |
| 35 | +@@ -411,6 +416,10 @@ ASSIMP_API aiReturn aiDetachLogStream(const aiLogStream *stream) { |
| 36 | + DefaultLogger::get()->detachStream(it->second); |
| 37 | + delete it->second; |
| 38 | + |
| 39 | ++ if ((Assimp::LogStream *)stream->user == DefaultStream) { |
| 40 | ++ DefaultStream = nullptr; |
| 41 | ++ } |
| 42 | ++ |
| 43 | + gActiveLogStreams.erase(it); |
| 44 | + |
| 45 | + if (gActiveLogStreams.empty()) { |
| 46 | +diff --git a/fuzz/assimp_fuzzer.cc b/fuzz/assimp_fuzzer.cc |
| 47 | +index 8178674..91ffd9d 100644 |
| 48 | +--- a/fuzz/assimp_fuzzer.cc |
| 49 | ++++ b/fuzz/assimp_fuzzer.cc |
| 50 | +@@ -47,7 +47,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | + using namespace Assimp; |
| 52 | + |
| 53 | + extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize) { |
| 54 | +- aiLogStream stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL); |
| 55 | ++ aiLogStream stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT, nullptr); |
| 56 | + aiAttachLogStream(&stream); |
| 57 | + |
| 58 | + Importer importer; |
| 59 | +diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt |
| 60 | +index 7b7fd85..1a45ada 100644 |
| 61 | +--- a/test/CMakeLists.txt |
| 62 | ++++ b/test/CMakeLists.txt |
| 63 | +@@ -100,6 +100,7 @@ SET( COMMON |
| 64 | + unit/Common/utBase64.cpp |
| 65 | + unit/Common/utHash.cpp |
| 66 | + unit/Common/utBaseProcess.cpp |
| 67 | ++ unit/Common/utLogger.cpp |
| 68 | + ) |
| 69 | + |
| 70 | + SET(Geometry |
| 71 | +diff --git a/test/unit/Common/utLogger.cpp b/test/unit/Common/utLogger.cpp |
| 72 | +new file mode 100644 |
| 73 | +index 0000000..932240a |
| 74 | +--- /dev/null |
| 75 | ++++ b/test/unit/Common/utLogger.cpp |
| 76 | +@@ -0,0 +1,52 @@ |
| 77 | ++/* |
| 78 | ++--------------------------------------------------------------------------- |
| 79 | ++Open Asset Import Library (assimp) |
| 80 | ++--------------------------------------------------------------------------- |
| 81 | ++ |
| 82 | ++Copyright (c) 2006-2024, assimp team |
| 83 | ++ |
| 84 | ++All rights reserved. |
| 85 | ++ |
| 86 | ++Redistribution and use of this software in source and binary forms, |
| 87 | ++with or without modification, are permitted provided that the following |
| 88 | ++conditions are met: |
| 89 | ++ |
| 90 | ++* Redistributions of source code must retain the above |
| 91 | ++copyright notice, this list of conditions and the |
| 92 | ++following disclaimer. |
| 93 | ++ |
| 94 | ++* Redistributions in binary form must reproduce the above |
| 95 | ++copyright notice, this list of conditions and the |
| 96 | ++following disclaimer in the documentation and/or other |
| 97 | ++materials provided with the distribution. |
| 98 | ++ |
| 99 | ++* Neither the name of the assimp team, nor the names of its |
| 100 | ++contributors may be used to endorse or promote products |
| 101 | ++derived from this software without specific prior |
| 102 | ++written permission of the assimp team. |
| 103 | ++ |
| 104 | ++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 105 | ++"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 106 | ++LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 107 | ++A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 108 | ++OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 109 | ++SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 110 | ++LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 111 | ++DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 112 | ++THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 113 | ++(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 114 | ++OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 115 | ++--------------------------------------------------------------------------- |
| 116 | ++*/ |
| 117 | ++ |
| 118 | ++#include "UnitTestPCH.h" |
| 119 | ++#include <assimp/Importer.hpp> |
| 120 | ++ |
| 121 | ++using namespace Assimp; |
| 122 | ++class utLogger : public ::testing::Test {}; |
| 123 | ++ |
| 124 | ++TEST_F(utLogger, aiGetPredefinedLogStream_leak_test) { |
| 125 | ++ aiLogStream stream1 = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT, nullptr); |
| 126 | ++ aiLogStream stream2 = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT, nullptr); |
| 127 | ++ ASSERT_EQ(stream1.callback, stream2.callback); |
| 128 | ++} |
0 commit comments