(WIP) feat: add sentry_merge_context#1835
Conversation
sentry_merge_contextsentry_merge_context
| sentry__value_merge_objects(context, value); | ||
| sentry_value_decref(value); |
There was a problem hiding this comment.
Bug: The return value of sentry__value_merge_objects is ignored, which could lead to silent data loss if the merge operation fails.
Severity: LOW
Suggested Fix
Check the return value of sentry__value_merge_objects. If it indicates an error, consider logging the failure to make the code more robust and aid in debugging potential issues with context merging.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: src/sentry_core.c#L1086-L1087
Potential issue: In `sentry_merge_context` and `sentry_merge_context_n`, the return
value of the internal function `sentry__value_merge_objects` is not checked. This
function can return an error if it fails, for example, if the destination object is
frozen or if a memory allocation fails. While the analysis indicates that such failure
conditions are unlikely in normal production use, ignoring the return value creates a
defensive coding gap. If a merge operation were to fail, it would do so silently,
potentially leading to incomplete context data being processed without any indication of
an error. The reference counting is correct, so no memory leak is introduced, but data
could be lost.
Also affects:
src/sentry_core.c:1101~1102
Did we get this right? 👍 / 👎 to inform future reviews.
| const char *key, size_t key_len, sentry_value_t value); | ||
| /** | ||
| * Merges a context object with the existing context object. | ||
| * Existing keys within the context object will not be overwritten. |
There was a problem hiding this comment.
Hmm, shouldn't downstream values take precedence? For example, passing "Xbox" should override "PC", right? 🤔
There was a problem hiding this comment.
In general there are definitely values that should take precedence (e.g., on detecting steamOS for example), but these could then still use the normal SetContext. That would lose the 'merge' for other OS context though, so maybe alternatively we could add a flag to our merge called "overwrite_dst" that says whether to keep values of existing keys, or overwrite them 🤔
There was a problem hiding this comment.
What if we made the OS/Device context extensible by allowing downstream SDKs to provide the initial device context as a starting point, and then internally merge sentry-native's OS context into that?
Adding this for downstream SDKs, so they can avoid overwriting pre-set context from our native SDK init.
Popped up as part of https://github.com/getsentry/sentry-xbox/pull/151 where the unreal integration test calls AddDeviceContext which does a plain
set_by_key, making us losedevice.Xcontext set by the console SDK.