[vim/vim] fix: ":for" over a tuple leaks memory on every iteration (PR #20914)

Samuel Schlesinger (Vim Github Repository) <[email protected]> Sat, 01 Aug 2026 12:38:54 -0700
Newsgroups gmane.editors.vim.devel
Message-ID <vim/vim/pull/[email protected]>

----==_mimepart_6a6e4b4e7d2a1_d11160359893b
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Problem:  Looping over a tuple with ":for" copies each item with copy_tv() =
but never clears the copy, leaking the value on every iteration (26MB over =
100k iterations of a two-string tuple).  Container items keep an extra refe=
rence forever, also defeating garbage collection.

Solution: Clear the copied typval on both return paths, like the string bra=
nch of next_for_item() already does.

Disclosure: Claude Fable 5 was used to assist with finding this bug. I revi=
ewed this, but I am new to this codebase so forgive me if I am wrong about =
this problem. This fix was also verified with tests.
You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/20914

-- Commit Summary --

  * fix: ":for" over a tuple leaks memory on every iteration

-- File Changes --

    M src/eval.c (7)

-- Patch Links --

https://github.com/vim/vim/pull/20914.patch
https://github.com/vim/vim/pull/20914.diff

--=20
Reply to this email directly or view it on GitHub:
https://github.com/vim/vim/pull/20914
You are receiving this because you are subscribed to this thread.

Message ID: <vim/vim/pull/[email protected]>

--=20
--=20
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---=20
You received this message because you are subscribed to the Google Groups "=
vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/vim_dev/vim=
/vim/pull/20914%40github.com.

----==_mimepart_6a6e4b4e7d2a1_d11160359893b
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<p dir=3D"auto">Problem:  Looping over a tuple with ":for" copies each item=
 with copy_tv() but never clears the copy, leaking the value on every itera=
tion (26MB over 100k iterations of a two-string tuple).  Container items ke=
ep an extra reference forever, also defeating garbage collection.</p>
<p dir=3D"auto">Solution: Clear the copied typval on both return paths, lik=
e the string branch of next_for_item() already does.</p>
<p dir=3D"auto">Disclosure: Claude Fable 5 was used to assist with finding =
this bug. I reviewed this, but I am new to this codebase so forgive me if I=
 am wrong about this problem. This fix was also verified with tests.</p>

<hr>

<h4>You can view, comment on, or merge this pull request online at:</h4>
<p>&nbsp;&nbsp;<a href=3D'https://github.com/vim/vim/pull/20914'>https://gi=
thub.com/vim/vim/pull/20914</a></p>

<h4>Commit Summary</h4>
<ul>
  <li><a href=3D"https://github.com/vim/vim/pull/20914/commits/9a4ad0117678=
e829deb614cf6d0b338e38a3c927" class=3D"commit-link">9a4ad01</a>  fix: &quot=
;:for&quot; over a tuple leaks memory on every iteration</li>
</ul>

<h4 style=3D"display: inline-block">File Changes </h4> <p style=3D"display:=
 inline-block">(<a href=3D"https://github.com/vim/vim/pull/20914/files">1&n=
bsp;file</a>)</p>
<ul>
  <li>
    <strong>M</strong>
    <a href=3D"https://github.com/vim/vim/pull/20914/files#diff-3f6891fec05=
432f415cfd49150db67e2bc72d20e0a94a4e3ac762f7ff55f9921">src/eval.c</a>
    (7)
  </li>
</ul>

<h4>Patch Links:</h4>
<ul>
  <li><a href=3D'https://github.com/vim/vim/pull/20914.patch'>https://githu=
b.com/vim/vim/pull/20914.patch</a></li>
  <li><a href=3D'https://github.com/vim/vim/pull/20914.diff'>https://github=
.com/vim/vim/pull/20914.diff</a></li>
</ul>

<p style=3D"font-size:small;-webkit-text-size-adjust:none;color:#666;">&mda=
sh;<br />Reply to this email directly, <a href=3D"https://github.com/vim/vi=
m/pull/20914">view it on GitHub</a>, or <a href=3D"https://github.com/notif=
ications/unsubscribe-auth/ACY5DGC6T6JPU2NU7MXHBG35HZBM5AVCNFSNUABEKJSXA33TN=
F2G64TZHM2DAOJZG42DQMR3JFZXG5LFHM2TAMZYG4YDANRVGSQXMAQ">unsubscribe</a>.<br=
 />Triage notifications, keep track of coding agent tasks and review pull r=
equests on the go with GitHub Mobile for <a href=3D"https://github.com/noti=
fications/mobile/ios/ACY5DGFOVCNLJDYTC2YPFDT5HZBM5A5CNFSNUABEM5UWIORPF5TWS5=
BNNB2WEL2QOVWGYUTFOF2WK43UF42DCOBVGIYTSNZYGOTHEZLBONXW5KTTOVRHGY3SNFRGKZFFM=
V3GK3TUVJTG633UMVZF62LPOM">iOS</a> and <a href=3D"https://github.com/notifi=
cations/mobile/android/ACY5DGDWKJL7KERLVI47FAD5HZBM5A5CNFSNUABEM5UWIORPF5TW=
S5BNNB2WEL2QOVWGYUTFOF2WK43UF42DCOBVGIYTSNZYGOTHEZLBONXW5KTTOVRHGY3SNFRGKZF=
FMV3GK3TUVZTG633UMVZF6YLOMRZG62LE">Android</a>. Download it today!
<br />You are receiving this because you are subscribed to this thread.<img=
 src=3D"https://github.com/notifications/beacon/ACY5DGD6WE7NGCYO5V3EATL5HZB=
M5BFCNFSM6AAAAAC4V66BCKWGG33NNVSW45C7OR4XAZNFJFZXG5LFVJRW63LNMVXHIX3JMTHQAA=
AAAEWFI6DOUZZGKYLTN5XKU43VMJZWG4TJMJSWI.gif" height=3D"1" width=3D"1" alt=
=3D"" /><span style=3D"color: transparent; font-size: 0; display: none; vis=
ibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-wid=
th: 0; max-height: 0; mso-hide: all">Message ID: <span>&lt;vim/vim/pull/209=
14</span><span>@</span><span>github</span><span>.</span><span>com&gt;</span=
></span></p>
<script type=3D"application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/vim/vim/pull/20914",
"url": "https://github.com/vim/vim/pull/20914",
"name": "View Pull Request"
},
"description": "View this Pull Request on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>

<p></p>

-- <br />
-- <br />
You received this message from the &quot;vim_dev&quot; maillist.<br />
Do not top-post! Type your reply below the text you are replying to.<br />
For more information, visit <a href=3D"http://www.vim.org/maillist.php">htt=
p://www.vim.org/maillist.php</a><br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;vim_dev&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">vim_dev+uns=
[email protected]</a>.<br />
To view this discussion visit <a href=3D"https://groups.google.com/d/msgid/=
vim_dev/vim/vim/pull/20914%40github.com?utm_medium=3Demail&utm_source=3Dfoo=
ter">https://groups.google.com/d/msgid/vim_dev/vim/vim/pull/20914%40github.=
com</a>.<br />

----==_mimepart_6a6e4b4e7d2a1_d11160359893b--