Skip to content

Use offsetof for struct field pointer calculation instead of implicit layout assumption#34365

Closed
Copilot wants to merge 1 commit intofix/fixCrashOfTimeWhiteListfrom
copilot/sub-pr-34351
Closed

Use offsetof for struct field pointer calculation instead of implicit layout assumption#34365
Copilot wants to merge 1 commit intofix/fixCrashOfTimeWhiteListfrom
copilot/sub-pr-34351

Conversation

Copy link

Copilot AI commented Jan 21, 2026

Addresses review feedback on PR #34351 regarding fragile pointer casting that assumed specific memory layout.

Changes

  • Replaced implicit cast with explicit offset calculation: The original code cast &pList->numWhiteLists directly to SDateTimeWhiteList*, relying on numWhiteLists being the first field. This is fragile and breaks if struct layout changes.

  • Introduced helper function uvListIsTimeInWhiteList: Encapsulates the pointer arithmetic using offsetof() macro for compile-time safety.

  • Added robustness improvements: Additional null checks, explicit ownership transfer semantics in uvDataTimeWhiteListAdd(), and proper cleanup in uvDataTimeWhiteListDestroy().

Before

SDateTimeWhiteList* p = (SDateTimeWhiteList*)&pList->numWhiteLists;
int64_t now = taosGetTimestampSec();
if (isTimeInDateTimeWhiteList(p, now)) {
  return true;
}

After

const SDateTimeWhiteList* pList =
    (const SDateTimeWhiteList*)((const char*)pWhiteList + offsetof(SUserDateTimeWhiteList, numWhiteLists));
return isTimeInDateTimeWhiteList(pList, now);

The offsetof approach makes the struct layout dependency explicit and will cause compile-time errors if the struct definition changes incompatibly.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI mentioned this pull request Jan 21, 2026
3 tasks
Copilot AI changed the title [WIP] Address feedback from review on previous PR Use offsetof for struct field pointer calculation instead of implicit layout assumption Jan 21, 2026
Copilot AI requested a review from yihaoDeng January 21, 2026 12:16
@minhuinie minhuinie deleted the branch fix/fixCrashOfTimeWhiteList February 4, 2026 03:02
@minhuinie minhuinie closed this Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants