Ensure free space in Vector is zero-initialized#134
Open
pgaskin wants to merge 1 commit intos-yata:masterfrom
Open
Ensure free space in Vector is zero-initialized#134pgaskin wants to merge 1 commit intos-yata:masterfrom
pgaskin wants to merge 1 commit intos-yata:masterfrom
Conversation
87b1510 to
4797668
Compare
Author
|
Building words.txt before and after this change: Stack trace of the write which wrote that offset: The buffer of length 330656 passed to the write function (i.e., The |
pgaskin
added a commit
to pgaskin/go-marisa
that referenced
this pull request
Nov 8, 2025
See s-yata/marisa-trie#134. Without this, written tries may include unitialized memory, which makes them non-reproducible.
Author
|
Note that the output of a simple marisa-build command isn't changed by this PR, as it's uninitialized memory happens to be zero. This is an alternative patch which would have the same effect: diff --git a/lib/marisa/grimoire/vector/vector.h b/lib/marisa/grimoire/vector/vector.h
index afc7d9a..b25031c 100644
--- a/lib/marisa/grimoire/vector/vector.h
+++ b/lib/marisa/grimoire/vector/vector.h
@@ -256,7 +256,7 @@ class Vector {
assert(new_capacity >= size_);
assert(new_capacity <= max_size());
- std::unique_ptr<char[]> new_buf(new char[sizeof(T) * new_capacity]);
+ std::unique_ptr<char[]> new_buf(new char[sizeof(T) * new_capacity]());
T *new_objs = reinterpret_cast<T *>(new_buf.get());
static_assert(std::is_trivially_copyable_v<T>);
@@ -273,7 +273,7 @@ class Vector {
void copyInit(const T *src, std::size_t size, std::size_t capacity) {
assert(size_ == 0);
- std::unique_ptr<char[]> new_buf(new char[sizeof(T) * capacity]);
+ std::unique_ptr<char[]> new_buf(new char[sizeof(T) * capacity]());
T *new_objs = reinterpret_cast<T *>(new_buf.get());
static_assert(std::is_trivially_copyable_v<T>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes a lack of reproducibility when writing the FlatVector LoudsTrie.extras_, which comes from the Vector<uint32_t> terminals, which is set to the Tail offsets during Tail::build from LoudsTrie::build_next_trie.