mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-26 02:21:57 +00:00
fixes #25251; SIGBUS with iterator over const Table lookup - premature temporary destruction (#25255)
fixes #25251
enforce a copy if the arg is a deref of a lent pointer since the arg
could be a temporary that will go out of scope
(cherry picked from commit 6f73094263)
This commit is contained in:
@@ -432,3 +432,28 @@ block:
|
||||
let x = cast[typeof(aaa)](aaa) # not even var
|
||||
for _ in x[]:
|
||||
discard
|
||||
|
||||
import std/[tables, unicode, sequtils]
|
||||
|
||||
const
|
||||
myTable = {
|
||||
"en": "abcdefghijklmnopqrstuvwxyz",
|
||||
}.toTable
|
||||
|
||||
proc buggyVersion(locale: string): seq[Rune] =
|
||||
result = toSeq(runes(myTable[locale]))
|
||||
|
||||
proc workingVersion(locale: string): seq[Rune] =
|
||||
# string lifetime is extended
|
||||
let str = myTable[locale]
|
||||
result = toSeq(runes(str))
|
||||
|
||||
# echo "Testing working version..."
|
||||
let runes2 = workingVersion("en")
|
||||
# echo "Got ", runes2.len, " runes"
|
||||
|
||||
# echo "Testing buggy version..."
|
||||
let runes1 = buggyVersion("en") # <-- CRASHES HERE
|
||||
|
||||
doAssert runes1.len == runes2.len
|
||||
# echo "Got ", runes1.len, " runes"
|
||||
|
||||
Reference in New Issue
Block a user