Failed cookie replacements lose data and leave the count too high

perfloop-oss/curl · Derived summary drift

https://perfloop.ai/t/oss/case_p3hytvdev6

Verdict

VERIFIED · settled 2026-11-15

What happened: The check fails before this change and passes with it.

Hypothesis

An in-memory cookie update can return out of memory after deleting the cookie it was meant to replace. The jar then records more cookies than it stores. A deterministic native test confirms both effects for one Netscape-format session-cookie update. Failure frequency and downstream impact remain unmeasured.

Curl_cookie_add calls cookie_accept before cookiedup. For an identity match, cookie_accept unlinks and frees the old entry at lib/cookie.c:1026-1035. The subsequent owned-cookie allocation can fail at lines 1177-1180. That exit neither restores the entry nor adjusts ci->numcookies. The cookielist handler propagates the allocation error to CURLOPT_COOKIELIST. This is a failed-update claim, not a claim about successful replacements or cookies deliberately removed by expiration.

The reduced counterexample is the new native test tests/unit/unit4997.c with tests/data/test4997, run in bash call call_9ibp19xHobxen9P10u24TAfL. It creates one handle, inserts example.com\tFALSE\t/\tFALSE\t0\tsid\told, and attempts the same identity with value new while failing exactly the next calloc through curl_global_init_mem. After disarming failure it reads CURLINFO_COOKIELIST and counts all stored nodes. The exact test command was `(cd .hunt-cookie/tests && perl ../../tests/runtests.pl -n -v 4997)`. Its output was `REPLAY rc=27 injected=1 public_count=0 actual=0 numcookies=1 old_present=0`, followed by failed assertions `failed replacement must preserve the existing cookie` and `jar count must equal the stored cookie count after failure`. The native test returned 2 and runtests.pl exited 1. The test bounds are two cookie calls, one allocation failure, session cookies, no transfer, expiration, concurrency, or time advance. Only new test files and build outputs were written; curl's existing mk-unity.pl generated the native test bundle without manifest edits.

The Case proof target is failure-safe replacement through the documented CURLOPT_COOKIELIST API, including preservation of the matching entry and exact stored count. A provisional next check is a bounded allocation-failure matrix covering both accepted input formats, initialized and running jars, new identities and matching identities, followed by a successful retry. It should distinguish legitimate expiration cleanup and rejected cookies from failed replacement. Passing that matrix after the repair, with successful replacement identity and creation order unchanged, would confirm the intended property; an independently required destructive failure contract would refute the preservation part. The test layout and bundling method are provisional, not additional frozen requirements.

Current upstream work was checked on 2026-11-14. The inspected implementation hunks in https://github.com/curl/curl/pull/23660 add SameSite/Partitioned behavior on an older cookie implementation, not this allocation-commit ordering. https://github.com/curl/curl/pull/23394 handles file-jar write/close failures, not in-memory replacement. Neither owns this repair. The bounded subject-index search at https://curl.se/mail/lib-2026-10/ and https://curl.se/mail/lib-2026-11/ found no matching mechanism. The checkout is shallow and the remote path-history reads were refused, so earlier merged-history coverage remains a named gap.

Change to test: Keep acceptance non-destructive until the accepted replacement’s owned storage is available, then commit replacement and count bookkeeping together. Preserve the existing cookie and exact jar count on allocation failure without changing successful identity matching, creation order, or rejection behavior.

Where it lives

perfloop-oss/curl · lib/setopt.c

Evidence

The check fails before this change and passes with it: `With initialized or running cookie storage, an accepted Netscape or Set-Cookie session-cookie replacement through CURLOPT_COOKIELIST that fails its owned-cookie allocation preserves the matching cookie, other cookies, stored count and creation order; a successful retry replaces the value without increasing the count or changing creation order.`

Checks: 4 of 4 passed. Verification: no defect found.

Timeline