diff options
author | nadya73 <nadya73@yandex-team.com> | 2025-02-06 10:22:46 +0300 |
---|---|---|
committer | nadya73 <nadya73@yandex-team.com> | 2025-02-06 10:50:57 +0300 |
commit | b95f4be407ba7fb5befb4293f04fb96fd795bd3a (patch) | |
tree | 3b185c6ef352acf13d955c8e8f28e9fc8c9c5323 /library | |
parent | 56887343f8d710aa97ace7f00188ef42334db324 (diff) | |
download | ydb-b95f4be407ba7fb5befb4293f04fb96fd795bd3a.tar.gz |
Fix UB in chunked memory pool
* Changelog entry
Type: fix
Component: misc-server
Fix UB in chunked memory pool
commit_hash:d2d5a9707e30abe1814d1ac0c33fc0f2aba134e2
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/yt/memory/chunked_memory_pool-inl.h | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/library/cpp/yt/memory/chunked_memory_pool-inl.h b/library/cpp/yt/memory/chunked_memory_pool-inl.h index c6ed21fbac..68d346167f 100644 --- a/library/cpp/yt/memory/chunked_memory_pool-inl.h +++ b/library/cpp/yt/memory/chunked_memory_pool-inl.h @@ -78,7 +78,7 @@ inline TChunkedMemoryPool::TChunkedMemoryPool( inline char* TChunkedMemoryPool::AllocateUnaligned(size_t size) { // Fast path. - if (FreeZoneEnd_ >= FreeZoneBegin_ + size) { + if (FreeZoneBegin_ && FreeZoneEnd_ >= FreeZoneBegin_ + size) { FreeZoneEnd_ -= size; Size_ += size; return FreeZoneEnd_; @@ -90,15 +90,17 @@ inline char* TChunkedMemoryPool::AllocateUnaligned(size_t size) inline char* TChunkedMemoryPool::AllocateAligned(size_t size, int align) { - // NB: This can lead to FreeZoneBegin_ >= FreeZoneEnd_ in which case the chunk is full. - FreeZoneBegin_ = AlignUp(FreeZoneBegin_, align); - - // Fast path. - if (FreeZoneBegin_ + size <= FreeZoneEnd_) { - char* result = FreeZoneBegin_; - Size_ += size; - FreeZoneBegin_ += size; - return result; + if (FreeZoneBegin_) { + // NB: This can lead to FreeZoneBegin_ >= FreeZoneEnd_ in which case the chunk is full. + FreeZoneBegin_ = AlignUp(FreeZoneBegin_, align); + + // Fast path. + if (FreeZoneBegin_ + size <= FreeZoneEnd_) { + char* result = FreeZoneBegin_; + Size_ += size; + FreeZoneBegin_ += size; + return result; + } } // Slow path. |