{
  "id": "go/unsafe-string-conversion",
  "signature": "panic: runtime error: slice bounds out of range",
  "signature_zh": "恐慌：运行时错误：切片索引越界",
  "regex": "panic:\\ runtime\\ error:\\ slice\\ bounds\\ out\\ of\\ range",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Converting a `[]byte` to `string` using `unsafe` without ensuring proper length, leading to out-of-bounds read.",
  "root_cause_type": "generic",
  "root_cause_zh": "使用`unsafe`将`[]byte`转换为`string`时未确保正确长度，导致越界读取。",
  "versions": [
    {
      "version": "1.19",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.20",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Using `*(*string)(unsafe.Pointer(&b))` directly",
      "why_fails": "String header may have wrong length if slice header changes.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Assuming zero-copy is always safe",
      "why_fails": "If slice is modified later, string becomes invalid.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use `string(b)` for safe conversion (copies)",
      "success_rate": 1.0,
      "how": "s := string(b)\n// s is immutable copy",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use `unsafe.String` with explicit length (Go 1.20+)",
      "success_rate": 0.8,
      "how": "s := unsafe.String(&b[0], len(b))\n// but ensure b is not mutated",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-08-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}