-
Notifications
You must be signed in to change notification settings - Fork 88
fix(cpp): remove UB when reading unset builder vertex id #892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| #include <cassert> | ||
| #include <cstddef> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <unordered_set> | ||
|
|
@@ -51,21 +52,30 @@ namespace graphar::builder { | |
| */ | ||
| class Vertex { | ||
| public: | ||
| Vertex() : empty_(true) {} | ||
| Vertex() = default; | ||
|
|
||
| /** | ||
| * @brief Initialize the vertex with a given id. | ||
| * | ||
| * @param id The id of the vertex. | ||
| */ | ||
| explicit Vertex(IdType id) : id_(id), empty_(false) {} | ||
| explicit Vertex(IdType id) : id_(id) {} | ||
|
|
||
| /** | ||
| * @brief Get id of the vertex. | ||
| * | ||
| * The id is absent until explicitly set or assigned by VerticesBuilder. | ||
| * | ||
| * @return The id of the vertex. | ||
| */ | ||
| IdType GetId() const noexcept { return id_; } | ||
| IdType GetId() const { return id_.value(); } | ||
|
|
||
|
Comment on lines
+71
to
+72
|
||
| /** | ||
| * @brief Check if the vertex id has been initialized. | ||
| * | ||
| * @return true/false. | ||
| */ | ||
| bool HasId() const noexcept { return id_.has_value(); } | ||
|
|
||
| /** | ||
| * @brief Set id of the vertex. | ||
|
|
@@ -75,11 +85,11 @@ class Vertex { | |
| void SetId(IdType id) { id_ = id; } | ||
|
|
||
| /** | ||
| * @brief Check if the vertex is empty. | ||
| * @brief Check if the vertex contains no property payload. | ||
| * | ||
| * @return true/false. | ||
| */ | ||
| bool Empty() const noexcept { return empty_; } | ||
| bool Empty() const noexcept { return properties_.empty(); } | ||
|
|
||
| /** | ||
| * @brief Add a property to the vertex. | ||
|
|
@@ -89,7 +99,6 @@ class Vertex { | |
| */ | ||
| // TODO(@acezen): Enable the property to be a vector(list). | ||
| void AddProperty(const std::string& name, const std::any& val) { | ||
| empty_ = false; | ||
| properties_[name] = val; | ||
| } | ||
|
|
||
|
|
@@ -100,7 +109,6 @@ class Vertex { | |
| AddProperty(name, val); | ||
| return; | ||
| } | ||
| empty_ = false; | ||
| if (cardinalities_.find(name) != cardinalities_.end()) { | ||
| if (cardinalities_[name] != cardinality) { | ||
| throw std::runtime_error("Cardinality mismatch for property: " + name); | ||
|
|
@@ -211,8 +219,7 @@ class Vertex { | |
| } | ||
|
|
||
| private: | ||
| IdType id_; | ||
| bool empty_; | ||
| std::optional<IdType> id_; | ||
| std::unordered_map<std::string, std::any> properties_; | ||
| std::unordered_map<std::string, Cardinality> cardinalities_; | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,6 +111,16 @@ def test_vertices_builder(sample_graph_vertex): | |
| # Set validate level | ||
| builder.SetValidateLevel(ValidateLevel.strong_validate) | ||
|
|
||
| empty_vertex = BuilderVertex() | ||
| assert empty_vertex.Empty() | ||
| with pytest.raises(Exception): | ||
| empty_vertex.GetId() | ||
| empty_vertex.SetId(42) | ||
|
Comment on lines
+116
to
+118
|
||
| assert empty_vertex.GetId() == 42 | ||
| assert empty_vertex.Empty() | ||
| empty_vertex.AddProperty("id", 42) | ||
| assert not empty_vertex.Empty() | ||
|
|
||
| # Prepare vertex data | ||
| vertex_count = 3 | ||
| property_names = ["id", "firstName", "lastName", "gender"] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new doc comment says the id may be “assigned by VerticesBuilder”, but it doesn’t clarify whether that id is relative to the builder (0..N-1) or includes
start_vertex_index_. SinceAddVertex(..., index=-1)assignsvertices_.size()today, consider clarifying this wording to avoid misleading API users about what id they will observe.