Repository navigation
Fix dangling stack references in ParseGuid and subscription parsing - #8
Merged
Merged
Conversation
- ParseGuid(data, swapEndianness = true) read through a pointer to a block-scoped array after the block ended; optimized g++ builds returned garbage Guids - ParseSubscriptionRequest bound a const Guid& into a temporary Nullable<Guid> and used it when building the subscriber signal index cache Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
Fixes two uses of stack memory after its lifetime ended (undefined behavior). Both are one-line changes.
ParseGuid(const uint8_t* data, bool swapEndianness)—src/lib/Convert.cppswappedBytes[16]was declared inside theif (swapEndianness)block,encodedByteswas pointed at it, and the bytes were read throughencodedBytesafter the block had ended. With g++ 10.5 at-O2the swapped bytes are never stored, so every .NET-ordered Guid decodes as garbage; unoptimized builds happen to work. The array is now declared at function scope.No code in this repository calls
ParseGuidwithswapEndianness = true, so this only affects consumers of the public API. It was found in a port of this code (snapdb/cppapi), where Release builds decoded every Guid incorrectly.SubscriberConnection::ParseSubscriptionRequest—src/lib/transport/SubscriberConnection.cppValueAsGuidreturns a temporaryNullable<Guid>andGetValueOrDefaultreturns a reference to its member, sosignalIDdangled once the statement ended. It was then passed toSignalIndexCache::AddMeasurementKey. This is publisher side: it determines the signal IDs placed in a subscriber's signal index cache.signalIDis now held by value.The existing tests pass in Release with or without this change; it was only caught by AddressSanitizer.
Testing
ParseGuid(…, true)before-O2(garbage / all-zero Guids), passes at-O0ParseGuid(…, true)afterFilterExpressionTests,PublisherCacheIndexTest,ConfigurationFramesTest,SubscriberStartupTestSimpleSubscribeagainst a local openHistorianAddressSanitizer (
-fsanitize=address -fsanitize-address-use-after-scope, Linux):ParseGuid:stack-use-after-scopeatConvert.cpp:604; clean after the fix.PublisherCacheIndexTest: aborted withstack-use-after-scopeunderSubscriberConnection.cpp:1410before the fix; clean after.Not included
ParseGuid(…, true); it was checked with a throwaway program linked against the built library.🤖 Generated with Claude Code