{
 "number": 36183,
 "repo": "bitcoin/bitcoin",
 "url": "https://github.com/bitcoin/bitcoin/pull/36183",
 "title": "span: diagnose dangling views from MakeByteSpan/MakeUCharSpan",
 "author": "kevkevinpal",
 "author_association": "CONTRIBUTOR",
 "created_at": "2026-09-07T14:35:44Z",
 "updated_at": "2026-09-07T22:31:07Z",
 "age_days": 10,
 "draft": false,
 "labels": [],
 "milestone": null,
 "base": "master",
 "head_sha": "6591bf02ac072bc59fcd339fd41b1befc55447a5",
 "head_ref": "span-lifetimebound-make-byte-uchar",
 "head_repo": "kevkevinpal/bitcoin",
 "head_history": [],
 "additions": 14,
 "deletions": 7,
 "changed_files": 4,
 "commit_count": 1,
 "size_bucket": "S",
 "mergeable_state": "clean",
 "bot": {
  "drahtbot": {
   "present": true,
   "reviews": {
    "concept_ack": [
     {
      "login": "jeanpablojp",
      "url": "https://github.com/bitcoin/bitcoin/pull/36183#pullrequestreview-5135551506"
     }
    ]
   },
   "conflicts": []
  }
 },
 "acks_parsed": {
  "jeanpablojp": {
   "kind": "concept_ack",
   "hash": null,
   "t": "2026-09-07T22:31:02Z",
   "stale": false
  }
 },
 "acks_tally": {
  "ack": 0,
  "stale_ack": 0,
  "concept_ack": 1,
  "approach_ack": 0,
  "nack": 0,
  "concept_nack": 0,
  "approach_nack": 0
 },
 "reviews": {
  "approved": 0,
  "changes_requested": 0,
  "distinct_reviewers": [
   "jeanpablojp"
  ]
 },
 "signals": {
  "needs_rebase": false,
  "ci_failed": false,
  "mergeable_state": "clean",
  "last_author_activity": "2026-09-07T14:34:39Z",
  "last_reviewer_activity": "2026-09-07T22:31:02Z",
  "last_reviewer": "jeanpablojp",
  "author_silent_days": 10,
  "waiting_on_author_days": 9,
  "days_since_update": 9
 },
 "refs": {
  "mentioned": [
   36164
  ],
  "depends_on": [
   36164
  ],
  "fixes": [],
  "linked_issues": [],
  "references": [
   {
    "number": 36164,
    "type": "pull",
    "state": "closed",
    "merged": true,
    "merged_at": "2026-09-06",
    "title": "util: diagnose dangling views of temporary strings"
   }
  ],
  "conflicts": []
 },
 "stack": {
  "shares_commits_with": [],
  "based_on": [],
  "base_for": []
 },
 "review_paths": [
  "src/dbwrapper.cpp",
  "src/span.h",
  "src/test/serialize_tests.cpp"
 ],
 "body": "Follow-up to #36164, as suggested in https://github.com/bitcoin/bitcoin/pull/36164#issuecomment-5541268260.\n\nTracked in https://github.com/kevkevinpal/bitcoin/issues/550.\n\n**Problem:** `MakeByteSpan` and `MakeUCharSpan` return views into their input. A temporary container can leave those views dangling.\n\n**Fix:** Add `LIFETIMEBOUND` so Clang diagnoses the misuse while preserving immediate use, matching the `util/string.h` helpers from #36164.\n\nA few call sites that wrap a temporary view (`leveldb::Slice`, `std::span`, `std::string_view`) are updated so they do not trip the new diagnostic.",
 "commits": [
  {
   "sha": "6591bf02ac072bc59fcd339fd41b1befc55447a5",
   "date": "2026-09-07T14:34:39Z",
   "message": "span: diagnose dangling views from MakeByteSpan/MakeUCharSpan\n\nFollow-up to #36164. Add LIFETIMEBOUND so Clang diagnoses storing\nspans of temporaries, matching the util/string.h helpers."
  }
 ],
 "timeline": [
  {
   "t": "2026-09-07T22:31:02Z",
   "kind": "review",
   "who": "jeanpablojp",
   "assoc": "CONTRIBUTOR",
   "state": "COMMENTED",
   "commit": "6591bf02ac072bc59fcd339fd41b1befc55447a5",
   "text": "Concept ACK\n\n`LIFETIMEBOUND` on `const V&` claims the result borrows from `v`. True when `V` owns the bytes, false when `V` is a non-owning view, and `MakeByteSpan` takes both, so the annotation also fires on correct code. The four warnings this diff works around are all that case.\n\nIt also reaches a shape outside the diff, where a function returning `MakeByteSpan` of a local `std::span` or `std::string_view` now gets `-Wreturn-stack-address`.\n\nWould a `requires (!std::ranges::borrowed_range<V>)` on the annotated overload work? Compiled both ways here, the constrained form keeps the temporary-container warning and drops the two in `net_tests`. `leveldb::Slice` is not a borrowed range, so `dbwrapper` needs its own handling regardless."
  },
  {
   "t": "2026-09-07T22:31:02Z",
   "kind": "review_comment",
   "who": "jeanpablojp",
   "assoc": "CONTRIBUTOR",
   "path": "src/span.h",
   "commit": "6591bf02ac072bc59fcd339fd41b1befc55447a5",
   "in_reply_to": null,
   "text": "Concretely this, plus a `#include <ranges>` at the top:\n\n```cpp\ntemplate <typename V> requires std::ranges::borrowed_range<V>\nauto MakeByteSpan(const V& v) noexcept\n{\n    return std::as_bytes(std::span{v});\n}\ntemplate <typename V> requires (!std::ranges::borrowed_range<V>)\nauto MakeByteSpan(const V& v LIFETIMEBOUND) noexcept\n{\n    return std::as_bytes(std::span{v});\n}\n```\n\nThe first overload takes whatever `std::ranges::borrowed_range` accepts, `std::span` and `std::string_view` among them, and everything else takes the second. `MakeUCharSpan` takes the same pair, with the `requires` sitting alongside its `decltype` return."
  },
  {
   "t": "2026-09-07T22:31:02Z",
   "kind": "review_comment",
   "who": "jeanpablojp",
   "assoc": "CONTRIBUTOR",
   "path": "src/dbwrapper.cpp",
   "commit": "6591bf02ac072bc59fcd339fd41b1befc55447a5",
   "in_reply_to": null,
   "text": "Neither form keeps the helper here. `MakeByteSpan(m_impl_iter->iter->key())` warns because the `Slice` is a temporary, and binding it to a local only changes the warning to \"address of stack memory associated with local variable 'key' returned\".\n\nWould opting the `Slice` in be worth it? On top of those overloads it brings both getters back to the helper, and a stored `MakeByteSpan` of a temporary `std::vector` still warns. On its own it changes nothing, since the annotation as it stands is unconditional.\n\n```cpp\ntemplate<> inline constexpr bool std::ranges::enable_borrowed_range<leveldb::Slice> = true;\n```"
  },
  {
   "t": "2026-09-07T22:31:02Z",
   "kind": "review_comment",
   "who": "jeanpablojp",
   "assoc": "CONTRIBUTOR",
   "path": "src/test/serialize_tests.cpp",
   "commit": "6591bf02ac072bc59fcd339fd41b1befc55447a5",
   "in_reply_to": null,
   "text": "`UncopyableStream` inherits the `DataStream` constructor that copies into `vch`, so no view is kept here. Reverted, the file still compiles clean.\n\n```suggestion\n    UncopyableStream stream{MakeByteSpan(std::string_view{\"abc\"})};\n```"
  }
 ],
 "labels_log": [],
 "state_log": [],
 "text_chars": 3067,
 "text_tokens_estimate": 766,
 "changed_paths": [
  "src/dbwrapper.cpp",
  "src/span.h",
  "src/test/net_tests.cpp",
  "src/test/serialize_tests.cpp"
 ],
 "files": [
  {
   "path": "src/dbwrapper.cpp",
   "add": 4,
   "del": 2
  },
  {
   "path": "src/span.h",
   "add": 4,
   "del": 2
  },
  {
   "path": "src/test/net_tests.cpp",
   "add": 4,
   "del": 2
  },
  {
   "path": "src/test/serialize_tests.cpp",
   "add": 2,
   "del": 1
  }
 ],
 "test_lines": 9,
 "git": {
  "head": "6591bf02ac072bc59fcd339fd41b1befc55447a5",
  "head_matches_backup": true,
  "base": "19b0ff2fa0500b7ab71ae60080c1daf98d722270",
  "commits": [
   {
    "sha": "6591bf02ac",
    "subject": "span: diagnose dangling views from MakeByteSpan/MakeUCharSpan",
    "files": 4,
    "add": 14,
    "del": 7
   }
  ],
  "patch_truncated": false
 },
 "input_hash": "1c6888433ee64b10",
 "extracted_at": "2026-09-17T16:15:31+00:00"
}