Skip to content

[Beginner]: Validate protobuf parsing in TopicId::fromBytes #1367

@cheese-cakee

Description

@cheese-cakee

🐥 Beginner Friendly

This issue is a great fit for contributors who are ready to explore the Hiero C++ codebase a little more and take on slightly more independent work.

Beginner Issues often involve reading existing C++ code, understanding how different parts of the SDK fit together, and making small, thoughtful updates that follow established patterns.

The goal is to support skill growth while keeping the experience approachable, well-scoped, and enjoyable.

Important

Comment /assign on the GitHub issue to request assignment before starting work.


👾 Description of the Task

TopicId::fromBytes() currently parses protobuf bytes without checking whether parsing succeeded.

Current implementation:

TopicId TopicId::fromBytes(const std::vector<std::byte>& bytes)
{
  proto::TopicID proto;
  proto.ParseFromArray(bytes.data(), static_cast<int>(bytes.size()));
  return fromProtobuf(proto);
}

If invalid bytes are provided, ParseFromArray(...) may fail and the function can still return a default/invalid-looking TopicId instead of failing fast.

Relevant files:

src/sdk/main/src/TopicId.cc
src/sdk/tests/unit/TopicIdUnitTests.cc

💡 Proposed Solution

Update TopicId::fromBytes() to validate the parse result and throw a clear std::invalid_argument when bytes cannot be parsed into proto::TopicID.

Also add a unit test that passes invalid bytes and verifies an exception is thrown.

This should be a narrow change:

  • one production file (TopicId.cc)
  • one unit test file (TopicIdUnitTests.cc)

👩‍💻 Implementation Steps

  • Read src/sdk/main/src/TopicId.cc and locate TopicId::fromBytes()
  • Update fromBytes() to check the return value of ParseFromArray(...)
  • Throw std::invalid_argument with a descriptive message when parse fails
  • Open src/sdk/tests/unit/TopicIdUnitTests.cc
  • Add a unit test for invalid bytes that asserts TopicId::fromBytes(...) throws
  • Confirm existing FromBytes happy-path test still passes
  • Open a pull request with the changes

✅ Acceptance Criteria

  • Scope: Changes are limited to src/sdk/main/src/TopicId.cc and src/sdk/tests/unit/TopicIdUnitTests.cc
  • Behavior: Invalid bytes now throw std::invalid_argument in TopicId::fromBytes()
  • Tests: New invalid-bytes unit test is added and passes; existing tests continue to pass
  • Review: All code review feedback addressed

📋 Step-by-Step Contribution Guide

  • Comment /assign to request assignment
  • Wait for assignment
  • Fork the repository and create a branch
  • Set up the project using the instructions in README.md
  • Make the requested changes
  • Sign each commit using -s -S
  • Push your branch and open a pull request

Read Workflow Guide for step-by-step workflow guidance. Read README.md for setup instructions.

❗ Pull requests cannot be merged without S and s signed commits. See the Signing Guide.


🤔 Additional Information

This improves SDK robustness by preventing silent fallback behavior on malformed input bytes.

If you have questions while working on this issue, feel free to ask!

You can reach the community and maintainers here: Hiero-SDK-C++ Discord

Whether you need help finding the right file, understanding existing code, or confirming your approach — we're happy to help.

Metadata

Metadata

Assignees

Labels

kind: enhancementImprovement to existing code or performance without adding new featurespriority: lowNon-urgent tasks, nice-to-have improvements, or minor issuesskill: beginnerSuitable for contributors who have completed a good first issue and want to build skills

Type

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions