Skip to content

gh-153691: Fix empty-list handling in PyInitConfig_*StrList#153816

Open
AayushMainali-Github wants to merge 1 commit into
python:mainfrom
AayushMainali-Github:fix-gh-153691
Open

gh-153691: Fix empty-list handling in PyInitConfig_*StrList#153816
AayushMainali-Github wants to merge 1 commit into
python:mainfrom
AayushMainali-Github:fix-gh-153691

Conversation

@AayushMainali-Github

@AayushMainali-Github AayushMainali-Github commented Jul 16, 2026

Copy link
Copy Markdown

Summary

PyInitConfig_GetStrList() and PyInitConfig_SetStrList() used malloc(n * sizeof(...)) even when n == 0. On platforms where malloc(0) returns NULL, that was treated as an out-of-memory failure, so a valid empty list (for example the default xoptions) incorrectly failed.

This change skips malloc(0) for empty lists:

  • GetStrList returns length == 0 and items == NULL
  • SetStrList stores {.length = 0, .items = NULL} (same as _PyWideStringList_INIT)

This matches the existing empty-list handling in _PyWideStringList_CopyEx().

Fixes #153691.

Test plan

  • Reproduced the failure mode with LD_PRELOAD forcing malloc(0)NULL
    • buggy path: GetStrList-style empty alloc returns -1
    • fixed path: returns 0 with items == NULL
  • ./python -m test test_embed -v -m test_initconfig_get_api
  • ./Programs/_testembed test_initconfig_get_api (with and without the preload)
  • ./python -m test test_embed -v -m 'test_initconfig*'
  • CI on GitHub Actions after the PR is opened

malloc(0) may return NULL on some platforms. Treat empty string lists
as a successful no-allocation result instead of out-of-memory.
@python-cla-bot

python-cla-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_initconfig_get_api fails on platforms where malloc(0) returns NULL

1 participant