wwbmmm commented on code in PR #3663:
URL: https://github.com/apache/brpc/pull/3663#discussion_r4501009558


##########
test/rapidjson_allocator_unittest.cc:
##########
@@ -0,0 +1,53 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <gtest/gtest.h>
+#include <cstdlib>
+#include <cstring>
+#include "butil/third_party/rapidjson/allocators.h"
+
+namespace {
+struct FailingAllocator {
+    bool fail = true;
+    void* Malloc(size_t size) { return fail ? nullptr : std::malloc(size); }
+    static void Free(void* ptr) { std::free(ptr); }
+};
+
+TEST(RapidJsonAllocatorTest, allocation_failure_preserves_existing_chunks) {

Review Comment:
   [replied by brpc-oncall robot] Existing tests in this repo use CamelCase for 
suite and test names (e.g. `TEST(AlignedMemoryTest, DynamicAllocation)`, 
`TEST(Allocators, ReallocZero)`); please rename this one, e.g. 
`AllocationFailurePreservesExistingChunks`, for consistency.



##########
src/butil/third_party/rapidjson/allocators.h:
##########
@@ -228,14 +230,17 @@ class MemoryPoolAllocator {
     //! Creates a new chunk.
     /*! \param capacity Capacity of the chunk in bytes.
     */
-    void AddChunk(size_t capacity) {
+    bool AddChunk(size_t capacity) {

Review Comment:
   [replied by brpc-oncall robot] `AddChunk` now returns `bool`, but the 
doxygen block above still only documents `\param capacity`. Please add the 
return value contract, e.g. `\return true on success, false if the backing 
allocator failed (chunkHead_ is left unchanged)`.



##########
test/rapidjson_allocator_unittest.cc:
##########
@@ -0,0 +1,53 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <gtest/gtest.h>
+#include <cstdlib>
+#include <cstring>
+#include "butil/third_party/rapidjson/allocators.h"
+
+namespace {
+struct FailingAllocator {
+    bool fail = true;
+    void* Malloc(size_t size) { return fail ? nullptr : std::malloc(size); }
+    static void Free(void* ptr) { std::free(ptr); }
+};
+
+TEST(RapidJsonAllocatorTest, allocation_failure_preserves_existing_chunks) {
+    FailingAllocator allocator;
+    BUTIL_RAPIDJSON_NAMESPACE::MemoryPoolAllocator<FailingAllocator> pool(
+        32, &allocator);
+    EXPECT_EQ(nullptr, pool.Malloc(8));
+    EXPECT_EQ(0u, pool.Size());
+    allocator.fail = false;
+    char* saved = static_cast<char*>(pool.Malloc(8));
+    ASSERT_NE(nullptr, saved);
+    memcpy(saved, "stored", 7);

Review Comment:
   [replied by brpc-oncall robot] `<cstring>` is included but `memcpy` is used 
unqualified; use `std::memcpy` as done elsewhere in the code base.



##########
src/butil/third_party/rapidjson/allocators.h:
##########
@@ -178,8 +178,9 @@ class MemoryPoolAllocator {
             return NULL;
 
         size = RAPIDJSON_ALIGN(size);
-        if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
-            AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
+        if ((chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
+            && !AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
+            return NULL;

Review Comment:
   [replied by brpc-oncall robot] Returning NULL here only moves the failure to 
the caller: every bundled consumer of this allocator dereferences the result 
unconditionally — `SetStringRaw` (document.h, `str = allocator.Malloc(...)` 
followed by `memcpy`)`, `SetArrayRaw`/`SetObjectRaw` (document.h), 
`GenericValue::Reserve` (document.h), and `internal::Stack::Resize` 
(internal/stack.h:154, which then computes `stackTop_ = stack_ + size`). An OOM 
therefore still ends in a crash, just at those sites instead of inside 
`AddChunk`, so parsing is not actually made failure-tolerant by this PR. Please 
either add failure handling to those few call sites in this PR, or state 
explicitly in the description/commit that this is only a contract fix for the 
allocator with no behaviour change for DOM/parser users (and note that it 
back-ports the upstream RapidJSON fix, which helps future re-vendoring).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to