feat(std): add bounds-tracked string builder#10
Open
NKS01X wants to merge 1 commit into
Open
Conversation
Author
|
@microsoft-github-policy-service agree |
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.
Resolves #9
The Problem
Standard C string manipulation functions (
strcat,sprintf, etc.) are notoriously prone to buffer overflows and silent truncation.lib0xcneeded a safe, bounds-tracked mechanism for dynamically constructing strings within statically-allocated buffers.The Solution
This PR introduces
xc_str_builder_tand its associated API to thestdmodule. It provides a lightweight, bounds-checked wrapper around a character array that safely handles appends and formatting without risking memory corruption.Key Features Implemented:
xc_str_builder_init: Safely wraps a statically-allocated buffer, ensuring it is properly null-terminated from the start.xc_str_builder_append: Appends strings while strictly enforcing capacity limits.xc_str_builder_appendf: Safely formats data usingvsnprintf. Includes specific logic to detect silent truncation and actively "undo" partial writes to prevent corrupting the buffer state.xc_str_builder_append_char: Fast, O(1) safe single-character appends.clearandtruncateutilities for safe buffer reuse.Testing
0xtest/unit/test_str_builder.c.UNIT_TESTframework (0xc/sys/check.h).Notes for Reviewers
All formatting strictly adheres to the project's C guidelines, including Allman-style bracing, padded function parameters, and the required Microsoft copyright headers.